Class: Mongoid::Relations::Referenced::Many
- 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, ids) ⇒ Criteria
Eager load the relation based on the criteria.
-
.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? ⇒ 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: #push)
Appends a document or array of documents to the relation.
-
#build(attributes = {}, options = {}, type = nil) {|doc| ... } ⇒ Document
(also: #new)
Build a new document from the attributes and append it to this relation without saving.
-
#concat(documents) ⇒ Array<Document>
Appends an array of documents to the 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.
-
#exists? ⇒ true, false
Determine if any documents in this relation exist in the database.
-
#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.
-
#unscoped ⇒ Criteria
Get a criteria for the documents without the default scoping applied.
Methods inherited from Many
#blank?, #create, #create!, #find_or_create_by, #find_or_initialize_by, #nil?, #respond_to?, #scoped, #serializable_hash
Methods inherited from Proxy
apply_ordering, eager_load_ids, #init, #klass, #reset_unloaded, #substitutable, #with
Methods included from Marshalable
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.
219 220 221 222 223 |
# File 'lib/mongoid/relations/referenced/many.rb', line 219 def initialize(base, target, ) init(base, Targets::Enumerable.new(target), ) do raise_mixed if klass. && !klass.cyclic? 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.
410 411 412 413 414 415 416 417 418 |
# File 'lib/mongoid/relations/referenced/many.rb', line 410 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.
545 546 547 |
# File 'lib/mongoid/relations/referenced/many.rb', line 545 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.
561 562 563 564 565 566 567 568 569 570 571 572 |
# File 'lib/mongoid/relations/referenced/many.rb', line 561 def criteria(, object, type = nil) apply_ordering( with_inverse_field_criterion( with_polymorphic_criterion( .klass.where(.foreign_key => object), , type ), ), ) end |
.eager_load(metadata, ids) ⇒ Criteria
Eager load the relation based on the criteria.
585 586 587 |
# File 'lib/mongoid/relations/referenced/many.rb', line 585 def eager_load(, ids) eager_load_ids(, ids) { |doc, key| IdentityMap.set_many(doc, key) } end |
.embedded? ⇒ false
Returns true if the relation is an embedded one. In this case always false.
598 599 600 |
# File 'lib/mongoid/relations/referenced/many.rb', line 598 def false end |
.foreign_key(name) ⇒ String
Get the foreign key for the provided name.
612 613 614 |
# File 'lib/mongoid/relations/referenced/many.rb', line 612 def foreign_key(name) "#{name}#{foreign_key_suffix}" end |
.foreign_key_default ⇒ nil
Get the default value for the foreign key.
624 625 626 |
# File 'lib/mongoid/relations/referenced/many.rb', line 624 def foreign_key_default nil end |
.foreign_key_suffix ⇒ String
Returns the suffix of the foreign key field, either “_id” or “_ids”.
636 637 638 |
# File 'lib/mongoid/relations/referenced/many.rb', line 636 def foreign_key_suffix "_id" end |
.macro ⇒ Symbol
Returns the macro for this relation. Used mostly as a helper in reflection.
647 648 649 |
# File 'lib/mongoid/relations/referenced/many.rb', line 647 def macro :has_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.
673 674 675 |
# File 'lib/mongoid/relations/referenced/many.rb', line 673 def nested_builder(, attributes, ) Builders::NestedAttributes::Many.new(, attributes, ) end |
.path(document) ⇒ Root
Get the path calculator for the supplied document.
687 688 689 |
# File 'lib/mongoid/relations/referenced/many.rb', line 687 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.
700 701 702 |
# File 'lib/mongoid/relations/referenced/many.rb', line 700 def stores_foreign_key? false end |
.valid_options ⇒ Array<Symbol>
Get the valid options allowed with this relation.
712 713 714 715 716 717 718 719 720 721 722 723 724 725 |
# File 'lib/mongoid/relations/referenced/many.rb', line 712 def [ :after_add, :after_remove, :as, :autosave, :before_add, :before_remove, :dependent, :foreign_key, :order, :primary_key ] end |
.validation_default ⇒ true, false
Get the default validation setting for the relation. Determines if by default a validates associated will occur.
736 737 738 |
# File 'lib/mongoid/relations/referenced/many.rb', line 736 def validation_default true end |
Instance Method Details
#<<(*args) ⇒ Array<Document> Also known as: push
Appends a document or array of documents to the relation. Will set the parent and update the index in the process.
30 31 32 33 34 35 36 37 38 |
# File 'lib/mongoid/relations/referenced/many.rb', line 30 def <<(*args) docs = args.flatten return concat(docs) if docs.size > 1 if doc = docs.first append(doc) doc.save if persistable? && !_assigning? && !doc.validated? end self 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.
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/mongoid/relations/referenced/many.rb', line 81 def build(attributes = {}, = {}, type = nil) if .is_a? Class , type = {}, end doc = Factory.build(type || klass, attributes, ) append(doc) doc.apply_post_processed_defaults yield(doc) if block_given? doc.run_callbacks(:build) { doc } doc end |
#concat(documents) ⇒ Array<Document>
Appends an array of documents to the relation. Performs a batch insert of the documents instead of persisting one at a time.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mongoid/relations/referenced/many.rb', line 52 def concat(documents) docs, inserts = [], [] documents.each do |doc| next unless doc append(doc) save_or_delay(doc, docs, inserts) if persistable? end persist_delayed(docs, inserts) self 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.
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/mongoid/relations/referenced/many.rb', line 107 def delete(document) execute_callback :before_remove, document target.delete(document) do |doc| if doc unbind_one(doc) cascade!(doc) if !_assigning? end execute_callback :after_remove, doc end end |
#delete_all(conditions = nil) ⇒ Integer
Deletes all related documents from the database given the supplied conditions.
132 133 134 |
# File 'lib/mongoid/relations/referenced/many.rb', line 132 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.
150 151 152 |
# File 'lib/mongoid/relations/referenced/many.rb', line 150 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.
167 168 169 170 171 172 173 |
# File 'lib/mongoid/relations/referenced/many.rb', line 167 def each if block_given? target.each { |doc| yield(doc) } else to_enum end end |
#exists? ⇒ true, false
Determine if any documents in this relation exist in the database.
181 182 183 |
# File 'lib/mongoid/relations/referenced/many.rb', line 181 def exists? criteria.exists? end |
#find(*args) ⇒ Document, Criteria
This will keep matching documents in memory for iteration later.
Find the matchind document on the association, either based on id or conditions.
202 203 204 205 206 |
# File 'lib/mongoid/relations/referenced/many.rb', line 202 def find(*args) matching = criteria.find(*args) Array(matching).each { |doc| target.push(doc) } matching 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.
233 234 235 236 237 238 239 |
# File 'lib/mongoid/relations/referenced/many.rb', line 233 def nullify criteria.update_all(foreign_key => nil) target.clear do |doc| unbind_one(doc) doc.changed_attributes.delete(foreign_key) end end |
#purge ⇒ Many Also known as: clear
Clear the relation. Will delete the documents from the db if they are already persisted.
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/mongoid/relations/referenced/many.rb', line 251 def purge unless .destructive? nullify else after_remove_error = nil criteria.delete_all many = target.clear do |doc| execute_callback :before_remove, doc unbind_one(doc) doc.destroyed = true begin execute_callback :after_remove, doc rescue => e after_remove_error = e end end raise after_remove_error if after_remove_error many 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.
285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/mongoid/relations/referenced/many.rb', line 285 def substitute(replacement) if replacement new_docs, docs = replacement.compact, [] new_ids = new_docs.map { |doc| doc.id } remove_not_in(new_ids) new_docs.each do |doc| docs.push(doc) if doc.send(foreign_key) != base.id end concat(docs) else purge end self end |
#unscoped ⇒ Criteria
Get a criteria for the documents without the default scoping applied.
309 310 311 312 313 |
# File 'lib/mongoid/relations/referenced/many.rb', line 309 def unscoped klass.unscoped.where( foreign_key => Conversions.flag(base.id, ) ) end |