Extract service from comments controller

closes #6307
This commit is contained in:
theworldbright 2015-08-09 17:08:33 +09:00 committed by Jonne Haß
parent dc576fb0d6
commit d7c92431ae
5 changed files with 86 additions and 47 deletions

View file

@ -7,6 +7,7 @@
* Refactor ApplicationController#after\_sign\_out\_path\_for [#6258](https://github.com/diaspora/diaspora/pull/6258) * 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) * Extract StatusMessageService from StatusMessagesController [#6280](https://github.com/diaspora/diaspora/pull/6280)
* Refactor HomeController#toggle\_mobile [#6260](https://github.com/diaspora/diaspora/pull/6260) * 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 ## Bug fixes
* Fix indentation and a link title on the default home page [#6212](https://github.com/diaspora/diaspora/pull/6212) * Fix indentation and a link title on the default home page [#6212](https://github.com/diaspora/diaspora/pull/6212)

View file

@ -3,73 +3,71 @@
# the COPYRIGHT file. # the COPYRIGHT file.
class CommentsController < ApplicationController class CommentsController < ApplicationController
include ApplicationHelper before_action :authenticate_user!, except: :index
before_action :authenticate_user!, :except => [:index]
respond_to :html, respond_to :html, :mobile, :json
:mobile,
:json
rescue_from ActiveRecord::RecordNotFound do rescue_from ActiveRecord::RecordNotFound do
render :nothing => true, :status => 404 render nothing: true, status: 404
end end
def create def create
post = current_user.find_visible_shareable_by_id(Post, params[:post_id]) @comment = CommentService.new(post_id: params[:post_id], text: params[:text], user: current_user).create_comment
@comment = current_user.comment!(post, params[:text]) if post
if @comment if @comment
respond_to do |format| respond_create_success
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
else else
render :nothing => true, :status => 422 render nothing: true, status: 404
end end
end end
def destroy def destroy
@comment = Comment.find(params[:id]) service = CommentService.new(comment_id: params[:id], user: current_user)
if current_user.owns?(@comment) || current_user.owns?(@comment.parent) if service.destroy_comment
current_user.retract(@comment) respond_destroy_success
respond_to do |format|
format.js { render :nothing => true, :status => 204 }
format.json { render :nothing => true, :status => 204 }
format.mobile{ redirect_to :back }
end
else else
respond_to do |format| respond_destroy_error
format.mobile { redirect_to :back }
format.any(:js, :json) {render :nothing => true, :status => 403}
end
end end
end end
def new def new
respond_to do |format| respond_to do |format|
format.mobile { render :layout => false } format.mobile { render layout: false }
end end
end end
def index def index
find_post service = CommentService.new(post_id: params[:post_id], user: current_user)
raise(ActiveRecord::RecordNotFound.new) unless @post @post = service.post
@comments = service.comments
@comments = @post.comments.for_a_stream
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 }
end end
end end
private private
def find_post def respond_create_success
if user_signed_in? respond_to do |format|
@post = current_user.find_visible_shareable_by_id(Post, params[:post_id]) format.json { render json: CommentPresenter.new(@comment), status: 201 }
else format.html { render nothing: true, status: 201 }
@post = Post.find_by_id_and_public(params[:post_id], true) 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 end
end end

View file

@ -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

View file

@ -30,16 +30,13 @@ Diaspora::Application.routes.draw do
resources :posts do resources :posts do
member do member do
get :next
get :previous
get :interactions get :interactions
end end
resources :poll_participations, :only => [:create] resource :participation, only: %i(create destroy)
resources :poll_participations, only: :create
resources :likes, :only => [:create, :destroy, :index ] resources :likes, only: %i(create destroy index)
resource :participation, :only => [:create, :destroy] resources :comments, only: %i(new create destroy index)
resources :comments, :only => [:new, :create, :destroy, :index]
end end

View file

@ -66,7 +66,7 @@ describe CommentsController, :type => :controller do
expect(alice).not_to receive(:comment) expect(alice).not_to receive(:comment)
post :create, comment_hash post :create, comment_hash
expect(response.code).to eq('422') expect(response.code).to eq("404")
end end
end end