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

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

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.

Example:

One.new(metadata, attributes, options)

Options:

metadata: The relation metadata attributes: The attributes hash to attempt to set. options: The options defined.

Returns:

A new builder.



52
53
54
55
56
57
# File 'lib/mongoid/relations/builders/nested_attributes/one.rb', line 52

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

Instance Attribute Details

#destroyObject

Returns the value of attribute destroy.



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

def destroy
  @destroy
end

Instance Method Details

#build(parent) ⇒ Object

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:

one.build(person)

Options:

parent: The parent document of the relation.



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

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