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
Overview
Instance Attribute Summary collapse
Attributes inherited from NestedBuilder
#attributes, #existing, #metadata, #options
Instance Method Summary collapse
-
#build(parent) ⇒ Document
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.
49 50 51 52 53 54 |
# File 'lib/mongoid/relations/builders/nested_attributes/one.rb', line 49 def initialize(, attributes, ) @attributes = attributes.with_indifferent_access @metadata = @options = @destroy = @attributes.delete(:_destroy) end |
Instance Attribute Details
#destroy ⇒ Object
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.
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 |