refactoring CommentService

This commit is contained in:
Benjamin Neff 2016-03-06 03:20:37 +01:00
parent b67b7cf8c6
commit b398b115bc
4 changed files with 38 additions and 38 deletions

View file

@ -12,17 +12,16 @@ class CommentsController < ApplicationController
end end
def create def create
@comment = CommentService.new(post_id: params[:post_id], text: params[:text], user: current_user).create_comment comment = comment_service.create(params[:post_id], params[:text])
if @comment if comment
respond_create_success respond_create_success(comment)
else else
render nothing: true, status: 404 render nothing: true, status: 404
end end
end end
def destroy def destroy
service = CommentService.new(comment_id: params[:id], user: current_user) if comment_service.destroy(params[:id])
if service.destroy_comment
respond_destroy_success respond_destroy_success
else else
respond_destroy_error respond_destroy_error
@ -36,22 +35,24 @@ class CommentsController < ApplicationController
end end
def index def index
service = CommentService.new(post_id: params[:post_id], user: current_user) comments = comment_service.find_for_post(params[:post_id])
@post = service.post
@comments = service.comments
respond_with do |format| respond_with do |format|
format.json { render json: CommentPresenter.as_collection(@comments), status: 200 } format.json { render json: CommentPresenter.as_collection(comments), status: 200 }
format.mobile { render layout: false } format.mobile { render layout: false, locals: {comments: comments} }
end end
end end
private private
def respond_create_success def comment_service
@comment_service ||= CommentService.new(current_user)
end
def respond_create_success(comment)
respond_to do |format| respond_to do |format|
format.json { render json: CommentPresenter.new(@comment), status: 201 } format.json { render json: CommentPresenter.new(comment), status: 201 }
format.html { render nothing: true, status: 201 } format.html { render nothing: true, status: 201 }
format.mobile { render partial: "comment", locals: {post: @comment.post, comment: @comment} } format.mobile { render partial: "comment", locals: {comment: comment} }
end end
end end

View file

@ -1,43 +1,42 @@
class CommentService class CommentService
attr_reader :post, :comments def initialize(user=nil)
@user = user
def initialize(params)
@user = params[:user]
@post_id = params[:post_id]
@comment_id = params[:comment_id]
@text = params[:text]
@post = find_post! if @post_id
@comments = @post.comments.for_a_stream if @post
end end
def create_comment def create(post_id, text)
@user.comment!(post, @text) if @post post = find_post!(post_id)
user.comment!(post, text)
end end
def destroy_comment def destroy(comment_id)
@comment = Comment.find(@comment_id) comment = Comment.find(comment_id)
if @user.owns?(@comment) || @user.owns?(@comment.parent) if user.owns?(comment) || user.owns?(comment.parent)
@user.retract(@comment) user.retract(comment)
true true
else else
false false
end end
end end
def find_for_post(post_id)
find_post!(post_id).comments.for_a_stream
end
private private
def find_post! attr_reader :user
find_post.tap do |post|
raise(ActiveRecord::RecordNotFound) unless post def find_post!(post_id)
find_post(post_id).tap do |post|
raise ActiveRecord::RecordNotFound unless post
end end
end end
def find_post def find_post(post_id)
if @user if user
@user.find_visible_shareable_by_id(Post, @post_id) user.find_visible_shareable_by_id(Post, post_id)
else else
Post.find_by_id_and_public(@post_id, true) Post.find_by_id_and_public(post_id, true)
end end
end end
end end

View file

@ -1,3 +1,3 @@
.comment-container .comment-container
%ul.comments %ul.comments
= render partial: "comments/comment", collection: @post.comments.for_a_stream, locals: {post: @post} = render partial: "comments/comment", collection: comments

View file

@ -140,7 +140,7 @@ describe CommentsController, :type => :controller do
comments = [alice, bob, eve].map{ |u| u.comment!(@message, "hey") } comments = [alice, bob, eve].map{ |u| u.comment!(@message, "hey") }
get :index, :post_id => @message.id, :format => :json get :index, :post_id => @message.id, :format => :json
expect(assigns[:comments].map(&:id)).to match_array(comments.map(&:id)) expect(JSON.parse(response.body).map {|comment| comment["id"] }).to match_array(comments.map(&:id))
end end
it 'returns a 404 on a nonexistent post' do it 'returns a 404 on a nonexistent post' do