Skip to content

Commit

Permalink
Implement JBuilder for AnswersController
Browse files Browse the repository at this point in the history
* Add JBuilder gem
* Remove Active Model Serializer
* Generate JBuilder views and partials for Answers
* Implement JBuilder render in controller
* Create mock partials placeholders for users, default and comments JBuilder views
  • Loading branch information
Osmond Oscar committed Feb 23, 2016
1 parent 6fcfe9b commit c5438f3
Show file tree
Hide file tree
Showing 21 changed files with 34 additions and 58 deletions.
Binary file removed .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ gem 'figaro'
gem 'ancestry'
gem 'will_paginate'
# gem 'kaminari'
gem "active_model_serializers", github: "rails-api/active_model_serializers"
gem 'jbuilder'
gem "jwt"
gem 'unicorn-rails'
gem 'rack-cors'
Expand Down
14 changes: 4 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
GIT
remote: git://github.com/rails-api/active_model_serializers.git
revision: b45f7b4ffea36b44fdf7be075295ad9db6bea1b0
specs:
active_model_serializers (0.10.0.rc4)
actionpack (>= 4.0)
activemodel (>= 4.0)
railties (>= 4.0)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -63,6 +54,9 @@ GEM
activesupport (>= 4.1.0)
hashie (3.4.3)
i18n (0.7.0)
jbuilder (2.4.1)
activesupport (>= 3.0.0, < 5.1)
multi_json (~> 1.2)
json (1.8.3)
jwt (1.5.1)
kgio (2.10.0)
Expand Down Expand Up @@ -184,10 +178,10 @@ PLATFORMS
ruby

DEPENDENCIES
active_model_serializers!
ancestry
faker
figaro
jbuilder
jwt
omniauth-google-oauth2
omniauth-oauth2 (~> 1.3.1)
Expand Down
7 changes: 2 additions & 5 deletions app/controllers/answers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,25 @@ class AnswersController < ApplicationController

def index
@answers = @question.answers.with_votes

render json: @answers
end

def show
render json: @answer
end

def create
@answer = @question.answers.new(answer_params)
@answer.user = current_user

if @answer.save
render json: @answer, status: :created, location: question_answer_path(@question, @answer)
render :show
else
invalid_request(error_msg)
end
end

def update
if @answer.update(answer_params)
render json: @answer, status: 200
render :show
else
invalid_request(error_msg)
end
Expand Down
1 change: 0 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class ApplicationController < ActionController::API
include ActionController::HttpAuthentication::Token::ControllerMethods
include ActionController::Serialization

attr_reader :current_user
before_action :authenticate_user
Expand Down
4 changes: 0 additions & 4 deletions app/serializers/answer_serializer.rb

This file was deleted.

4 changes: 0 additions & 4 deletions app/serializers/comment_serializer.rb

This file was deleted.

8 changes: 0 additions & 8 deletions app/serializers/main_serializer.rb

This file was deleted.

12 changes: 0 additions & 12 deletions app/serializers/question_serializer.rb

This file was deleted.

13 changes: 0 additions & 13 deletions app/serializers/user_serializer.rb

This file was deleted.

7 changes: 7 additions & 0 deletions app/views/answers/_answer.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
json.answers(answers) do |answer|
json.partial! 'comments/default', data: answer
json.extract! answer, :question_id
json.extract! answer, :comments_count
json.partial! 'users/user', user: answer.user
json.partial! 'comments/comment', comments: answer.comments
end
1 change: 1 addition & 0 deletions app/views/answers/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! 'answer', answers: @answers
5 changes: 5 additions & 0 deletions app/views/answers/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
json.partial! 'comments/default', data: @answer
json.extract! @answer, :question_id
json.extract! @answer, :comments_count
json.partial! 'users/user', user: @answer.user
json.partial! 'comments/comment', comments: @answer.comments
1 change: 1 addition & 0 deletions app/views/comments/_comment.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.comments comments, partial: 'comments/default', as: :data
2 changes: 2 additions & 0 deletions app/views/comments/_default.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.(data, :id, :content, :votes_count, :created_at, :updated_at)
json.partial! 'users/user', user: data.user
4 changes: 4 additions & 0 deletions app/views/comments/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
json.array!(@comments) do |comment|
json.extract! comment, :id
json.url comment_url(comment, format: :json)
end
1 change: 1 addition & 0 deletions app/views/comments/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.extract! @comment, :id, :created_at, :updated_at
1 change: 1 addition & 0 deletions app/views/users/_user.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.user user
4 changes: 4 additions & 0 deletions app/views/users/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
json.array!(@users) do |user|
json.extract! user, :id
json.url user_url(user, format: :json)
end
1 change: 1 addition & 0 deletions app/views/users/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.extract! @user, :id, :created_at, :updated_at
Binary file removed docs/.DS_Store
Binary file not shown.

0 comments on commit c5438f3

Please sign in to comment.