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, loading = false) ⇒ 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(metadata, criteria) ⇒ Criteria
Get the criteria that is used to eager load a relation of this type.
-
.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.
-
.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.
-
#substitute(replacement) ⇒ In?
Substitutes the supplied target documents for the existing document in the relation.
Methods inherited from One
Methods inherited from Proxy
Constructor Details
#initialize(base, target, metadata) ⇒ In
Instantiate a new referenced_in relation.
21 22 23 24 25 26 |
# File 'lib/mongoid/relations/referenced/in.rb', line 21 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(meta, object, loading = false) ⇒ Builder
Return the builder that is responsible for generating the documents that will be used by this relation.
92 93 94 |
# File 'lib/mongoid/relations/referenced/in.rb', line 92 def builder(, object, loading = false) Builders::Referenced::In.new(, object, loading) end |
.criteria(metadata, object, type = nil) ⇒ Criteria
Get the standard criteria used for querying this relation.
108 109 110 |
# File 'lib/mongoid/relations/referenced/in.rb', line 108 def criteria(, object, type = nil) type.where(:_id => object) end |
.eager_load(metadata, criteria) ⇒ Criteria
Get the criteria that is used to eager load a relation of this type.
124 125 126 127 128 129 130 |
# File 'lib/mongoid/relations/referenced/in.rb', line 124 def eager_load(, criteria) raise Errors::EagerLoad.new(.name) if .polymorphic? klass, foreign_key = .klass, .foreign_key klass.any_in("_id" => criteria.load_ids(foreign_key).uniq).each do |doc| IdentityMap.set(doc) end end |
.embedded? ⇒ false
Returns true if the relation is an embedded one. In this case always false.
141 142 143 |
# File 'lib/mongoid/relations/referenced/in.rb', line 141 def false end |
.foreign_key_default ⇒ nil
Get the default value for the foreign key.
153 154 155 |
# File 'lib/mongoid/relations/referenced/in.rb', line 153 def foreign_key_default nil end |
.foreign_key_suffix ⇒ String
Returns the suffix of the foreign key field, either “_id” or “_ids”.
165 166 167 |
# File 'lib/mongoid/relations/referenced/in.rb', line 165 def foreign_key_suffix "_id" end |
.macro ⇒ Symbol
Returns the macro for this relation. Used mostly as a helper in reflection.
176 177 178 |
# File 'lib/mongoid/relations/referenced/in.rb', line 176 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.
202 203 204 |
# File 'lib/mongoid/relations/referenced/in.rb', line 202 def nested_builder(, attributes, ) Builders::NestedAttributes::One.new(, attributes, ) end |
.path(document) ⇒ Root
Get the path calculator for the supplied document.
216 217 218 |
# File 'lib/mongoid/relations/referenced/in.rb', line 216 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.
229 230 231 |
# File 'lib/mongoid/relations/referenced/in.rb', line 229 def stores_foreign_key? true end |
.valid_options ⇒ Array<Symbol>
Get the valid options allowed with this relation.
241 242 243 |
# File 'lib/mongoid/relations/referenced/in.rb', line 241 def [ :autosave, :foreign_key, :index, :polymorphic ] end |
.validation_default ⇒ true, false
Get the default validation setting for the relation. Determines if by default a validates associated will occur.
254 255 256 |
# File 'lib/mongoid/relations/referenced/in.rb', line 254 def validation_default false end |
Instance Method Details
#substitute(replacement) ⇒ In?
Substitutes the supplied target documents for the existing document in the relation.
40 41 42 43 44 45 46 47 |
# File 'lib/mongoid/relations/referenced/in.rb', line 40 def substitute(replacement) tap do |proxy| proxy.unbind_one return nil unless replacement proxy.target = replacement proxy.bind_one end end |