Module: Ballot::Sequel

Defined in:
lib/ballot/sequel.rb,
lib/ballot/sequel/vote.rb

Overview

Extensions to Sequel::Model to support Ballot.

Defined Under Namespace

Modules: ClassMethods Classes: Vote

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.type_name(model) ⇒ Object

Respond with the canonical name for this model. This differs if the model is STI-enabled.



67
68
69
70
71
72
73
74
75
# File 'lib/ballot/sequel.rb', line 67

def type_name(model)
  return model if model.kind_of?(String)
  model = model.model if model.kind_of?(::Sequel::Model)
  if model.respond_to?(:sti_dataset)
    model.sti_dataset.model.name
  else
    model.name
  end
end

.votable_for(item) ⇒ Object

Return a valid Votable object from the provided item or nil. Permitted values are a Votable, a hash with the key :votable, or a hash with the keys :votable_type and :votable_id.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ballot/sequel.rb', line 80

def votable_for(item)
  votable =
    if item.kind_of?(::Sequel::Plugins::BallotVotable::InstanceMethods)
      item
    elsif item.kind_of?(Hash)
      if item[:votable]
        item[:votable]
      elsif item[:votable_type] && item[:votable_id]
        __instance_of_model(item[:votable_type], item[:votable_id])
      elsif item[:votable_gid]
        fail 'GlobalID is not enabled.' unless defined?(::GlobalID)

        # NOTE: Until GlobalID has patches or a plug-in to work with
        # Sequel, this is more likely to fail than to succeed.
        GlobalID::Locator.locate(item[:votable_gid])
      end
    end

  votable if votable && votable.kind_of?(::Sequel::Model) &&
      votable.ballot_votable?
end

.votable_id_and_type_name_for(item) ⇒ Object

Return the id and canonical votable type name for the item, using #votable_for.



104
105
106
# File 'lib/ballot/sequel.rb', line 104

def votable_id_and_type_name_for(item)
  __id_and_type_name(votable_for(item))
end

.voter_for(item) ⇒ Object

Return a valid Voter object from the provided item or nil. Permitted values are a Voter, a hash with the key :voter, or a hash with the keys :voter_type and :voter_id.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ballot/sequel.rb', line 111

def voter_for(item)
  voter =
    if item.kind_of?(::Sequel::Plugins::BallotVoter::InstanceMethods)
      item
    elsif item.kind_of?(Hash)
      if item[:voter]
        item[:voter]
      elsif item[:voter_type] && item[:voter_id]
        __instance_of_model(item[:voter_type], item[:voter_id])
      elsif item[:voter_gid]
        fail 'GlobalID is not enabled.' unless defined?(::GlobalID)

        # This should actually be GlobalID::Sequel::Locator when I
        # get that ported.
        GlobalID::Locator.locate(item[:voter_gid])
      end
    end

  voter if voter && voter.kind_of?(::Sequel::Model) &&
      voter.ballot_voter?
end

.voter_id_and_type_name_for(item) ⇒ Object

Return the id and canonical voter type name for the item, using #voter_for.



135
136
137
# File 'lib/ballot/sequel.rb', line 135

def voter_id_and_type_name_for(item)
  __id_and_type_name(voter_for(item))
end

Instance Method Details

#ballot_votable?Boolean

Delegate the question of #ballot_votable? to the class.

Returns:

  • (Boolean)


55
56
57
# File 'lib/ballot/sequel.rb', line 55

def ballot_votable?
  self.class.ballot_votable?
end

#ballot_voter?Boolean

Delegate the question of #ballot_voter? to the class.

Returns:

  • (Boolean)


60
61
62
# File 'lib/ballot/sequel.rb', line 60

def ballot_voter?
  self.class.ballot_voter?
end