Class: Mongoid::Relations::Referenced::Many
- Includes:
- Batch
- Defined in:
- lib/mongoid/relations/referenced/many.rb
Overview
This class defines the behaviour for all relations that are a one-to-many between documents in different collections.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Proxy
#base, #loaded, #metadata, #target
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(metadata, criteria) ⇒ Criteria
Eager load the relation based on the criteria.
-
.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? ⇒ false
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
-
#<<(*args) ⇒ Array<Document>
(also: #concat, #push)
Appends a document or array of documents to the relation.
-
#build(attributes = {}, options = {}, type = nil) ⇒ Document
(also: #new)
Build a new document from the attributes and append it to this relation without saving.
-
#create(attributes = nil, options = {}, type = nil, &block) ⇒ Document
Creates a new document on the references many relation.
-
#create!(attributes = nil, options = {}, type = nil, &block) ⇒ Document
Creates a new document on the references many relation.
-
#delete(document) ⇒ Document
Delete the document from the relation.
-
#delete_all(conditions = nil) ⇒ Integer
Deletes all related documents from the database given the supplied conditions.
-
#destroy_all(conditions = nil) ⇒ Integer
Destroys all related documents from the database given the supplied conditions.
-
#each ⇒ Array<Document>
Iterate over each document in the relation and yield to the provided block.
-
#find(*args) ⇒ Document, Criteria
Find the matchind document on the association, either based on id or conditions.
-
#initialize(base, target, metadata) ⇒ Many
constructor
Instantiate a new references_many relation.
-
#nullify ⇒ Object
(also: #nullify_all)
Removes all associations between the base document and the target documents by deleting the foreign keys and the references, orphaning the target documents in the process.
-
#purge ⇒ Many
(also: #clear)
Clear the relation.
-
#substitute(replacement) ⇒ Many
Substitutes the supplied target documents for the existing documents in the relation.
Methods inherited from Many
#blank?, #exists?, #find_or_create_by, #find_or_initialize_by, #nil?, #respond_to?, #scoped, #serializable_hash
Methods inherited from Proxy
Constructor Details
#initialize(base, target, metadata) ⇒ Many
Instantiate a new references_many relation. Will set the foreign key and the base on the inverse object.
238 239 240 241 242 |
# File 'lib/mongoid/relations/referenced/many.rb', line 238 def initialize(base, target, ) init(base, Targets::Enumerable.new(target), ) do raise_mixed if klass. end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Criteria, Object (private)
If the target array does not respond to the supplied method then try to find a named scope or criteria on the class and send the call there.
If the method exists on the array, use the default proxy behavior.
392 393 394 395 396 397 398 399 400 |
# File 'lib/mongoid/relations/referenced/many.rb', line 392 def method_missing(name, *args, &block) if target.respond_to?(name) target.send(name, *args, &block) else klass.send(:with_scope, criteria) do criteria.send(name, *args, &block) end end end |
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.
456 457 458 |
# File 'lib/mongoid/relations/referenced/many.rb', line 456 def builder(base, , object) Builders::Referenced::Many.new(base, , object || []) end |
.criteria(metadata, object, type = nil) ⇒ Criteria
Get the standard criteria used for querying this relation.
472 473 474 |
# File 'lib/mongoid/relations/referenced/many.rb', line 472 def criteria(, object, type = nil) .klass.where(.foreign_key => object) end |
.eager_load(metadata, criteria) ⇒ Criteria
Eager load the relation based on the criteria.
487 488 489 490 491 492 |
# File 'lib/mongoid/relations/referenced/many.rb', line 487 def eager_load(, criteria) klass, foreign_key = .klass, .foreign_key klass.any_in(foreign_key => criteria.load_ids("_id").uniq).each do |doc| IdentityMap.set_many(doc, foreign_key => doc.send(foreign_key)) end end |
.embedded? ⇒ false
Returns true if the relation is an embedded one. In this case always false.
503 504 505 |
# File 'lib/mongoid/relations/referenced/many.rb', line 503 def false end |
.foreign_key_default ⇒ nil
Get the default value for the foreign key.
515 516 517 |
# File 'lib/mongoid/relations/referenced/many.rb', line 515 def foreign_key_default nil end |
.foreign_key_suffix ⇒ String
Returns the suffix of the foreign key field, either “_id” or “_ids”.
527 528 529 |
# File 'lib/mongoid/relations/referenced/many.rb', line 527 def foreign_key_suffix "_id" end |
.macro ⇒ Symbol
Returns the macro for this relation. Used mostly as a helper in reflection.
538 539 540 |
# File 'lib/mongoid/relations/referenced/many.rb', line 538 def macro :references_many 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.
564 565 566 |
# File 'lib/mongoid/relations/referenced/many.rb', line 564 def nested_builder(, attributes, ) Builders::NestedAttributes::Many.new(, attributes, ) end |
.path(document) ⇒ Root
Get the path calculator for the supplied document.
578 579 580 |
# File 'lib/mongoid/relations/referenced/many.rb', line 578 def path(document) Mongoid::Atomic::Paths::Root.new(document) end |
.stores_foreign_key? ⇒ false
Tells the caller if this relation is one that stores the foreign key on its own objects.
591 592 593 |
# File 'lib/mongoid/relations/referenced/many.rb', line 591 def stores_foreign_key? false end |
.valid_options ⇒ Array<Symbol>
Get the valid options allowed with this relation.
603 604 605 |
# File 'lib/mongoid/relations/referenced/many.rb', line 603 def [ :as, :autosave, :dependent, :foreign_key, :order ] end |
.validation_default ⇒ true, false
Get the default validation setting for the relation. Determines if by default a validates associated will occur.
616 617 618 |
# File 'lib/mongoid/relations/referenced/many.rb', line 616 def validation_default true end |
Instance Method Details
#<<(*args) ⇒ Array<Document> Also known as: concat, push
Appends a document or array of documents to the relation. Will set the parent and update the index in the process.
31 32 33 34 35 36 37 38 39 |
# File 'lib/mongoid/relations/referenced/many.rb', line 31 def <<(*args) batched do args.flatten.each do |doc| next unless doc append(doc) doc.save if persistable? && !doc.validated? end end end |
#build(attributes = {}, options = {}, type = nil) ⇒ Document #build(attributes = {}, type = nil) ⇒ Document Also known as: new
Build a new document from the attributes and append it to this relation without saving.
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/mongoid/relations/referenced/many.rb', line 61 def build(attributes = {}, = {}, type = nil) if .is_a? Class , type = {}, end Factory.build(type || klass, attributes, ).tap do |doc| append(doc) yield(doc) if block_given? end end |
#create(attributes = nil, options = {}, type = nil) ⇒ Document #create(attributes = nil, type = nil) ⇒ Document
Creates a new document on the references many relation. This will save the document if the parent has been persisted.
91 92 93 94 95 |
# File 'lib/mongoid/relations/referenced/many.rb', line 91 def create(attributes = nil, = {}, type = nil, &block) build(attributes, , type, &block).tap do |doc| base.persisted? ? doc.save : raise_unsaved(doc) end end |
#create!(attributes = nil, options = {}, type = nil) ⇒ Document #create!(attributes = nil, type = nil) ⇒ Document
Creates a new document on the references many relation. This will save the document if the parent has been persisted and will raise an error if validation fails.
118 119 120 121 122 |
# File 'lib/mongoid/relations/referenced/many.rb', line 118 def create!(attributes = nil, = {}, type = nil, &block) build(attributes, , type, &block).tap do |doc| base.persisted? ? doc.save! : raise_unsaved(doc) end end |
#delete(document) ⇒ Document
Delete the document from the relation. This will set the foreign key on the document to nil. If the dependent options on the relation are :delete or :destroy the appropriate removal will occur.
136 137 138 139 140 141 142 143 |
# File 'lib/mongoid/relations/referenced/many.rb', line 136 def delete(document) target.delete(document) do |doc| if doc unbind_one(doc) cascade!(doc) end end end |
#delete_all(conditions = nil) ⇒ Integer
Deletes all related documents from the database given the supplied conditions.
159 160 161 |
# File 'lib/mongoid/relations/referenced/many.rb', line 159 def delete_all(conditions = nil) remove_all(conditions, :delete_all) end |
#destroy_all(conditions = nil) ⇒ Integer
Destroys all related documents from the database given the supplied conditions.
177 178 179 |
# File 'lib/mongoid/relations/referenced/many.rb', line 177 def destroy_all(conditions = nil) remove_all(conditions, :destroy_all) end |
#each ⇒ Array<Document>
This will load the entire relation into memory.
Iterate over each document in the relation and yield to the provided block.
194 195 196 |
# File 'lib/mongoid/relations/referenced/many.rb', line 194 def each target.each { |doc| yield(doc) if block_given? } end |
#find(*args) ⇒ Document, Criteria
Find the matchind document on the association, either based on id or conditions.
223 224 225 |
# File 'lib/mongoid/relations/referenced/many.rb', line 223 def find(*args) criteria.find(*args) end |
#nullify ⇒ Object Also known as: nullify_all
Removes all associations between the base document and the target documents by deleting the foreign keys and the references, orphaning the target documents in the process.
252 253 254 255 256 257 |
# File 'lib/mongoid/relations/referenced/many.rb', line 252 def nullify criteria.update(.foreign_key => nil) target.clear do |doc| unbind_one(doc) end end |
#purge ⇒ Many Also known as: clear
Clear the relation. Will delete the documents from the db if they are already persisted.
269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/mongoid/relations/referenced/many.rb', line 269 def purge unless .destructive? nullify else criteria.delete_all target.clear do |doc| unbind_one(doc) doc.destroyed = true end end end |
#substitute(replacement) ⇒ Many
Substitutes the supplied target documents for the existing documents in the relation. If the new target is nil, perform the necessary deletion.
294 295 296 297 298 299 300 301 |
# File 'lib/mongoid/relations/referenced/many.rb', line 294 def substitute(replacement) tap do |proxy| if replacement != proxy.in_memory proxy.purge proxy.push(replacement.compact.uniq) if replacement end end end |