Class: Mongoid::Relations::Builders::NestedAttributes::One
- Inherits:
-
NestedBuilder
- Object
- NestedBuilder
- Mongoid::Relations::Builders::NestedAttributes::One
- Defined in:
- lib/mongoid/relations/builders/nested_attributes/one.rb
Instance Attribute Summary collapse
-
#destroy ⇒ Object
Returns the value of attribute destroy.
Attributes inherited from NestedBuilder
#attributes, #existing, #metadata, #options
Instance Method Summary collapse
-
#build(parent) ⇒ Object
Builds the relation depending on the attributes and the options passed to the macro.
-
#initialize(metadata, attributes, options) ⇒ One
constructor
Create the new builder for nested attributes on one-to-one relations.
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, ) @attributes = attributes.with_indifferent_access @metadata = @options = @destroy = @attributes.delete(:_destroy) end |
Instance Attribute Details
#destroy ⇒ Object
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 |