Module: Voting::Extension::ClassMethods

Defined in:
lib/voting/models/voting/extension.rb

Instance Method Summary collapse

Instance Method Details

#voting(as: nil, scoping: nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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