Module: Sequel::Plugins::BallotVotable::InstanceMethods

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

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#ballot_by(voter = nil, kwargs = {}) ⇒ Object

:nodoc:



51
52
53
54
55
56
57
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
87
88
89
90
91
92
# File 'lib/sequel/plugins/ballot_votable.rb', line 51

def ballot_by(voter = nil, kwargs = {}) #:nodoc:
  kwargs = { vote: true, scope: nil }.
    merge(__ballot_votable_kwargs(voter, kwargs))
  self.ballot_registered = false

  voter_id, voter_type = Ballot::Sequel.voter_id_and_type_name_for(kwargs)
  return false unless voter_id

  votes_ = find_ballots_for(
    scope: kwargs[:scope],
    voter_id: voter_id,
    voter_type: voter_type
  )

  vote = if votes_.none? || kwargs[:duplicate]
           Ballot::Sequel::Vote.new(
             votable_id: id,
             votable_type: Ballot::Sequel.type_name(model),
             voter_id: voter_id,
             voter_type: voter_type,
             scope: kwargs[:scope]
           )
         else
           votes_.last
         end

  flag = Ballot::Words.truthy?(kwargs[:vote])
  weight = kwargs[:weight] && kwargs[:weight].to_i || 1

  return false if vote.vote == flag && vote.weight == weight

  vote.vote = flag
  vote.weight = weight

  model.db.transaction do
    if vote.save
      self.ballot_registered = true
      update_cached_votes kwargs[:scope]
      true
    end
  end
end

#ballot_by?(voter = nil, kwargs = {}) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/sequel/plugins/ballot_votable.rb', line 115

def ballot_by?(voter = nil, kwargs = {}) #:nodoc:
  kwargs = __ballot_votable_kwargs(voter, kwargs)
  voter_id, voter_type = Ballot::Sequel.voter_id_and_type_name_for(kwargs)
  return false unless voter_id

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

  find_ballots_for(cond).any?
end

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

:nodoc:



130
131
132
133
134
135
136
137
138
# File 'lib/sequel/plugins/ballot_votable.rb', line 130

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

  find_ballots_for(cond)
end

#remove_ballot_by(voter = nil, kwargs = {}) ⇒ Object

:nodoc:



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/sequel/plugins/ballot_votable.rb', line 94

def remove_ballot_by(voter = nil, kwargs = {}) # :nodoc:
  kwargs = __ballot_votable_kwargs(voter, kwargs)
  voter_id, voter_type = Ballot::Sequel.voter_id_and_type_name_for(kwargs)
  return false unless voter_id

  votes_ = find_ballots_for(
    scope: kwargs[:scope],
    voter_id: voter_id,
    voter_type: voter_type
  )

  return true if votes_.none?

  model.db.transaction do
    votes_.each(&:destroy)
    update_cached_votes kwargs[:scope]
    self.ballot_registered = ballots_for_dataset.any?
    true
  end
end