Module: Ballot::Voter

Defined in:
lib/ballot/voter.rb

Overview

Methods added to a model that is marked as a Voter.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#ballot_down_votables(*conds, &block) ⇒ Object

Returns the Votable objects that this Voter has made negative votes on. See #ballot_voters for how conds and block apply.



294
295
296
297
298
# File 'lib/ballot/voter.rb', line 294

def ballot_down_votables(*conds, &block)
  __eager_ballot_votables(
    find_ballots_by(*conds, &block).where(vote: false)
  )
end

#ballot_up_votables(*conds, &block) ⇒ Object

Returns the Votable objects that this Voter has made positive votes on. See #ballot_voters for how conds and block apply.



285
286
287
288
289
# File 'lib/ballot/voter.rb', line 285

def ballot_up_votables(*conds, &block)
  __eager_ballot_votables(
    find_ballots_by(*conds, &block).where(vote: true)
  )
end

#ballot_votables(*conds, &block) ⇒ Object

Returns the Votable objects that this Voter has voted on. Additional query conditions may be specified in conds, or in the block if supported by the ORM. The Voter objects are eager loaded to minimize the number of queries required to satisfy this request.

ActiveRecord

Polymorphic eager loading is directly supported, using ballots_for.includes(:votable). Normal where-clause conditions may be provided in conds.

Sequel

Polymorphic eager loading is not supported by Sequel, but has been implemented in Ballot for this method. Normal where-clause conditions may be provided in conds or in block for Sequel virtual row support.



278
279
280
# File 'lib/ballot/voter.rb', line 278

def ballot_votables(*conds, &block)
  __eager_ballot_votables(find_ballots_by(*conds, &block))
end

#cast_down_ballot_for(votable = nil, kwargs = {}) ⇒ Object Also known as: down_ballot_for

Records a negative vote by this Voter on the provided votable with options provided in kwargs. Any value passed to the vote keyword argument will be ignored. See #cast_ballot_for for more details.



68
69
70
# File 'lib/ballot/voter.rb', line 68

def cast_down_ballot_for(votable = nil, kwargs = {})
  cast_ballot_for(votable, kwargs.merge(vote: false))
end

#cast_down_ballot_for?(votable = nil, kwargs = {}) ⇒ Boolean Also known as: down_ballot_for?

Returns true if this Voter has made negative votes for the provided votable. Any value passed to the vote keyword argument will be ignored. See #cast_ballot_for? for more details.

Returns:

  • (Boolean)


199
200
201
# File 'lib/ballot/voter.rb', line 199

def cast_down_ballot_for?(votable = nil, kwargs = {})
  cast_ballot_for?(votable, kwargs.merge(vote: false))
end

#cast_up_ballot_for(votable = nil, kwargs = {}) ⇒ Object Also known as: up_ballot_for

Records a positive vote by this Voter on the provided votable with options provided in kwargs. Any value passed to the vote keyword argument will be ignored. See #cast_ballot_for for more details.



59
60
61
# File 'lib/ballot/voter.rb', line 59

def cast_up_ballot_for(votable = nil, kwargs = {})
  cast_ballot_for(votable, kwargs.merge(vote: true))
end

#cast_up_ballot_for?(votable = nil, kwargs = {}) ⇒ Boolean Also known as: up_ballot_for?

Returns true if this Voter has made positive votes for the provided votable. Any value passed to the vote keyword argument will be ignored. See #cast_ballot_for? for more details.

Returns:

  • (Boolean)


190
191
192
# File 'lib/ballot/voter.rb', line 190

def cast_up_ballot_for?(votable = nil, kwargs = {})
  cast_ballot_for?(votable, kwargs.merge(vote: true))
end

#down_ballots_by(kwargs = {}) ⇒ Object

Returns ballots by this Voter where the recorded vote is negative.

ActiveRecord

There are no special notes for ActiveRecord.

Sequel

This method returns the dataset; if vote objects are desired, use down_ballots_by.all.



138
139
140
# File 'lib/ballot/voter.rb', line 138

def down_ballots_by(kwargs = {})
  find_ballots_by(vote: false, scope: kwargs[:scope])
end

#down_ballots_for_class(model_class, kwargs = {}) ⇒ Object

Find negative ballots cast by this Voter matching the canonical name of the model_class as the type of Votable. Any value passed to the vote keyword argument will be ignored. See #ballots_for_class for more details.



261
262
263
# File 'lib/ballot/voter.rb', line 261

def down_ballots_for_class(model_class, kwargs = {})
  ballots_for_class(model_class, kwargs.merge(vote: false))
end

#up_ballots_by(kwargs = {}) ⇒ Object

Returns ballots by this Voter where the recorded vote is positive.

ActiveRecord

There are no special notes for ActiveRecord.

Sequel

This method returns the dataset; if vote objects are desired, use up_ballots_by.all.



128
129
130
# File 'lib/ballot/voter.rb', line 128

def up_ballots_by(kwargs = {})
  find_ballots_by(vote: true, scope: kwargs[:scope])
end

#up_ballots_for_class(model_class, kwargs = {}) ⇒ Object

Find positive ballots cast by this Voter matching the canonical name of the model_class as the type of Votable. Any value passed to the vote keyword argument will be ignored. See #ballots_for_class for more details.



252
253
254
# File 'lib/ballot/voter.rb', line 252

def up_ballots_for_class(model_class, kwargs = {})
  ballots_for_class(model_class, kwargs.merge(vote: true))
end