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.
22 23 24 25 26 27 28 29 |
# File 'lib/mongoid/association/referenced/has_one/proxy.rb', line 22 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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Mongoid::Association::Proxy
Class Method Details
.eager_loader(association, docs) ⇒ Object
99 100 101 |
# File 'lib/mongoid/association/referenced/has_one/proxy.rb', line 99 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.
112 113 114 |
# File 'lib/mongoid/association/referenced/has_one/proxy.rb', line 112 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.
39 40 41 42 |
# File 'lib/mongoid/association/referenced/has_one/proxy.rb', line 39 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.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/mongoid/association/referenced/has_one/proxy.rb', line 56 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 |