Skip to content

Commit

Permalink
Merge comment services into one
Browse files Browse the repository at this point in the history
  • Loading branch information
KentShikama committed Aug 9, 2015
1 parent 295585f commit 7666167
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 61 deletions.
11 changes: 5 additions & 6 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@ class CommentsController < ApplicationController
end

def create
@comment = CommentCreationService
.new(post_id: params[:post_id], text: params[:text], user: current_user).create_comment
@comment = CommentService.new(post_id: params[:post_id], text: params[:text], user: current_user).create_comment
if @comment
respond_create_success
else
render nothing: true, status: 422
render nothing: true, status: 404
end
end

def destroy
deletion_service = CommentDeletionService.new(comment_id: params[:id], user: current_user)
if deletion_service.destroy_comment
service = CommentService.new(comment_id: params[:id], user: current_user)
if service.destroy_comment
respond_destroy_success
else
respond_destroy_error
Expand All @@ -37,7 +36,7 @@ def new
end

def index
service = CommentIndexService.new(post_id: params[:post_id], user: current_user)
service = CommentService.new(post_id: params[:post_id], user: current_user)
@post = service.post
@comments = service.comments
respond_with do |format|
Expand Down
12 changes: 0 additions & 12 deletions app/services/comment_creation_service.rb

This file was deleted.

16 changes: 0 additions & 16 deletions app/services/comment_deletion_service.rb

This file was deleted.

26 changes: 0 additions & 26 deletions app/services/comment_index_service.rb

This file was deleted.

43 changes: 43 additions & 0 deletions app/services/comment_service.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion spec/controllers/comments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@

expect(alice).not_to receive(:comment)
post :create, comment_hash
expect(response.code).to eq('422')
expect(response.code).to eq("404")
end
end

Expand Down

0 comments on commit 7666167

Please sign in to comment.