Module: CommentableOn::Commentable

Defined in:
lib/commentable_on/commentable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/commentable_on/commentable.rb', line 3

def self.included(base)
  base.class_eval do
    has_many :comments, class_name: "CommentableOn::Comment", as: :commentable, dependent: :delete_all do
      def commenters
        includes(:commenter).map(&:commenter)
      end
    end
  end
end

Instance Method Details

#add_comment(commenter:, body:) ⇒ Object



13
14
15
16
# File 'lib/commentable_on/commentable.rb', line 13

def add_comment(commenter:, body:)
  comment = CommentableOn::Comment.new(commentable: self, commenter: commenter, body: body)
  comment.save
end

#create_reply(comment:, commenter:, body:) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/commentable_on/commentable.rb', line 18

def create_reply(comment:, commenter:, body:)
  unless comment.instance_of?(CommentableOn::Comment)
    return CommentableOn::Comment.create(commentable: self, commenter: commenter, body: body, parent_id: comment)
  end

  comment.children.create(commentable: self, commenter: commenter, body: body)
end

#replies_for(comment) ⇒ Object



30
31
32
# File 'lib/commentable_on/commentable.rb', line 30

def replies_for(comment)
  comments.children_of(comment)
end

#root_commentsObject



26
27
28
# File 'lib/commentable_on/commentable.rb', line 26

def root_comments
  comments.roots
end

#thread_for(comment) ⇒ Object



34
35
36
# File 'lib/commentable_on/commentable.rb', line 34

def thread_for(comment)
  comments.descendents_of(comment)
end