Module: Sequel::Plugins::BallotVotable
- Defined in:
- lib/sequel/plugins/ballot_votable.rb
Overview
The votable plugin marks the model as containing objects that can be voted on. It creates a polymorphic one-to-many relationship from the Votable model to Ballot::Sequel::Vote.
This may be used with single_table_inheritance, but should be loaded after single_table_inheritance has been called. It has not been tested with class_table_inheritance.
This plug-in causes Ballot::Votable to be included into the affected model, and Ballot::Votable::ClassMethods to be extended onto the affected model.
Defined Under Namespace
Modules: InstanceMethods
Class Method Summary collapse
-
.apply(model) ⇒ Object
:nodoc:.
Class Method Details
.apply(model) ⇒ Object
:nodoc:
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/sequel/plugins/ballot_votable.rb', line 17 def self.apply(model) # :nodoc: require 'ballot/sequel/vote' require 'ballot/words' require 'ballot/votable' model.instance_eval do if columns.include?(:cached_ballot_summary) plugin :serialization, :json, :cached_ballot_summary end # Create a polymorphic one-to-many relationship for votables. Based # heavily on the one_to_many implementation from sequel_polymorphic, # but customized to sequel-voting's needs. one_to_many :ballots_for, key: :votable_id, reciprocal: :votable, reciprocal_type: :one_to_many, conditions: { votable_type: Ballot::Sequel.type_name(model) }, adder: ->(many_of_instance) { many_of_instance.update( votable_id: pk, votable_type: Ballot::Sequel.type_name(model) ) }, remover: ->(many_of_instance) { many_of_instance.delete }, clearer: -> { ballots_for_dataset.delete }, class: '::Ballot::Sequel::Vote' include Ballot::Votable extend Ballot::Votable::ClassMethods end end |