Class: Mongoid::Relations::Builders::NestedAttributes::One

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

Overview

Since:

  • 2.0.0.rc.1

Instance Attribute Summary collapse

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) ⇒ One

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

Examples:

Instantiate the builder.

One.new(, attributes)

Parameters:

  • metadata (Metadata)

    The relation metadata.

  • attributes (Hash)

    The attributes hash to attempt to set.

  • options (Hash)

    The options defined.

Since:

  • 2.0.0



49
50
51
52
53
54
# File 'lib/mongoid/relations/builders/nested_attributes/one.rb', line 49

def initialize(, attributes, options)
  @attributes = attributes.with_indifferent_access
  @metadata = 
  @options = options
  @destroy = @attributes.delete(:_destroy)
end

Instance Attribute Details

#destroyObject

Since:

  • 2.0.0.rc.1



8
9
10
# File 'lib/mongoid/relations/builders/nested_attributes/one.rb', line 8

def destroy
  @destroy
end

Instance Method Details

#build(parent) ⇒ Document

Note:

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.

Builds the relation depending on the attributes and the options passed to the macro.

Examples:

Build a 1-1 nested document.

one.build(person, as: :admin)

Parameters:

  • parent (Document)

    The parent document.

Returns:

Since:

  • 2.0.0



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mongoid/relations/builders/nested_attributes/one.rb', line 25

def build(parent)
  return if reject?(parent, attributes)
  @existing = parent.send(.name)
  if update?
    attributes.delete_id
    existing.assign_attributes(attributes)
  elsif replace?
    parent.send(.setter, Factory.build(.klass, attributes))
  elsif delete?
    parent.send(.setter, nil)
  end
end