Class: Manufactory::ActiveRecordAdapter
- Defined in:
- lib/manufactory/adapters/activerecord.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.
- .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.
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/manufactory/adapters/activerecord.rb', line 29 def self.assigned_attributes_without_associations(lathe) attributes = {} lathe.assigned_attributes.each_pair do |attribute, value| association = lathe.object.class.reflect_on_association(attribute) if association && association.macro == :belongs_to && !value.nil? attributes[association.primary_key_name.to_sym] = value.id else attributes[attribute] = value end end attributes end |
.class_for_association(object, attribute) ⇒ Object
11 12 13 14 |
# File 'lib/manufactory/adapters/activerecord.rb', line 11 def self.class_for_association(object, attribute) association = object.class.reflect_on_association(attribute) association && association.klass end |
.has_association?(object, attribute) ⇒ Boolean
7 8 9 |
# File 'lib/manufactory/adapters/activerecord.rb', line 7 def self.has_association?(object, attribute) object.class.reflect_on_association(attribute) end |