Class: Mongoid::Relations::Referenced::One
- Defined in:
- lib/mongoid/relations/referenced/one.rb
Overview
This class defines the behaviour for all relations that are a one-to-one between documents in different collections.
Instance Attribute Summary
Attributes inherited from Proxy
#base, #loaded, #metadata, #target
Class Method Summary collapse
-
.builder(meta, object) ⇒ Builder
Return the builder that is responsible for generating the documents that will be used by this relation.
-
.embedded? ⇒ false
Returns true if the relation is an embedded one.
-
.foreign_key_default ⇒ nil
Get the default value for the foreign key.
-
.foreign_key_suffix ⇒ String
Returns the suffix of the foreign key field, either “_id” or “_ids”.
-
.macro ⇒ Symbol
Returns the macro for this relation.
-
.nested_builder(metadata, attributes, options) ⇒ NestedBuilder
Return the nested builder that is responsible for generating the documents that will be used by this relation.
-
.stores_foreign_key? ⇒ false
Tells the caller if this relation is one that stores the foreign key on its own objects.
Instance Method Summary collapse
-
#bind(options = {}) ⇒ Object
Binds the base object to the inverse of the relation.
-
#initialize(base, target, metadata) ⇒ One
constructor
Instantiate a new references_one relation.
-
#load!(options = {}) ⇒ One
Will load the target into an array if the target had not already been loaded.
-
#nullify ⇒ Object
Removes the association between the base document and the target document by deleting the foreign key and the reference, orphaning the target document in the process.
-
#unbind(old_target, options = {}) ⇒ Object
Unbinds the base object to the inverse of the relation.
Methods inherited from One
Methods inherited from Proxy
Constructor Details
#initialize(base, target, metadata) ⇒ One
Instantiate a new references_one relation. Will set the foreign key and the base on the inverse object.
41 42 43 44 45 |
# File 'lib/mongoid/relations/referenced/one.rb', line 41 def initialize(base, target, ) init(base, target, ) do characterize_one(target) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Mongoid::Relations::Proxy
Class Method Details
.builder(meta, object) ⇒ Builder
Return the builder that is responsible for generating the documents that will be used by this relation.
128 129 130 |
# File 'lib/mongoid/relations/referenced/one.rb', line 128 def builder(, object) Builders::Referenced::One.new(, object) end |
.embedded? ⇒ false
Returns true if the relation is an embedded one. In this case always false.
141 142 143 |
# File 'lib/mongoid/relations/referenced/one.rb', line 141 def false end |
.foreign_key_default ⇒ nil
Get the default value for the foreign key.
153 154 155 |
# File 'lib/mongoid/relations/referenced/one.rb', line 153 def foreign_key_default nil end |
.foreign_key_suffix ⇒ String
Returns the suffix of the foreign key field, either “_id” or “_ids”.
165 166 167 |
# File 'lib/mongoid/relations/referenced/one.rb', line 165 def foreign_key_suffix "_id" end |
.macro ⇒ Symbol
Returns the macro for this relation. Used mostly as a helper in reflection.
176 177 178 |
# File 'lib/mongoid/relations/referenced/one.rb', line 176 def macro :references_one end |
.nested_builder(metadata, attributes, options) ⇒ NestedBuilder
Return the nested builder that is responsible for generating the documents that will be used by this relation.
202 203 204 |
# File 'lib/mongoid/relations/referenced/one.rb', line 202 def nested_builder(, attributes, ) Builders::NestedAttributes::One.new(, attributes, ) end |
.stores_foreign_key? ⇒ false
Tells the caller if this relation is one that stores the foreign key on its own objects.
215 216 217 |
# File 'lib/mongoid/relations/referenced/one.rb', line 215 def stores_foreign_key? false end |
Instance Method Details
#bind(options = {}) ⇒ Object
Binds the base object to the inverse of the relation. This is so we are referenced to the actual objects themselves and dont hit the database twice when setting the relations up.
This is called after first creating the relation, or if a new object is set on the relation.
27 28 29 30 |
# File 'lib/mongoid/relations/referenced/one.rb', line 27 def bind( = {}) binding.bind() target.save if base.persisted? && ![:binding] end |
#load!(options = {}) ⇒ One
Will load the target into an array if the target had not already been loaded.
56 57 58 59 |
# File 'lib/mongoid/relations/referenced/one.rb', line 56 def load!( = {}) raise_mixed if klass. super() end |
#nullify ⇒ Object
Removes the association between the base document and the target document by deleting the foreign key and the reference, orphaning the target document in the process.
69 70 71 72 73 74 |
# File 'lib/mongoid/relations/referenced/one.rb', line 69 def nullify target.send(.foreign_key_setter, nil) target.send(:remove_instance_variable, "@#{.inverse(target)}") base.send(:remove_instance_variable, "@#{.name}") target.save end |
#unbind(old_target, options = {}) ⇒ Object
Unbinds the base object to the inverse of the relation. This occurs when setting a side of the relation to nil.
Will delete the object if necessary.
92 93 94 95 96 97 |
# File 'lib/mongoid/relations/referenced/one.rb', line 92 def unbind(old_target, = {}) binding(old_target).unbind() if base.persisted? && !old_target.destroyed? && ![:binding] old_target.delete end end |