Skip to content

Commit

Permalink
Extract services from comments controller
Browse files Browse the repository at this point in the history
  • Loading branch information
KentShikama committed Aug 9, 2015
1 parent 480384a commit bc26b46
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 46 deletions.
78 changes: 39 additions & 39 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,73 +3,73 @@
# 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 = CommentCreationService
.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: 422
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
deletion_service = CommentDeletionService.new(comment_id: params[:id], user: current_user)
if deletion_service.allow_destroy?
deletion_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 = CommentIndexService.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
12 changes: 12 additions & 0 deletions app/services/comment_creation_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CommentCreationService
def initialize(params)
@user = params[:user]
@post_id = params[:post_id]
@text = params[:text]
end

def create_comment!
post = @user.find_visible_shareable_by_id(Post, @post_id)
@user.comment!(post, @text) if post
end
end
15 changes: 15 additions & 0 deletions app/services/comment_deletion_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class CommentDeletionService
def initialize(params)
@user = params[:user]
comment_id = params[:comment_id]
@comment = Comment.find(comment_id)
end

def allow_destroy?
@user.owns?(@comment) || @user.owns?(@comment.parent)
end

def destroy_comment
@user.retract(@comment)
end
end
26 changes: 26 additions & 0 deletions app/services/comment_index_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class CommentIndexService
attr_reader :post, :comments

def initialize(params)
@user = params[:user]
@post_id = params[:post_id]
@post = find_post!
@comments = @post.comments.for_a_stream
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
11 changes: 4 additions & 7 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,13 @@

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


Expand Down

0 comments on commit bc26b46

Please sign in to comment.