Class: Mongoid::Association::Referenced::HasOne::Proxy
- Defined in:
- lib/mongoid/association/referenced/has_one/proxy.rb
Overview
This class defines the behavior for all associations that are a one-to-one between documents in different collections.
Instance Attribute Summary
Attributes inherited from Proxy
#_association, #_base, #_target
Class Method Summary collapse
- .eager_loader(association, docs) ⇒ Object
-
.embedded? ⇒ false
Returns true if the association is an embedded one.
Instance Method Summary collapse
-
#initialize(base, target, association) ⇒ Proxy
constructor
Instantiate a new references_one association.
-
#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 association.
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, association) ⇒ Proxy
Instantiate a new references_one association. Will set the foreign key and the base on the inverse object.
21 22 23 24 25 26 27 28 |
# File 'lib/mongoid/association/referenced/has_one/proxy.rb', line 21 def initialize(base, target, association) init(base, target, association) do raise_mixed if klass. && !klass.cyclic? characterize_one(_target) bind_one _target.save if persistable? end end |
Class Method Details
.eager_loader(association, docs) ⇒ Object
92 93 94 |
# File 'lib/mongoid/association/referenced/has_one/proxy.rb', line 92 def eager_loader(association, docs) Eager.new(association, docs) end |
.embedded? ⇒ false
Returns true if the association is an embedded one. In this case always false.
103 104 105 |
# File 'lib/mongoid/association/referenced/has_one/proxy.rb', line 103 def false 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/association/referenced/has_one/proxy.rb', line 36 def nullify unbind_one _target.save end |
#substitute(replacement) ⇒ One
Substitutes the supplied target document for the existing document in the association. If the new target is nil, perform the necessary deletion.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mongoid/association/referenced/has_one/proxy.rb', line 51 def substitute(replacement) # If the same object currently associated is being assigned, # rebind the association and save the target but do not destroy # the target. unbind_one if persistable? # TODO can this entire method be skipped if self == replacement? if _association.destructive? && self != replacement send(_association.dependent) else save if persisted? end end HasOne::Proxy.new(_base, replacement, _association) if replacement end |