Class: Mongoid::Association::Nested::One
- Inherits:
-
Object
- Object
- Mongoid::Association::Nested::One
- Includes:
- Buildable
- Defined in:
- lib/mongoid/association/nested/one.rb
Instance Attribute Summary collapse
-
#destroy ⇒ Object
Returns the value of attribute destroy.
Attributes included from Buildable
#association, #attributes, #existing, #options
Instance Method Summary collapse
-
#build(parent) ⇒ Document
Builds the relation depending on the attributes and the options passed to the macro.
-
#initialize(association, attributes, options) ⇒ One
constructor
Create the new builder for nested attributes on one-to-one relations.
Methods included from Buildable
#allow_destroy?, #convert_id, #reject?, #update_only?
Constructor Details
#initialize(association, attributes, options) ⇒ One
Create the new builder for nested attributes on one-to-one relations.
50 51 52 53 54 55 56 |
# File 'lib/mongoid/association/nested/one.rb', line 50 def initialize(association, attributes, ) @attributes = attributes.with_indifferent_access @association = association @options = @class_name = [:class_name] ? [:class_name].constantize : association.klass @destroy = @attributes.delete(:_destroy) end |
Instance Attribute Details
#destroy ⇒ Object
Returns the value of attribute destroy.
8 9 10 |
# File 'lib/mongoid/association/nested/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.
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/mongoid/association/nested/one.rb', line 26 def build(parent) return if reject?(parent, attributes) @existing = parent.send(association.name) if update? attributes.delete_id existing.assign_attributes(attributes) elsif replace? parent.send(association.setter, Factory.build(@class_name, attributes)) elsif delete? parent.send(association.setter, nil) end end |