Module: Sequel::Plugins::BallotVoter::InstanceMethods

Defined in:
lib/sequel/plugins/ballot_voter.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

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

:nodoc:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/sequel/plugins/ballot_voter.rb', line 70

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

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

  vote = find_ballots_by(cond).
    order(Sequel.desc(:updated_at), Sequel.desc(:created_at)).
    limit(1).first
  vote && vote.vote
end

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

:nodoc:



88
89
90
91
92
93
94
95
96
# File 'lib/sequel/plugins/ballot_voter.rb', line 88

def ballots_for_class(klass, kwargs = {}) #:nodoc:
  cond = {
    votable_type: Ballot::Sequel.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:



39
40
41
42
43
44
# File 'lib/sequel/plugins/ballot_voter.rb', line 39

def cast_ballot_for(votable = nil, kwargs = {}) #:nodoc:
  kwargs = __ballot_voter_kwargs(votable, kwargs)
  votable = Ballot::Sequel.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:

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sequel/plugins/ballot_voter.rb', line 54

def cast_ballot_for?(votable = nil, kwargs = {}) #:nodoc:
  kwargs = __ballot_voter_kwargs(votable, kwargs)
  votable_id, votable_type = Ballot::Sequel.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:



47
48
49
50
51
52
# File 'lib/sequel/plugins/ballot_voter.rb', line 47

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