Commit d7c92431 authored by theworldbright's avatar theworldbright Committed by Jonne Haß

Extract service from comments controller

closes #6307
parent dc576fb0
......@@ -7,6 +7,7 @@
* 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)
......
......@@ -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
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
......@@ -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
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment