lib/ripple-anaf/nested_attributes.rb in ripple-anaf-0.8.0.beta1 vs lib/ripple-anaf/nested_attributes.rb in ripple-anaf-0.8.0.beta2
- old
+ new
@@ -69,11 +69,35 @@
#
# manifest.update_attributes(:shipment_attributes => [ { :key => 'xxx', :reference => 'updated foo1' },
# { :key => 'yyy', :reference => 'updated foo2' } ])
# manifest.shipments.first.reference # => updated foo1
# manifest.shipments.second.reference # => updated foo2
+ #
+ # NOTE: On many embedded, then entire collection of embedded documents is replaced, as there
+ # is no key to specifically update.
#
+ # Given
+ #
+ # class Manifest
+ # include Ripple::Documnet
+ # many :signatures
+ # accepts_nested_attributes_for :signatures
+ # end
+ #
+ # class Signature
+ # include Ripple::EmbeddedDocument
+ # property :esignature, String
+ # end
+ #
+ # The assigning of attributes replaces existing:
+ #
+ # manifest = Manifest.create(:signature_attributes => [ { :esig => 'a00001' }, { :esig => 'b00001' } ]
+ # manifest.signatures # => [<Signature esig="a00001">, <Signature esig="b00001">]
+ #
+ # manifest.signature_attributes = [ { :esig => 'c00001' } ]
+ # manifest.signatures # => [<Signature esig="c00001">]
+ #
module ClassMethods
def accepts_nested_attributes_for(*attr_names)
options = { :allow_destroy => false }
options.update(attr_names.extract_options!)
@@ -182,10 +206,17 @@
assign_nested_attributes_for_many_linked_association(association_name, attributes_collection)
end
end
def assign_nested_attributes_for_many_embedded_association(association_name, attributes_collection)
- raise NotImplementedError, "Unclear how to identify which embedded document to update without a key"
+ options = nested_attributes_options[association_name]
+ send(:"#{association_name}=", []) # Clobber existing
+ attributes_collection.each do |attributes|
+ attributes = attributes.stringify_keys
+ if !reject_new_record?(association_name, attributes)
+ send(association_name).build(attributes.except(*UNASSIGNABLE_KEYS))
+ end
+ end
end
def assign_nested_attributes_for_many_linked_association(association_name, attributes_collection)
options = nested_attributes_options[association_name]
attributes_collection.each do |attributes|