Module: Ballot::ActiveRecord::Voter

Defined in:
lib/ballot/active_record/voter.rb

Overview

The Ballot::ActiveRecord::Voter module is the ActiveRecord-specific implementation to enable Ballot::Voter for ActiveRecord. for full details.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(model) ⇒ Object

:nodoc:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ballot/active_record/voter.rb', line 10

def self.included(model) # :nodoc:
  require 'ballot/active_record/vote'
  require 'ballot/words'
  require 'ballot/voter'

  model.class_eval do
    has_many :ballots_by,
      class_name: '::Ballot::ActiveRecord::Vote',
      as: :voter,
      dependent: :destroy

    include Ballot::Voter
    extend Ballot::Voter::ClassMethods
  end
end

Instance Method Details

#ballot_as_cast_for(votable = nil, kwargs = {}) ⇒ Object

:nodoc:



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ballot/active_record/voter.rb', line 58

def ballot_as_cast_for(votable = nil, kwargs = {}) # :nodoc:
  kwargs = __ballot_voter_kwargs(votable, kwargs)
  votable = Ballot::ActiveRecord.votable_for(kwargs)
  return nil unless votable

  cond = {
    votable_id: votable.id,
    votable_type: Ballot::ActiveRecord.type_name(votable),
    scope: kwargs[:scope]
  }
  cond[:vote] = Ballot::Words.truthy?(kwargs[:vote]) if kwargs.key?(:vote)

  vote = find_ballots_by(cond).last
  vote && vote.vote
end

#ballots_for_class(klass, kwargs = {}) ⇒ Object

:nodoc:



74
75
76
77
78
79
80
81
82
# File 'lib/ballot/active_record/voter.rb', line 74

def ballots_for_class(klass, kwargs = {}) #:nodoc:
  cond = {
    votable_type: Ballot::ActiveRecord.type_name(klass),
    scope: kwargs[:scope]
  }
  cond[:vote] = Ballot::Words.truthy?(kwargs[:vote]) if kwargs.key?(:vote)

  find_ballots_by(cond)
end

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

:nodoc:



26
27
28
29
30
31
# File 'lib/ballot/active_record/voter.rb', line 26

def cast_ballot_for(votable = nil, kwargs = {}) #:nodoc:
  kwargs = __ballot_voter_kwargs(votable, kwargs)
  votable = Ballot::ActiveRecord.votable_for(kwargs)
  return false unless votable
  votable.ballot_by(kwargs.merge(voter: self))
end

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

:nodoc:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ballot/active_record/voter.rb', line 41

def cast_ballot_for?(votable = nil, kwargs = {}) #:nodoc:
  kwargs = __ballot_voter_kwargs(votable, kwargs)
  votable_id, votable_type =
    Ballot::ActiveRecord.votable_id_and_type_name_for(kwargs)
  return false unless votable_id

  cond = {
    votable_id: votable_id,
    votable_type: votable_type,
    scope: kwargs[:scope]
  }
  cond[:vote] = Ballot::Words.truthy?(kwargs[:vote]) if kwargs.key?(:vote)

  find_ballots_by(cond).any?
end

#remove_ballot_for(votable = nil, kwargs = {}) ⇒ Object

:nodoc:



34
35
36
37
38
39
# File 'lib/ballot/active_record/voter.rb', line 34

def remove_ballot_for(votable = nil, kwargs = {}) #:nodoc:
  kwargs = __ballot_voter_kwargs(votable, kwargs)
  votable = Ballot::ActiveRecord.votable_for(kwargs)
  return false unless votable
  votable.remove_ballot_by voter: self, scope: kwargs[:scope]
end