diff --git a/Changelog.md b/Changelog.md index 4a581367fb0b54be8ceba55f3159029229c8007a..d11bf369db36aed05e18549f5181c740ce52dae3 100644 --- a/Changelog.md +++ b/Changelog.md @@ -72,6 +72,7 @@ With the port to Bootstrap 3, app/views/terms/default.haml has a new structure. * Refactor ApplicationController#after\_sign\_out\_path\_for [#6258](https://github.com/diaspora/diaspora/pull/6258) * Extract StatusMessageService from StatusMessagesController [#6280](https://github.com/diaspora/diaspora/pull/6280) * Refactor HomeController#toggle\_mobile [#6260](https://github.com/diaspora/diaspora/pull/6260) +* Extract CommentService from CommentsController [#6307](https://github.com/diaspora/diaspora/pull/6307) ## Bug fixes * Fix indentation and a link title on the default home page [#6212](https://github.com/diaspora/diaspora/pull/6212) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 46927dc96f5ef5c764a2c75e7201ddabd9b3e64a..b8bdb3a4c44d2b10202125428272a15deef8f1cb 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -3,73 +3,71 @@ # the COPYRIGHT file. class CommentsController < ApplicationController - include ApplicationHelper - before_action :authenticate_user!, :except => [:index] + before_action :authenticate_user!, except: :index - respond_to :html, - :mobile, - :json + respond_to :html, :mobile, :json rescue_from ActiveRecord::RecordNotFound do - render :nothing => true, :status => 404 + render nothing: true, status: 404 end def create - post = current_user.find_visible_shareable_by_id(Post, params[:post_id]) - @comment = current_user.comment!(post, params[:text]) if post - + @comment = CommentService.new(post_id: params[:post_id], text: params[:text], user: current_user).create_comment if @comment - respond_to do |format| - 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} } - end + respond_create_success else - render :nothing => true, :status => 422 + render nothing: true, status: 404 end end def destroy - @comment = Comment.find(params[:id]) - if current_user.owns?(@comment) || current_user.owns?(@comment.parent) - current_user.retract(@comment) - respond_to do |format| - format.js { render :nothing => true, :status => 204 } - format.json { render :nothing => true, :status => 204 } - format.mobile{ redirect_to :back } - end + service = CommentService.new(comment_id: params[:id], user: current_user) + if service.destroy_comment + respond_destroy_success else - respond_to do |format| - format.mobile { redirect_to :back } - format.any(:js, :json) {render :nothing => true, :status => 403} - end + respond_destroy_error end end def new respond_to do |format| - format.mobile { render :layout => false } + format.mobile { render layout: false } end end def index - find_post - raise(ActiveRecord::RecordNotFound.new) unless @post - - @comments = @post.comments.for_a_stream + service = CommentService.new(post_id: params[:post_id], user: current_user) + @post = service.post + @comments = service.comments 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 } end end private - def find_post - if user_signed_in? - @post = current_user.find_visible_shareable_by_id(Post, params[:post_id]) - else - @post = Post.find_by_id_and_public(params[:post_id], true) + def respond_create_success + respond_to do |format| + 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} } + end + end + + def respond_destroy_success + respond_to do |format| + format.mobile { redirect_to :back } + format.js { render nothing: true, status: 204 } + format.json { render nothing: true, status: 204 } + end + end + + def respond_destroy_error + respond_to do |format| + format.mobile { redirect_to :back } + format.js { render nothing: true, status: 403 } + format.json { render nothing: true, status: 403 } end end end diff --git a/app/services/comment_service.rb b/app/services/comment_service.rb new file mode 100644 index 0000000000000000000000000000000000000000..a571db3757f9e54fa5fe3c7e68866e955efdf2ee --- /dev/null +++ b/app/services/comment_service.rb @@ -0,0 +1,43 @@ +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 + end + + def create_comment + @user.comment!(post, @text) if @post + end + + def destroy_comment + @comment = Comment.find(@comment_id) + if @user.owns?(@comment) || @user.owns?(@comment.parent) + @user.retract(@comment) + true + else + false + end + end + + private + + def find_post! + find_post.tap do |post| + raise(ActiveRecord::RecordNotFound) unless post + end + end + + def find_post + if @user + @user.find_visible_shareable_by_id(Post, @post_id) + else + Post.find_by_id_and_public(@post_id, true) + end + end +end diff --git a/config/routes.rb b/config/routes.rb index ebab9fe56f469b14810ae750337c0e45a5482804..a1bc5b14a9de06a0d27478ebdf2ffae589bf5f1c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -30,16 +30,13 @@ Diaspora::Application.routes.draw do resources :posts do member do - get :next - get :previous get :interactions end - resources :poll_participations, :only => [:create] - - resources :likes, :only => [:create, :destroy, :index ] - resource :participation, :only => [:create, :destroy] - resources :comments, :only => [:new, :create, :destroy, :index] + resource :participation, only: %i(create destroy) + resources :poll_participations, only: :create + resources :likes, only: %i(create destroy index) + resources :comments, only: %i(new create destroy index) end diff --git a/spec/controllers/comments_controller_spec.rb b/spec/controllers/comments_controller_spec.rb index f3e2bf1859fdc956bade40c8e10ee1e7e2c1637a..adb2ec1ec16d3e6295c27a0ce32048856ccc8fea 100644 --- a/spec/controllers/comments_controller_spec.rb +++ b/spec/controllers/comments_controller_spec.rb @@ -66,7 +66,7 @@ describe CommentsController, :type => :controller do expect(alice).not_to receive(:comment) post :create, comment_hash - expect(response.code).to eq('422') + expect(response.code).to eq("404") end end