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.
Constant Summary collapse
- VALID_OPTIONS =
The allowed options when defining this relation.
[ :as, :autobuild, :autosave, :dependent, :foreign_key, :primary_key ].freeze
Instance Attribute Summary
Attributes inherited from Proxy
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_klass ⇒ Object
-
.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
#__evolve_object_id__, #clear, #in_memory, #respond_to?
Methods inherited from Proxy
apply_ordering, #extend_proxies, #init, #klass, #reset_unloaded, #substitutable
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.
33 34 35 36 37 38 39 40 |
# File 'lib/mongoid/relations/referenced/one.rb', line 33 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.
121 122 123 |
# File 'lib/mongoid/relations/referenced/one.rb', line 121 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.
137 138 139 140 141 142 143 |
# File 'lib/mongoid/relations/referenced/one.rb', line 137 def criteria(, object, type = nil) crit = .klass.where(.foreign_key => object) if .polymorphic? crit = crit.where(.type => type.name) end crit end |
.eager_load_klass ⇒ Object
145 146 147 |
# File 'lib/mongoid/relations/referenced/one.rb', line 145 def eager_load_klass Relations::Eager::HasOne end |
.embedded? ⇒ false
Returns true if the relation is an embedded one. In this case always false.
158 159 160 |
# File 'lib/mongoid/relations/referenced/one.rb', line 158 def false end |
.foreign_key(name) ⇒ String
Get the foreign key for the provided name.
172 173 174 |
# File 'lib/mongoid/relations/referenced/one.rb', line 172 def foreign_key(name) "#{name}#{foreign_key_suffix}" end |
.foreign_key_default ⇒ nil
Get the default value for the foreign key.
184 185 186 |
# File 'lib/mongoid/relations/referenced/one.rb', line 184 def foreign_key_default nil end |
.foreign_key_suffix ⇒ String
Returns the suffix of the foreign key field, either “_id” or “_ids”.
196 197 198 |
# File 'lib/mongoid/relations/referenced/one.rb', line 196 def foreign_key_suffix "_id" end |
.macro ⇒ Symbol
Returns the macro for this relation. Used mostly as a helper in reflection.
207 208 209 |
# File 'lib/mongoid/relations/referenced/one.rb', line 207 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.
233 234 235 |
# File 'lib/mongoid/relations/referenced/one.rb', line 233 def nested_builder(, attributes, ) Builders::NestedAttributes::One.new(, attributes, ) end |
.path(document) ⇒ Root
Get the path calculator for the supplied document.
247 248 249 |
# File 'lib/mongoid/relations/referenced/one.rb', line 247 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.
260 261 262 |
# File 'lib/mongoid/relations/referenced/one.rb', line 260 def stores_foreign_key? false end |
.valid_options ⇒ Array<Symbol>
Get the valid options allowed with this relation.
272 273 274 |
# File 'lib/mongoid/relations/referenced/one.rb', line 272 def VALID_OPTIONS end |
.validation_default ⇒ true, false
Get the default validation setting for the relation. Determines if by default a validates associated will occur.
285 286 287 |
# File 'lib/mongoid/relations/referenced/one.rb', line 285 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.
50 51 52 53 |
# File 'lib/mongoid/relations/referenced/one.rb', line 50 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.
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/mongoid/relations/referenced/one.rb', line 67 def substitute(replacement) unbind_one if persistable? if .destructive? send(.dependent) else save if persisted? end end One.new(base, replacement, ) if replacement end |