Module: DataMapper::NestedAttributes::Model
- Defined in:
- lib/dm-accepts_nested_attributes/model.rb
Instance Method Summary collapse
-
#accepts_nested_attributes_for(association_name, options = {}) ⇒ Object
Allows any association to accept nested attributes.
- #options_for_nested_attributes ⇒ Object
Instance Method Details
#accepts_nested_attributes_for(association_name, options = {}) ⇒ Object
Allows any association to accept nested attributes.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/dm-accepts_nested_attributes/model.rb', line 35 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] = 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 ⇒ Object
73 74 75 |
# File 'lib/dm-accepts_nested_attributes/model.rb', line 73 def @options_for_nested_attributes ||= {} end |