module Rating::Extension::ClassMethods

Public Instance Methods

rating(options = {}) click to toggle source
# File lib/rating/models/rating/extension.rb, line 58
def rating(options = {})
  after_create -> { rating_warm_up scoping: options[:scoping] }, unless: -> { options[:as] == :author }

  has_many :rating_records,
    as:         :resource,
    class_name: '::Rating::Rating',
    dependent:  :destroy

  has_many :rates_records,
    as:         :resource,
    class_name: '::Rating::Rate',
    dependent:  :destroy

  has_many :rated_records,
    as:         :author,
    class_name: '::Rating::Rate',
    dependent:  :destroy

  scope :order_by_rating, lambda { |column = :estimate, direction = :desc, scope: nil|
    includes(:rating_records)
      .where(Rating.table_name => { scopeable_id: scope&.id, scopeable_type: scope&.class&.base_class&.name })
      .order("#{Rating.table_name}.#{column} #{direction}")
  }

  define_method :rating_options do
    options
  end
end