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
Instance Attribute Summary
Attributes inherited from NestedBuilder
#attributes, #existing, #metadata, #options
Instance Method Summary collapse
-
#build(parent) ⇒ 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) ⇒ 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.respond_to?(:with_indifferent_access) process(parent, attrs) else process(parent, attrs[1]) end end end |