Module: DataMapper::NestedAttributes::Model
- Defined in:
- lib/dm-accepts_nested_attributes/model.rb
Instance Method Summary collapse
-
#accepts_nested_attributes_for(association_name, options = {}) ⇒ void
Allows an association to accept nested attributes.
-
#options_for_nested_attributes ⇒ Hash{DataMapper::Associations::Relationship => Hash}
Returns a hash with the options for all associations (using the corresponding relationship as key) that accept nested attributes.
Instance Method Details
#accepts_nested_attributes_for(association_name, options = {}) ⇒ void
This method returns an undefined value.
Allows an association to accept nested attributes.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/dm-accepts_nested_attributes/model.rb', line 58 def accepts_nested_attributes_for(association_name, = {}) # ---------------------------------------------------------------------------------- # try to fail as early as possible # ---------------------------------------------------------------------------------- unless relationship = relationships(repository_name)[association_name] raise(ArgumentError, "No relationship #{association_name.inspect} for '#{name}' in :#{repository_name} repository") end # raise InvalidOptions if the given options don't make sense () # by default, nested attributes can't be destroyed = { :allow_destroy => false }.update() # ---------------------------------------------------------------------------------- # should be safe to go from here # ---------------------------------------------------------------------------------- [relationship.name] = include ::DataMapper::NestedAttributes::Resource type = relationship.max > 1 ? :collection : :resource define_method "#{association_name}_attributes" do instance_variable_get("@#{association_name}_attributes") end define_method "#{association_name}_attributes=" do |attributes| attributes = sanitize_nested_attributes(attributes) instance_variable_set("@#{association_name}_attributes", attributes) send("assign_nested_attributes_for_related_#{type}", relationship, attributes) end end |
#options_for_nested_attributes ⇒ Hash{DataMapper::Associations::Relationship => Hash}
Returns a hash with the options for all associations (using the corresponding relationship as key) that accept nested attributes.
100 101 102 |
# File 'lib/dm-accepts_nested_attributes/model.rb', line 100 def @options_for_nested_attributes ||= DataMapper::NestedAttributes::BackwardsCompatibilityHash.new(self) end |