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

View file

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

View file

@ -1,3 +1,3 @@
.comment-container
%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") }
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
it 'returns a 404 on a nonexistent post' do