Create the new builder for nested attributes on one-to-one relations.
@example Instantiate the builder.
One.new(metadata, attributes, options)
@param [ Metadata ] metadata The relation metadata. @param [ Hash ] attributes The attributes hash to attempt to set. @param [ Hash ] options The options defined.
@since 2.0.0
# File lib/mongoid/relations/builders/nested_attributes/one.rb, line 49 def initialize(metadata, attributes, options) @attributes = attributes.with_indifferent_access @metadata = metadata @options = options @destroy = @attributes.delete(:_destroy) end
Builds the relation depending on the attributes and the options passed to the macro.
@example Build a 1-1 nested document.
one.build(person, as: :admin)
@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.
@param [ Document ] parent The parent document. @param [ Hash ] options The mass assignment options.
@return [ Document ] The built document.
@since 2.0.0
# File lib/mongoid/relations/builders/nested_attributes/one.rb, line 25 def build(parent, options = {}) return if reject?(parent, attributes) @existing = parent.send(metadata.name) if update? attributes.delete_id existing.assign_attributes(attributes, options) elsif replace? parent.send(metadata.setter, Factory.build(metadata.klass, attributes, options)) elsif delete? parent.send(metadata.setter, nil) end end
Generated with the Darkfish Rdoc Generator 2.