Class: Mongoid::Relations::Builders::NestedAttributes::Many

Inherits:
NestedBuilder
  • Object
show all
Defined in:
lib/mongoid/relations/builders/nested_attributes/many.rb

Overview

Since:

  • 2.0.0.rc.1

Instance Attribute Summary

Attributes inherited from NestedBuilder

#attributes, #existing, #metadata, #options

Instance Method Summary collapse

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.

Examples:

Initialize the builder.

One.new(, attributes, options)

Parameters:

  • metadata (Metadata)

    The relation metadata.

  • attributes (Hash)

    The attributes hash to attempt to set.

  • options (Hash) (defaults to: {})

    The options defined.

Since:

  • 2.0.0.rc.1



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, options = {})
  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 = 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.

Examples:

Build the nested attrs.

many.build(person)

Parameters:

  • parent (Document)

    The parent document of the relation.

Returns:

  • (Array)

    The attributes.

Since:

  • 2.0.0.rc.1



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, options = {})
  @existing = parent.send(.name)
  if over_limit?(attributes)
    raise Errors::TooManyNestedAttributeRecords.new(existing, options[: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