Module: Sequel::Plugins::BallotVoter

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

Overview

The BallotVoter plugin marks the model as containing objects that can vote. It creates a polymorphic one-to-many relationship from the BallotVoter model to Ballot::Sequel::Vote.

Defined Under Namespace

Modules: InstanceMethods

Class Method Summary collapse

Class Method Details

.apply(model) ⇒ Object

:nodoc:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sequel/plugins/ballot_voter.rb', line 9

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

  model.instance_eval do
    # Create a polymorphic one-to-many relationship for voters. Based
    # heavily on the one_to_many implementation from sequel_polymorphic,
    # but customized to sequel-voting's needs.
    one_to_many :ballots_by,
      key: :voter_id,
      reciprocal: :voter,
      reciprocal_type: :one_to_many,
      conditions: { voter_type: Ballot::Sequel.type_name(model) },
      adder: ->(many_of_instance) {
        many_of_instance.update(
          voter_id: pk,
          voter_type: Ballot::Sequel.type_name(model)
        )
      },
      remover: ->(many_of_instance) { many_of_instance.delete },
      clearer: -> { ballots_by_dataset.delete },
      class: '::Ballot::Sequel::Vote'

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