From b398b115bcc98ce97bd05fc64b7477e13f65b109 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Sun, 6 Mar 2016 03:20:37 +0100 Subject: [PATCH] refactoring CommentService --- app/controllers/comments_controller.rb | 27 ++++++------ app/services/comment_service.rb | 45 ++++++++++---------- app/views/comments/index.mobile.haml | 2 +- spec/controllers/comments_controller_spec.rb | 2 +- 4 files changed, 38 insertions(+), 38 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index b8bdb3a4c..9abc7503b 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -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 diff --git a/app/services/comment_service.rb b/app/services/comment_service.rb index a571db375..b417933a9 100644 --- a/app/services/comment_service.rb +++ b/app/services/comment_service.rb @@ -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 diff --git a/app/views/comments/index.mobile.haml b/app/views/comments/index.mobile.haml index 6f0d545e2..bbb26073a 100644 --- a/app/views/comments/index.mobile.haml +++ b/app/views/comments/index.mobile.haml @@ -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 diff --git a/spec/controllers/comments_controller_spec.rb b/spec/controllers/comments_controller_spec.rb index adb2ec1ec..bd9bb899f 100644 --- a/spec/controllers/comments_controller_spec.rb +++ b/spec/controllers/comments_controller_spec.rb @@ -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