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(base, meta, object) ⇒ 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, ids) ⇒ 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(name) ⇒ String
Get the foreign key for the provided name.
-
.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
#clear, #in_memory, #respond_to?
Methods inherited from Proxy
apply_ordering, eager_load_ids, #extend_proxies, #init, #klass, #reset_unloaded, #substitutable, #with
Methods included from Marshalable
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. && !klass.cyclic? 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(base, meta, object) ⇒ Builder
Return the builder that is responsible for generating the documents that will be used by this relation.
107 108 109 |
# File 'lib/mongoid/relations/referenced/one.rb', line 107 def builder(base, , object) Builders::Referenced::One.new(base, , object) end |
.criteria(metadata, object, type = nil) ⇒ Criteria
Get the standard criteria used for querying this relation.
123 124 125 126 127 128 129 |
# File 'lib/mongoid/relations/referenced/one.rb', line 123 def criteria(, object, type = nil) crit = .klass.where(.foreign_key => object) if .polymorphic? crit = crit.where(.type => type.name) end crit end |
.eager_load(metadata, ids) ⇒ Criteria
Get the criteria that is used to eager load a relation of this type.
143 144 145 |
# File 'lib/mongoid/relations/referenced/one.rb', line 143 def eager_load(, ids) eager_load_ids(, ids) { |doc, key| IdentityMap.set_one(doc, key) } end |
.embedded? ⇒ false
Returns true if the relation is an embedded one. In this case always false.
156 157 158 |
# File 'lib/mongoid/relations/referenced/one.rb', line 156 def false end |
.foreign_key(name) ⇒ String
Get the foreign key for the provided name.
170 171 172 |
# File 'lib/mongoid/relations/referenced/one.rb', line 170 def foreign_key(name) "#{name}#{foreign_key_suffix}" end |
.foreign_key_default ⇒ nil
Get the default value for the foreign key.
182 183 184 |
# File 'lib/mongoid/relations/referenced/one.rb', line 182 def foreign_key_default nil end |
.foreign_key_suffix ⇒ String
Returns the suffix of the foreign key field, either “_id” or “_ids”.
194 195 196 |
# File 'lib/mongoid/relations/referenced/one.rb', line 194 def foreign_key_suffix "_id" end |
.macro ⇒ Symbol
Returns the macro for this relation. Used mostly as a helper in reflection.
205 206 207 |
# File 'lib/mongoid/relations/referenced/one.rb', line 205 def macro :has_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.
231 232 233 |
# File 'lib/mongoid/relations/referenced/one.rb', line 231 def nested_builder(, attributes, ) Builders::NestedAttributes::One.new(, attributes, ) end |
.path(document) ⇒ Root
Get the path calculator for the supplied document.
245 246 247 |
# File 'lib/mongoid/relations/referenced/one.rb', line 245 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.
258 259 260 |
# File 'lib/mongoid/relations/referenced/one.rb', line 258 def stores_foreign_key? false end |
.valid_options ⇒ Array<Symbol>
Get the valid options allowed with this relation.
270 271 272 |
# File 'lib/mongoid/relations/referenced/one.rb', line 270 def [ :as, :autobuild, :autosave, :dependent, :foreign_key, :primary_key ] end |
.validation_default ⇒ true, false
Get the default validation setting for the relation. Determines if by default a validates associated will occur.
283 284 285 |
# File 'lib/mongoid/relations/referenced/one.rb', line 283 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 63 |
# File 'lib/mongoid/relations/referenced/one.rb', line 53 def substitute(replacement) unbind_one if persistable? if .destructive? send(.dependent) else save if persisted? end end One.new(base, replacement, ) if replacement end |