Skip to content

Commit

Permalink
Implement jbuilder for comments controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
oojewale authored and Osmond Oscar committed Feb 27, 2016
1 parent f26ca43 commit 161bea2
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 9 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added app/.DS_Store
Binary file not shown.
6 changes: 2 additions & 4 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,24 @@ class CommentsController < ApplicationController
include OwnershipConcern

def index
render json: @resource_comments, status: 200
end

def show
render json: @comment, status: 200
end

def create
comment = @resource_comments.new(content: comment_params[:content])
comment.user = current_user
if comment.save
render json: comment, status: 201
render :show
else
invalid_request("Comment body can not be empty!")
end
end

def update
if @comment.update(content: comment_params[:content])
render json: @comment, status: 200
render :show
else
invalid_request("Comment body can not be empty!")
end
Expand Down
26 changes: 26 additions & 0 deletions app/controllers/concerns/common.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Common
def set_vars(allowed = {})
@question_id = allowed[:question_id]
@answer_id = allowed[:answer_id]
@comment_id = allowed[:comment_id]
@id = allowed[:id]
@content = allowed[:content]
@user_id = current_user.id if current_user
end

def action_on_question
question_id && answer_id.nil?
end

def action_on_answer
answer_id && question_id.nil?
end

def action_on_comment
comment_id.present?
end

def error_response(message = false, status = 403)
render json: { error: message }, status: status
end
end
2 changes: 1 addition & 1 deletion app/views/comments/_comment.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1 +1 @@
json.comments comments, partial: 'comments/default', as: :data
json.comments @comment, partial: 'comments/default', as: :data
7 changes: 4 additions & 3 deletions app/views/comments/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
json.array!(@comments) do |comment|
json.extract! comment, :id
json.url comment_url(comment, format: :json)
json.array!(@resource_comments) do |comment|
json.partial! 'comments/default', data: comment
json.extract! comment, :comment_on_id, :comment_on_type
json.partial! 'users/user', user: comment.user
end
4 changes: 3 additions & 1 deletion app/views/comments/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
json.extract! @comment, :id, :created_at, :updated_at
json.partial! 'comments/default', data: @comment
json.extract! @comment, :comment_on_id, :comment_on_type
json.partial! 'users/user', user: @comment.user

0 comments on commit 161bea2

Please sign in to comment.