Class: Mongoid::Association::Nested::One
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
self,
Buildable
|
|
Inherits: | Object |
Defined in: | lib/mongoid/association/nested/one.rb |
Overview
Builder class used to perform #accepts_nested_attributes_for attribute assignment on one-to-n associations.
Class Method Summary
-
.new(association, attributes, options) ⇒ One
constructor
Create the new builder for nested attributes on one-to-one associations.
Instance Attribute Summary
- #destroy rw
-
#acceptable_id? ⇒ true | false
readonly
private
Internal use only
Internal use only
Is the id in the attributes acceptable for allowing an update to the existing association?
-
#delete? ⇒ true | false
readonly
private
Can the existing association be deleted?
-
#destroyable? ⇒ true | false
readonly
private
Can the existing association potentially be destroyed?
-
#replace? ⇒ true | false
readonly
private
Is the document to be replaced?
-
#update? ⇒ true | false
readonly
private
Should the document be updated?
Buildable
- Included
#allow_destroy? | Determines if destroys are allowed for this document. |
#association, #attributes, #existing, #options, | |
#update_only? | Determines if only updates can occur. |
Instance Method Summary
-
#build(parent) ⇒ Document
Builds the association depending on the attributes and the options passed to the macro.
-
#check_for_id_violation!
private
Checks to see if the _id attribute (which is supposed to be immutable) is being asked to change.
-
#class_from(name_or_class) ⇒ Mongoid::Document
private
Coerces the argument into a class, or defaults to the association’s class.
-
#extracted_id ⇒ BSON::ObjectId | String | Object | nil
private
Extracts and converts the id to the expected type.
Buildable
- Included
#convert_id | Convert an id to its appropriate type. |
#reject? | Returns the reject if option defined with the macro. |
#delete_id | Deletes the id key from the given hash. |
#extract_id | Get the id attribute from the given hash, whether it’s prefixed with an underscore or is a symbol. |
Constructor Details
.new(association, attributes, options) ⇒ One
Create the new builder for nested attributes on one-to-one associations.
# File 'lib/mongoid/association/nested/one.rb', line 52
def initialize(association, attributes, ) @attributes = attributes.with_indifferent_access @association = association @options = @class_name = class_from( [:class_name]) @destroy = @attributes.delete(:_destroy) end
Instance Attribute Details
#acceptable_id? ⇒ true
| false
(readonly, private)
Is the id in the attributes acceptable for allowing an update to the existing association?
# File 'lib/mongoid/association/nested/one.rb', line 95
def acceptable_id? id = extracted_id existing._id == id || id.nil? || (existing._id != id && update_only?) end
#delete? ⇒ true
| false
(readonly, private)
Can the existing association be deleted?
# File 'lib/mongoid/association/nested/one.rb', line 106
def delete? id = association.klass.extract_id_field(attributes) destroyable? && !id.nil? end
#destroy (rw)
[ GitHub ]# File 'lib/mongoid/association/nested/one.rb', line 13
attr_accessor :destroy
#destroyable? ⇒ true
| false
(readonly, private)
Can the existing association potentially be destroyed?
# File 'lib/mongoid/association/nested/one.rb', line 118
def destroyable? Nested::DESTROY_FLAGS.include?(destroy) && allow_destroy? end
#replace? ⇒ true
| false
(readonly, private)
Is the document to be replaced?
# File 'lib/mongoid/association/nested/one.rb', line 128
def replace? !existing && !destroyable? && !attributes.blank? end
#update? ⇒ true
| false
(readonly, private)
Should the document be updated?
# File 'lib/mongoid/association/nested/one.rb', line 138
def update? existing && !destroyable? && acceptable_id? end
Instance Method Details
#build(parent) ⇒ Document
This attempts to perform 3 operations, either one of an update of the existing association, a replacement of the association with a new document, or a removal of the association.
Builds the association depending on the attributes and the options passed to the macro.
# File 'lib/mongoid/association/nested/one.rb', line 28
def build(parent) return if reject?(parent, attributes) @existing = parent.send(association.name) if update? delete_id(attributes) existing.assign_attributes(attributes) elsif replace? parent.send(association.setter, Factory.build(@class_name, attributes)) elsif delete? parent.send(association.setter, nil) else check_for_id_violation! end end
#check_for_id_violation! (private)
Checks to see if the _id attribute (which is supposed to be immutable) is being asked to change. If so, raise an exception.
If Mongoid::Config.immutable_ids is false, this will do nothing, and the update operation will fail silently.
# File 'lib/mongoid/association/nested/one.rb', line 150
def check_for_id_violation! # look for the basic criteria of an update (see #update?) return unless existing&.persisted? && !destroyable? # if the id is either absent, or if it equals the existing record's # id, there is no immutability violation. id = extracted_id return if existing._id == id || id.nil? # otherwise, an attempt has been made to set the _id of an existing, # persisted document. if Mongoid::Config.immutable_ids raise Errors::ImmutableAttribute.new(:_id, id) else Mongoid::Warnings.warn_mutable_ids end end
#class_from(name_or_class) ⇒ Mongoid::Document (private)
Coerces the argument into a class, or defaults to the association’s class.
# File 'lib/mongoid/association/nested/one.rb', line 67
def class_from(name_or_class) case name_or_class when nil, false then association.klass when String then name_or_class.constantize else name_or_class end end
#extracted_id ⇒ BSON::ObjectId | String | Object
| nil
(private)
Extracts and converts the id to the expected type.
# File 'lib/mongoid/association/nested/one.rb', line 79
def extracted_id @extracted_id ||= begin id = association.klass.extract_id_field(attributes) convert_id(existing.class, id) end end