Class: Mongoid::Association::Nested::Many
- Inherits:
-
Object
- Object
- Mongoid::Association::Nested::Many
- Includes:
- Buildable
- Defined in:
- lib/mongoid/association/nested/many.rb
Overview
Builder class used to perform #accepts_nested_attributes_for attribute assignment on many-to-n associations.
Instance Attribute Summary
Attributes included from Buildable
#association, #attributes, #existing, #options
Instance Method Summary collapse
-
#build(parent, options = {}) ⇒ Array
Builds the association depending on the attributes and the options passed to the macro.
-
#initialize(association, attributes, options = {}) ⇒ Many
constructor
Create the new builder for nested attributes on one-to-many associations.
Methods included from Buildable
#allow_destroy?, #convert_id, #reject?, #update_only?
Constructor Details
#initialize(association, attributes, options = {}) ⇒ Many
Create the new builder for nested attributes on one-to-many associations.
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mongoid/association/nested/many.rb', line 50 def initialize(association, 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 @association = association @options = @class_name = [:class_name] ? [:class_name].constantize : association.klass end |
Instance Method Details
#build(parent, options = {}) ⇒ Array
Builds the association 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 association, a replacement of the association with a new document, or a removal of the association.
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/mongoid/association/nested/many.rb', line 27 def build(parent, = {}) @existing = parent.send(association.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 |