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, loading = false) ⇒ Builder
Return the builder that is responsible for generating the documents that will be used by this relation.
-
.criteria(metadata, object, type = nil) ⇒ Criteria
Get the standard criteria used for querying this relation.
-
.eager_load(metadata, criteria) ⇒ Criteria
Get the criteria that is used to eager load a relation of this type.
-
.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.
-
.path(document) ⇒ Root
Get the path calculator for the supplied document.
-
.stores_foreign_key? ⇒ false
Tells the caller if this relation is one that stores the foreign key on its own objects.
-
.valid_options ⇒ Array<Symbol>
Get the valid options allowed with this relation.
-
.validation_default ⇒ true, false
Get the default validation setting for the relation.
Instance Method Summary collapse
-
#initialize(base, target, metadata) ⇒ One
constructor
Instantiate a new references_one relation.
-
#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.
-
#substitute(replacement) ⇒ One
Substitutes the supplied target document for the existing document in 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.
19 20 21 22 23 24 25 26 |
# File 'lib/mongoid/relations/referenced/one.rb', line 19 def initialize(base, target, ) init(base, target, ) do raise_mixed if klass. characterize_one(target) bind_one target.save if persistable? 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, loading = false) ⇒ Builder
Return the builder that is responsible for generating the documents that will be used by this relation.
105 106 107 |
# File 'lib/mongoid/relations/referenced/one.rb', line 105 def builder(, object, loading = false) Builders::Referenced::One.new(, object, loading) end |
.criteria(metadata, object, type = nil) ⇒ Criteria
Get the standard criteria used for querying this relation.
121 122 123 |
# File 'lib/mongoid/relations/referenced/one.rb', line 121 def criteria(, object, type = nil) .klass.where(.foreign_key => object) end |
.eager_load(metadata, criteria) ⇒ Criteria
Get the criteria that is used to eager load a relation of this type.
137 138 139 140 141 142 |
# File 'lib/mongoid/relations/referenced/one.rb', line 137 def eager_load(, criteria) klass, foreign_key = .klass, .foreign_key klass.any_in(foreign_key => criteria.load_ids("_id").uniq).each do |doc| IdentityMap.set_one(doc, foreign_key => doc.send(foreign_key)) end end |
.embedded? ⇒ false
Returns true if the relation is an embedded one. In this case always false.
153 154 155 |
# File 'lib/mongoid/relations/referenced/one.rb', line 153 def false end |
.foreign_key_default ⇒ nil
Get the default value for the foreign key.
165 166 167 |
# File 'lib/mongoid/relations/referenced/one.rb', line 165 def foreign_key_default nil end |
.foreign_key_suffix ⇒ String
Returns the suffix of the foreign key field, either “_id” or “_ids”.
177 178 179 |
# File 'lib/mongoid/relations/referenced/one.rb', line 177 def foreign_key_suffix "_id" end |
.macro ⇒ Symbol
Returns the macro for this relation. Used mostly as a helper in reflection.
188 189 190 |
# File 'lib/mongoid/relations/referenced/one.rb', line 188 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.
214 215 216 |
# File 'lib/mongoid/relations/referenced/one.rb', line 214 def nested_builder(, attributes, ) Builders::NestedAttributes::One.new(, attributes, ) end |
.path(document) ⇒ Root
Get the path calculator for the supplied document.
228 229 230 |
# File 'lib/mongoid/relations/referenced/one.rb', line 228 def path(document) Mongoid::Atomic::Paths::Root.new(document) end |
.stores_foreign_key? ⇒ false
Tells the caller if this relation is one that stores the foreign key on its own objects.
241 242 243 |
# File 'lib/mongoid/relations/referenced/one.rb', line 241 def stores_foreign_key? false end |
.valid_options ⇒ Array<Symbol>
Get the valid options allowed with this relation.
253 254 255 |
# File 'lib/mongoid/relations/referenced/one.rb', line 253 def [ :as, :autosave, :dependent, :foreign_key ] end |
.validation_default ⇒ true, false
Get the default validation setting for the relation. Determines if by default a validates associated will occur.
266 267 268 |
# File 'lib/mongoid/relations/referenced/one.rb', line 266 def validation_default true end |
Instance Method Details
#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.
36 37 38 39 |
# File 'lib/mongoid/relations/referenced/one.rb', line 36 def nullify unbind_one target.save end |
#substitute(replacement) ⇒ One
Substitutes the supplied target document for the existing document in the relation. If the new target is nil, perform the necessary deletion.
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/mongoid/relations/referenced/one.rb', line 53 def substitute(replacement) tap do |proxy| proxy.unbind_one proxy.target.delete if persistable? return nil unless replacement proxy.target = replacement proxy.bind_one replacement.save if persistable? end end |