Class: Machinist::DataMapperAdapter
- Defined in:
- lib/machinist/data_mapper.rb
Class Method Summary collapse
-
.assigned_attributes_without_associations(lathe) ⇒ Object
This method takes care of converting any associated objects, in the hash returned by Lathe#assigned_attributes, into their object ids.
- .association_is_many_to_one?(association) ⇒ Boolean
- .class_for_association(object, attribute) ⇒ Object
- .has_association?(object, attribute) ⇒ Boolean
Class Method Details
.assigned_attributes_without_associations(lathe) ⇒ Object
This method takes care of converting any associated objects, in the hash returned by Lathe#assigned_attributes, into their object ids.
For example, let’s say we have blueprints like this:
Post.blueprint { }
Comment.blueprint { post }
Lathe#assigned_attributes will return { :post => … }, but we want to pass { :post_id => 1 } to a controller.
This method takes care of cleaning this up.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/machinist/data_mapper.rb', line 40 def self.assigned_attributes_without_associations(lathe) attributes = {} lathe.assigned_attributes.each_pair do |attribute, value| association = lathe.object.class.relationships[attribute] if association && association_is_many_to_one?(association) # DataMapper child_key can have more than one property, but I'm not # sure in what circumstances this would be the case. I'm assuming # here that there's only one property. key = association.child_key.map(&:field).first.to_sym attributes[key] = value.id else attributes[attribute] = value end end attributes end |
.association_is_many_to_one?(association) ⇒ Boolean
17 18 19 20 21 22 23 24 25 |
# File 'lib/machinist/data_mapper.rb', line 17 def self.association_is_many_to_one?(association) if defined?(DataMapper::Associations::ManyToOne::Relationship) # We're using the next branch of DM association.class == DataMapper::Associations::ManyToOne::Relationship else # We're using the 0.9 or less branch. association.[:max].nil? end end |
.class_for_association(object, attribute) ⇒ Object
12 13 14 15 |
# File 'lib/machinist/data_mapper.rb', line 12 def self.class_for_association(object, attribute) association = object.class.relationships[attribute] association && association.parent_model end |
.has_association?(object, attribute) ⇒ Boolean
8 9 10 |
# File 'lib/machinist/data_mapper.rb', line 8 def self.has_association?(object, attribute) object.class.relationships.has_key?(attribute) end |