module Voting::Extension::ClassMethods

Public Instance Methods

voting(as: nil, scoping: nil) click to toggle source
# File lib/voting/models/voting/extension.rb, line 58
def voting(as: nil, scoping: nil)
  after_create -> { voting_warm_up scoping: scoping }, unless: -> { as == :author }

  has_many :voting_records,
           as: :resource,
           class_name: '::Voting::Voting',
           dependent: :destroy

  has_many :votes_records,
           as: :resource,
           class_name: '::Voting::Vote',
           dependent: :destroy

  has_many :voted_records,
           as: :author,
           class_name: '::Voting::Vote',
           dependent: :destroy

  scope :order_by_voting, lambda { |column = :estimate, direction = :desc, scope: nil|
    scope_values = {
      scopeable_id: scope&.id,
      scopeable_type: scope&.class&.base_class&.name
    }

    includes(:voting_records)
      .where(Voting.table_name => scope_values)
      .order("#{Voting.table_name}.#{column} #{direction}")
  }
end