Sha256: 5e8021be902638f9870565611f8ec0476eb018105a41a336580061a188662758

Contents?: true

Size: 1 KB

Versions: 2

Compression:

Stored size: 1 KB

Contents

module CommentableOn
  module Commentable
    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

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

    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

    def root_comments
      comments.roots
    end

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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
commentable_on-1.1.0 lib/commentable_on/commentable.rb
commentable_on-1.0.0 lib/commentable_on/commentable.rb