Class: Mongoid::Relations::Referenced::In
- Includes:
- Evolvable
- 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.
Constant Summary collapse
- VALID_OPTIONS =
The allowed options when defining this relation.
[ :autobuild, :autosave, :dependent, :foreign_key, :index, :polymorphic, :primary_key, :touch, :optional, :required ].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? ⇒ true
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) ⇒ In
constructor
Instantiate a new referenced_in 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) ⇒ In?
Substitutes the supplied target documents for the existing document in the relation.
Methods included from Evolvable
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) ⇒ In
Instantiate a new referenced_in relation.
40 41 42 43 44 45 |
# File 'lib/mongoid/relations/referenced/in.rb', line 40 def initialize(base, target, ) init(base, target, ) do characterize_one(target) bind_one 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.
140 141 142 |
# File 'lib/mongoid/relations/referenced/in.rb', line 140 def builder(base, , object) Builders::Referenced::In.new(base, , object) end |
.criteria(metadata, object, type = nil) ⇒ Criteria
Get the standard criteria used for querying this relation.
156 157 158 |
# File 'lib/mongoid/relations/referenced/in.rb', line 156 def criteria(, object, type = nil) type.where(.primary_key => object) end |
.eager_load_klass ⇒ Object
160 161 162 |
# File 'lib/mongoid/relations/referenced/in.rb', line 160 def eager_load_klass Relations::Eager::BelongsTo end |
.embedded? ⇒ false
Returns true if the relation is an embedded one. In this case always false.
173 174 175 |
# File 'lib/mongoid/relations/referenced/in.rb', line 173 def false end |
.foreign_key(name) ⇒ String
Get the foreign key for the provided name.
187 188 189 |
# File 'lib/mongoid/relations/referenced/in.rb', line 187 def foreign_key(name) "#{name}#{foreign_key_suffix}" end |
.foreign_key_default ⇒ nil
Get the default value for the foreign key.
199 200 201 |
# File 'lib/mongoid/relations/referenced/in.rb', line 199 def foreign_key_default nil end |
.foreign_key_suffix ⇒ String
Returns the suffix of the foreign key field, either “_id” or “_ids”.
211 212 213 |
# File 'lib/mongoid/relations/referenced/in.rb', line 211 def foreign_key_suffix "_id" end |
.macro ⇒ Symbol
Returns the macro for this relation. Used mostly as a helper in reflection.
222 223 224 |
# File 'lib/mongoid/relations/referenced/in.rb', line 222 def macro :belongs_to 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.
248 249 250 |
# File 'lib/mongoid/relations/referenced/in.rb', line 248 def nested_builder(, attributes, ) Builders::NestedAttributes::One.new(, attributes, ) end |
.path(document) ⇒ Root
Get the path calculator for the supplied document.
262 263 264 |
# File 'lib/mongoid/relations/referenced/in.rb', line 262 def path(document) Mongoid::Atomic::Paths::Root.new(document) end |
.stores_foreign_key? ⇒ true
Tells the caller if this relation is one that stores the foreign key on its own objects.
275 276 277 |
# File 'lib/mongoid/relations/referenced/in.rb', line 275 def stores_foreign_key? true end |
.valid_options ⇒ Array<Symbol>
Get the valid options allowed with this relation.
287 288 289 |
# File 'lib/mongoid/relations/referenced/in.rb', line 287 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.
300 301 302 |
# File 'lib/mongoid/relations/referenced/in.rb', line 300 def validation_default 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.
54 55 56 57 |
# File 'lib/mongoid/relations/referenced/in.rb', line 54 def nullify unbind_one target.save end |
#substitute(replacement) ⇒ In?
Substitutes the supplied target documents for the existing document in the relation.
71 72 73 74 75 76 77 |
# File 'lib/mongoid/relations/referenced/in.rb', line 71 def substitute(replacement) unbind_one return nil unless replacement self.target = normalize(replacement) bind_one self end |