From c5438f3252d5fedda72ad654735aa4d5d7a5feee Mon Sep 17 00:00:00 2001 From: Osmond Oscar Date: Thu, 18 Feb 2016 11:28:05 +0100 Subject: [PATCH] Implement JBuilder for AnswersController * 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 --- .DS_Store | Bin 6148 -> 0 bytes Gemfile | 2 +- Gemfile.lock | 14 ++++---------- app/controllers/answers_controller.rb | 7 ++----- app/controllers/application_controller.rb | 1 - app/serializers/answer_serializer.rb | 4 ---- app/serializers/comment_serializer.rb | 4 ---- app/serializers/main_serializer.rb | 8 -------- app/serializers/question_serializer.rb | 12 ------------ app/serializers/user_serializer.rb | 13 ------------- app/views/answers/_answer.json.jbuilder | 7 +++++++ app/views/answers/index.json.jbuilder | 1 + app/views/answers/show.json.jbuilder | 5 +++++ app/views/comments/_comment.json.jbuilder | 1 + app/views/comments/_default.json.jbuilder | 2 ++ app/views/comments/index.json.jbuilder | 4 ++++ app/views/comments/show.json.jbuilder | 1 + app/views/users/_user.json.jbuilder | 1 + app/views/users/index.json.jbuilder | 4 ++++ app/views/users/show.json.jbuilder | 1 + docs/.DS_Store | Bin 6148 -> 0 bytes 21 files changed, 34 insertions(+), 58 deletions(-) delete mode 100644 .DS_Store delete mode 100644 app/serializers/answer_serializer.rb delete mode 100644 app/serializers/comment_serializer.rb delete mode 100644 app/serializers/main_serializer.rb delete mode 100644 app/serializers/question_serializer.rb delete mode 100644 app/serializers/user_serializer.rb create mode 100644 app/views/answers/_answer.json.jbuilder create mode 100644 app/views/answers/index.json.jbuilder create mode 100644 app/views/answers/show.json.jbuilder create mode 100644 app/views/comments/_comment.json.jbuilder create mode 100644 app/views/comments/_default.json.jbuilder create mode 100644 app/views/comments/index.json.jbuilder create mode 100644 app/views/comments/show.json.jbuilder create mode 100644 app/views/users/_user.json.jbuilder create mode 100644 app/views/users/index.json.jbuilder create mode 100644 app/views/users/show.json.jbuilder delete mode 100644 docs/.DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 0bc3d0eaba4699ca2343b708a96d5964416948f2..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKO^ee&7=EX%yJ;6BqN3m>M}@LkWQd40NA3oZNkj$`$5I8^FO1*k zX~_zz! zjdLp1`$j zhu)zd`;H&@$y1}uudaDvGTGRwTASOO)2cP8*K1X4d#gU3n(qzc(v_Xv{ab_SvllO4 zy?*od-G`52#>B8n3p=ZD0iRGtexWIPA*?~!S+;1rUOrmrrCZ3Fb)RUPyp)hsyV3wd0 zYe>|&RzNF|RiG%VCg1<}et-VYI_a8LKr8THDZomtPOFJ2sl9b+a(u6~knSOI= 4.0) - activemodel (>= 4.0) - railties (>= 4.0) - GEM remote: https://rubygems.org/ specs: @@ -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) @@ -184,10 +178,10 @@ PLATFORMS ruby DEPENDENCIES - active_model_serializers! ancestry faker figaro + jbuilder jwt omniauth-google-oauth2 omniauth-oauth2 (~> 1.3.1) diff --git a/app/controllers/answers_controller.rb b/app/controllers/answers_controller.rb index 96476a6..7bc7f42 100644 --- a/app/controllers/answers_controller.rb +++ b/app/controllers/answers_controller.rb @@ -5,12 +5,9 @@ class AnswersController < ApplicationController def index @answers = @question.answers.with_votes - - render json: @answers end def show - render json: @answer end def create @@ -18,7 +15,7 @@ def create @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 @@ -26,7 +23,7 @@ def create def update if @answer.update(answer_params) - render json: @answer, status: 200 + render :show else invalid_request(error_msg) end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 064ce93..bd1e6bd 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,6 +1,5 @@ class ApplicationController < ActionController::API include ActionController::HttpAuthentication::Token::ControllerMethods - include ActionController::Serialization attr_reader :current_user before_action :authenticate_user diff --git a/app/serializers/answer_serializer.rb b/app/serializers/answer_serializer.rb deleted file mode 100644 index 227cefc..0000000 --- a/app/serializers/answer_serializer.rb +++ /dev/null @@ -1,4 +0,0 @@ -class AnswerSerializer < MainSerializer - attributes :question_id, :comments_count, :comments - has_many :comments -end diff --git a/app/serializers/comment_serializer.rb b/app/serializers/comment_serializer.rb deleted file mode 100644 index 9df92e7..0000000 --- a/app/serializers/comment_serializer.rb +++ /dev/null @@ -1,4 +0,0 @@ -class CommentSerializer < MainSerializer - attributes :user - belongs_to :user -end diff --git a/app/serializers/main_serializer.rb b/app/serializers/main_serializer.rb deleted file mode 100644 index 71964ca..0000000 --- a/app/serializers/main_serializer.rb +++ /dev/null @@ -1,8 +0,0 @@ -class MainSerializer < ActiveModel::Serializer - attributes :id, :content, :votes_count, :voted, :created_at, :updated_at - belongs_to :user - - def voted - object.votes.voted?(object.class.to_s, object.id, object.user) - end -end diff --git a/app/serializers/question_serializer.rb b/app/serializers/question_serializer.rb deleted file mode 100644 index 908ab4b..0000000 --- a/app/serializers/question_serializer.rb +++ /dev/null @@ -1,12 +0,0 @@ -class QuestionSerializer < MainSerializer - attributes :title, :tags, :answers_count, :comments_count, :views - - def attributes(*args) - class_eval { has_many :comments; has_many :answers } if instance_options[:include_answers] - super - end - - def answers - object.answers.with_votes - end -end diff --git a/app/serializers/user_serializer.rb b/app/serializers/user_serializer.rb deleted file mode 100644 index aea5eae..0000000 --- a/app/serializers/user_serializer.rb +++ /dev/null @@ -1,13 +0,0 @@ -class UserSerializer < ActiveModel::Serializer - attributes :id, :name, :email, :image, :points - - def attributes(*args) - data = super - data['api_key'] = instance_options[:api_key] if instance_options[:api_key] - data - end - - def image - object.social_providers.first.try(:profile_picture) - end -end diff --git a/app/views/answers/_answer.json.jbuilder b/app/views/answers/_answer.json.jbuilder new file mode 100644 index 0000000..9c6f52b --- /dev/null +++ b/app/views/answers/_answer.json.jbuilder @@ -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 diff --git a/app/views/answers/index.json.jbuilder b/app/views/answers/index.json.jbuilder new file mode 100644 index 0000000..d2c5ad4 --- /dev/null +++ b/app/views/answers/index.json.jbuilder @@ -0,0 +1 @@ +json.partial! 'answer', answers: @answers diff --git a/app/views/answers/show.json.jbuilder b/app/views/answers/show.json.jbuilder new file mode 100644 index 0000000..a1629d0 --- /dev/null +++ b/app/views/answers/show.json.jbuilder @@ -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 diff --git a/app/views/comments/_comment.json.jbuilder b/app/views/comments/_comment.json.jbuilder new file mode 100644 index 0000000..3ef6a20 --- /dev/null +++ b/app/views/comments/_comment.json.jbuilder @@ -0,0 +1 @@ + json.comments comments, partial: 'comments/default', as: :data diff --git a/app/views/comments/_default.json.jbuilder b/app/views/comments/_default.json.jbuilder new file mode 100644 index 0000000..8b1499d --- /dev/null +++ b/app/views/comments/_default.json.jbuilder @@ -0,0 +1,2 @@ +json.(data, :id, :content, :votes_count, :created_at, :updated_at) +json.partial! 'users/user', user: data.user diff --git a/app/views/comments/index.json.jbuilder b/app/views/comments/index.json.jbuilder new file mode 100644 index 0000000..66d72b2 --- /dev/null +++ b/app/views/comments/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@comments) do |comment| + json.extract! comment, :id + json.url comment_url(comment, format: :json) +end diff --git a/app/views/comments/show.json.jbuilder b/app/views/comments/show.json.jbuilder new file mode 100644 index 0000000..ca935ec --- /dev/null +++ b/app/views/comments/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @comment, :id, :created_at, :updated_at diff --git a/app/views/users/_user.json.jbuilder b/app/views/users/_user.json.jbuilder new file mode 100644 index 0000000..5e2b759 --- /dev/null +++ b/app/views/users/_user.json.jbuilder @@ -0,0 +1 @@ +json.user user diff --git a/app/views/users/index.json.jbuilder b/app/views/users/index.json.jbuilder new file mode 100644 index 0000000..6684a9c --- /dev/null +++ b/app/views/users/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@users) do |user| + json.extract! user, :id + json.url user_url(user, format: :json) +end diff --git a/app/views/users/show.json.jbuilder b/app/views/users/show.json.jbuilder new file mode 100644 index 0000000..80ed63e --- /dev/null +++ b/app/views/users/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @user, :id, :created_at, :updated_at diff --git a/docs/.DS_Store b/docs/.DS_Store deleted file mode 100644 index 143da29c84608ebc5afc9a99a291788ee101b41d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK+e$(~6kSsiQUppAJ)TEj3^nQbAQtoxM1FvILj}#tQF=X}(MRF-GXKXNC(5(Y_swTFt?zNe>zux@*Uv$!%DxeDdD+NrdTB}wV zlD}J5f|I*8V0vT{lX0;_>%z=m$F?9>@i~(<_61TPh8|;wY@z8N0V{)Ms=%)*@Bw(o Bm)`&Y