Class: CommentService

Inherits:
Object
  • Object
show all
Defined in:
app/services/comment_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(user = nil) ⇒ CommentService

Returns a new instance of CommentService.


4
5
6
# File 'app/services/comment_service.rb', line 4

def initialize(user=nil)
  @user = user
end

Instance Method Details

#create(post_id, text) ⇒ Object


8
9
10
11
# File 'app/services/comment_service.rb', line 8

def create(post_id, text)
  post = post_service.find!(post_id)
  user.comment!(post, text)
end

#destroy(comment_id) ⇒ Object


21
22
23
24
25
26
27
28
29
# File 'app/services/comment_service.rb', line 21

def destroy(comment_id)
  comment = Comment.find(comment_id)
  if user.owns?(comment) || user.owns?(comment.parent)
    user.retract(comment)
    true
  else
    false
  end
end

#destroy!(comment_guid) ⇒ Object


31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/services/comment_service.rb', line 31

def destroy!(comment_guid)
  comment = find!(comment_guid)
  if user.owns?(comment)
    user.retract(comment)
  elsif user.owns?(comment.parent)
    user.retract(comment)
  elsif comment
    raise ActiveRecord::RecordInvalid
  else
    raise ActiveRecord::RecordNotFound
  end
end

#find!(comment_guid) ⇒ Object


17
18
19
# File 'app/services/comment_service.rb', line 17

def find!(comment_guid)
  Comment.find_by!(guid: comment_guid)
end

#find_for_post(post_id) ⇒ Object


13
14
15
# File 'app/services/comment_service.rb', line 13

def find_for_post(post_id)
  post_service.find!(post_id).comments.for_a_stream
end