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
94 95 96 |
# File 'lib/mongoid/association/referenced/has_one/proxy.rb', line 94 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.
107 108 109 |
# File 'lib/mongoid/association/referenced/has_one/proxy.rb', line 107 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 |
# File 'lib/mongoid/association/referenced/has_one/proxy.rb', line 56 def substitute(replacement) unbind_one if persistable? if _association.destructive? send(_association.dependent) else save if persisted? end end HasOne::Proxy.new(_base, replacement, _association) if replacement end |