Class: Mongoid::Relations::Referenced::In
- Defined in:
- lib/mongoid/relations/referenced/in.rb
Overview
This class handles all behaviour for relations that are either one-to-many or one-to-one, where the foreign key is store on this side of the relation and the reference is to document(s) in another collection.
Instance Attribute Summary
Attributes inherited from Proxy
#base, #loaded, #metadata, #target
Class Method Summary collapse
-
.builder(meta, object) ⇒ Builder
Return the builder that is responsible for generating the documents that will be used by this relation.
-
.embedded? ⇒ false
Returns true if the relation is an embedded one.
-
.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.
-
.stores_foreign_key? ⇒ true
Tells the caller if this relation is one that stores the foreign key on its own objects.
Instance Method Summary collapse
-
#bind(options = {}) ⇒ Object
Binds the base object to the inverse of the relation.
-
#initialize(base, target, metadata) ⇒ In
constructor
Instantiate a new referenced_in relation.
-
#substitute(new_target, options = {}) ⇒ In?
Substitutes the supplied target documents for the existing document in the relation.
-
#unbind(old_target, options = {}) ⇒ Object
Unbinds the base object to the inverse of the relation.
Methods inherited from One
Methods inherited from Proxy
Constructor Details
#initialize(base, target, metadata) ⇒ In
Instantiate a new referenced_in relation.
42 43 44 45 46 |
# File 'lib/mongoid/relations/referenced/in.rb', line 42 def initialize(base, target, ) init(base, target, ) do characterize_one(target) 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(meta, object) ⇒ Builder
Return the builder that is responsible for generating the documents that will be used by this relation.
122 123 124 |
# File 'lib/mongoid/relations/referenced/in.rb', line 122 def builder(, object) Builders::Referenced::In.new(, object) end |
.embedded? ⇒ false
Returns true if the relation is an embedded one. In this case always false.
135 136 137 |
# File 'lib/mongoid/relations/referenced/in.rb', line 135 def false end |
.foreign_key_default ⇒ nil
Get the default value for the foreign key.
147 148 149 |
# File 'lib/mongoid/relations/referenced/in.rb', line 147 def foreign_key_default nil end |
.foreign_key_suffix ⇒ String
Returns the suffix of the foreign key field, either “_id” or “_ids”.
159 160 161 |
# File 'lib/mongoid/relations/referenced/in.rb', line 159 def foreign_key_suffix "_id" end |
.macro ⇒ Symbol
Returns the macro for this relation. Used mostly as a helper in reflection.
170 171 172 |
# File 'lib/mongoid/relations/referenced/in.rb', line 170 def macro :referenced_in 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.
196 197 198 |
# File 'lib/mongoid/relations/referenced/in.rb', line 196 def nested_builder(, attributes, ) Builders::NestedAttributes::One.new(, attributes, ) end |
.stores_foreign_key? ⇒ true
Tells the caller if this relation is one that stores the foreign key on its own objects.
209 210 211 |
# File 'lib/mongoid/relations/referenced/in.rb', line 209 def stores_foreign_key? true end |
Instance Method Details
#bind(options = {}) ⇒ Object
Binds the base object to the inverse of the relation. This is so we are referenced to the actual objects themselves and dont hit the database twice when setting the relations up.
This is called after first creating the relation, or if a new object is set on the relation.
29 30 31 |
# File 'lib/mongoid/relations/referenced/in.rb', line 29 def bind( = {}) binding.bind() end |
#substitute(new_target, options = {}) ⇒ In?
Substitutes the supplied target documents for the existing document in the relation.
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/mongoid/relations/referenced/in.rb', line 60 def substitute(new_target, = {}) old_target = target tap do |relation| relation.target = new_target if new_target bind() else unbind(old_target, ) nil end end end |
#unbind(old_target, options = {}) ⇒ Object
Unbinds the base object to the inverse of the relation. This occurs when setting a side of the relation to nil.
87 88 89 |
# File 'lib/mongoid/relations/referenced/in.rb', line 87 def unbind(old_target, = {}) binding(old_target).unbind() end |