Module: Mongoid::Attributes::Nested::ClassMethods
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Extended In:
| |
Defined in: | lib/mongoid/attributes/nested.rb |
Constant Summary
-
REJECT_ALL_BLANK_PROC =
# File 'lib/mongoid/attributes/nested.rb', line 18->(attributes){ attributes.all? { |key, value| key == '_destroy' || value.blank? } }
Instance Method Summary
-
#accepts_nested_attributes_for(*args)
Used when needing to update related models from a parent association.
-
#autosave_nested_attributes(association)
private
Internal use only
Internal use only
Add the autosave information for the nested association.
Instance Method Details
#accepts_nested_attributes_for(*args)
Used when needing to update related models from a parent association. Can be used on embedded or referenced associations.
# File 'lib/mongoid/attributes/nested.rb', line 48
def accepts_nested_attributes_for(*args) = args. .dup [:autosave] = true if [:autosave].nil? [:reject_if] = REJECT_ALL_BLANK_PROC if [:reject_if] == :all_blank args.each do |name| meth = "#{name}_attributes=" self.nested_attributes["#{name}_attributes"] = meth association = relations[name.to_s] raise Errors::NestedAttributesMetadataNotFound.new(self, name) unless association autosave_nested_attributes(association) if [:autosave] re_define_method(meth) do |attrs| _assigning do if association.polymorphic? and association.inverse_type klass = association.resolver.model_for(send(association.inverse_type)) = .merge!(:class_name => klass) end association.nested_builder(attrs, ).build(self) end end end end
#autosave_nested_attributes(association) (private)
This method is for internal use only.
Add the autosave information for the nested association.
# File 'lib/mongoid/attributes/nested.rb', line 82
def autosave_nested_attributes(association) # In order for the autosave functionality to work properly, the association needs to be # marked as autosave despite the fact that the option isn't present. Because the method # Association#autosave? is implemented by checking the autosave option, this is the most # straightforward way to mark it. if association.autosave? || (association. [:autosave].nil? && !association. ) association. [:autosave] = true Association::Referenced::AutoSave.define_autosave!(association) end end