Methods

Class/Module Index [+]

Quicksearch

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

Public Instance Methods

build(parent) click to toggle source

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.

@example Build the nested attrs.

many.build(person)

@param [ Document ] parent The parent document of the relation.

@return [ Array ] The attributes.

# File lib/mongoid/relations/builders/nested_attributes/many.rb, line 20
def build(parent)
  @existing = parent.send(metadata.name)
  if over_limit?(attributes)
    raise Errors::TooManyNestedAttributeRecords.new(existing, options[:limit])
  end
  attributes.each do |attrs|
    if attrs.respond_to?(:with_indifferent_access)
      process(parent, attrs)
    else
      process(parent, attrs[1])
    end
  end
end

Public Class Methods

new(metadata, attributes, options = {}) click to toggle source

Create the new builder for nested attributes on one-to-many relations.

@example Initialize the builder.

One.new(metadata, attributes, options)

@param [ Metadata ] metadata The relation metadata. @param [ Hash ] attributes The attributes hash to attempt to set. @param [ Hash ] options The options defined.

# File lib/mongoid/relations/builders/nested_attributes/many.rb, line 43
def initialize(metadata, 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 = metadata
  @options = options
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.