module Parole::Comment

Protected Instance Methods

ensure_valid_commentable() click to toggle source

Make sure that the record we’re commenting on is an instance of a commentable model.

# File lib/parole/comment.rb, line 68
def ensure_valid_commentable
  errors.add(:commentable, :invalid) unless commentable.respond_to?(:commentable?) && commentable.commentable?
end
ensure_valid_role_for_commentable() click to toggle source

Make sure that the value of the ‘role` attribute is a valid role for the commentable.

If the commentable doesn’t have any comment roles, we make sure that the value is blank.

# File lib/parole/comment.rb, line 56
def ensure_valid_role_for_commentable
  allowed_roles = commentable.class.commentable_options[:roles]

  if allowed_roles.any?
    errors.add(:role, :invalid) unless allowed_roles.include?(self.role)
  else
    errors.add(:role, :invalid) unless self.role.blank?
  end
end
update_cache_counters() click to toggle source

Update the commentable cache counter columns

Look for a ‘<role>_comments_count` and a `comments_count` column in the commentable model and the commenter model and update their value with the count.

# File lib/parole/comment.rb, line 28
def update_cache_counters
  commenter_has_comments = commenter.respond_to?(:comments)

  role_method = :"#{self.role}_comments_count="
  if commentable.respond_to?(role_method)
    commentable.send role_method, commentable.comments.where(role: self.role).count
  end
  if commenter_has_comments && commenter.respond_to?(role_method)
    commenter.send role_method, commenter.comments.where(role: self.role).count
  end

  total_method = :comments_count=
  if commentable.respond_to?(total_method)
    commentable.send total_method, commentable.comments.count
  end
  if commenter_has_comments && commenter.respond_to?(total_method)
    commenter.send total_method, commenter.comments.count
  end

  commentable.save(validate: false)
  commenter.save(validate: false)
end