Sha256: 6ce23fe375bedbe3cf55b16874878a571b2d0825b3d483b77081d982dde5cf19

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 KB

Contents

module Parole
  module Comment
    extend ActiveSupport::Concern

    included do
      # Associations
      belongs_to :commentable, polymorphic: true
      belongs_to :commenter, polymorphic: true

      # Callbacks
      after_create :update_cache_counters
      after_destroy :update_cache_counters

      # Validations
      validate :ensure_valid_role_for_commentable, if: lambda { commentable.present? && commentable.commentable? }
      validate :ensure_valid_commentable
      validates :commenter, presence: true
      validates :commentable, presence: true
      validates :comment, presence: true
    end

  protected

    # Update the commentable cache counter columns
    #
    # Look for a `<role>_comments_count` and a `comments_count` column
    # in the commentable model and update their value with the count.
    def update_cache_counters
      role_method = :"#{self.role}_comments_count="
      if commentable.respond_to?(role_method)
        commentable.send role_method, commentable.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

      commentable.save(validate: false)
    end

    # 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.
    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

    # Make sure that the record we're commenting on is an instance
    # of a commentable model.
    def ensure_valid_commentable
      errors.add(:commentable, :invalid) unless commentable.respond_to?(:commentable?) && commentable.commentable?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
parole-0.1.3 lib/parole/comment.rb