Module: Prim::ClassMethods
- Includes:
- Helpers
- Defined in:
- lib/prim.rb
Instance Method Summary collapse
-
#has_primary(name, options = {}) ⇒ Object
TODO: allow multiple singular names in one call.
Methods included from Helpers
Instance Method Details
#has_primary(name, options = {}) ⇒ Object
TODO: allow multiple singular names in one call.
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 |
# File 'lib/prim.rb', line 19 def has_primary name, = {} singular_name = name.to_sym association_name = plural_sym(singular_name) self.prim_relationships = self.prim_relationships.try(:dup) || Hash.new self.prim_relationships[ singular_name ] = Prim::Relationship.new(association_name, self, ) # Store this configuration for global access. Prim.configured_primaries << self.prim_relationships[ singular_name ] # Create dynamic methods for getting and setting the # primary Collection member. define_method "primary_#{ singular_name }" do prim_collection_for(singular_name).primary end define_method "primary_#{ singular_name }=" do |instance| prim_collection_for(singular_name).primary = instance end # We alias the association's method chain so the original can be accessed. # IS THIS IMPORTANT?! define_method "#{ association_name }_with_primaries" do prim_collection_for(singular_name).all end alias_method_chain association_name, :primaries end |