Class: Mongoid::Relations::Builders::NestedAttributes::Many
- Inherits:
-
NestedBuilder
- Object
- NestedBuilder
- Mongoid::Relations::Builders::NestedAttributes::Many
- Defined in:
- lib/mongoid/relations/builders/nested_attributes/many.rb
Overview
Instance Attribute Summary
Attributes inherited from NestedBuilder
#attributes, #existing, #metadata, #options
Instance Method Summary collapse
-
#build(parent, options = {}) ⇒ Array
Builds the relation depending on the attributes and the options passed to the macro.
-
#initialize(metadata, attributes, options = {}) ⇒ Many
constructor
Create the new builder for nested attributes on one-to-many relations.
Methods inherited from NestedBuilder
#allow_destroy?, #convert_id, #reject?, #update_only?
Constructor Details
#initialize(metadata, attributes, options = {}) ⇒ Many
Create the new builder for nested attributes on one-to-many relations.
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/mongoid/relations/builders/nested_attributes/many.rb', line 44 def initialize(, attributes, = {}) if attributes.respond_to?(:with_indifferent_access) @attributes = attributes.with_indifferent_access.sort do |a, b| a[0].to_i <=> b[0].to_i end else @attributes = attributes end @metadata = @options = end |
Instance Method Details
#build(parent, options = {}) ⇒ Array
Builds the relation depending on the attributes and the options passed to the macro.
This attempts to perform 3 operations, either one of an update of the existing relation, a replacement of the relation with a new document, or a removal of the relation.
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/mongoid/relations/builders/nested_attributes/many.rb', line 21 def build(parent, = {}) @existing = parent.send(.name) if over_limit?(attributes) raise Errors::TooManyNestedAttributeRecords.new(existing, [:limit]) end attributes.each do |attrs| if attrs.is_a?(::Hash) process_attributes(parent, attrs.with_indifferent_access) else process_attributes(parent, attrs[1].with_indifferent_access) end end end |