diff options
author | theworldbright <kent@kentshikama.com> | 2015-08-09 11:08:33 +0300 |
---|---|---|
committer | Jonne Haß <me@jhass.eu> | 2015-08-09 19:35:11 +0300 |
commit | d7c92431aea25c56356b9678690798350ff7a38b (patch) | |
tree | 364b0815288787df5f85f95b7fde7118d06d8e09 /app/services | |
parent | dc576fb0d615db3d2a4626ff3c85bcd4483d910a (diff) |
Extract service from comments controller
closes #6307
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/comment_service.rb | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/app/services/comment_service.rb b/app/services/comment_service.rb new file mode 100644 index 000000000..a571db375 --- /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 |