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(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? ⇒ false
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.
-
#clear(options = {}) ⇒ Many
Clear the relation.
-
#count ⇒ Integer
Returns a count of the number of documents in the association that have actually been persisted to the database.
-
#create(attributes = nil, type = nil, &block) ⇒ Document
Creates a new document on the references many relation.
-
#create!(attributes = nil, type = nil, &block) ⇒ Document
Creates a new document on the references many 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.
-
#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.
-
#load!(options = {}) ⇒ Many
Will load the target into an array if the target had not already been loaded.
-
#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.
-
#substitute(target, options = {}) ⇒ Many
Substitutes the supplied target documents for the existing documents in the relation.
-
#unbind(options = {}) ⇒ Object
Unbinds the base object to the inverse of the relation.
Methods inherited from Many
#<<, #build, #exists?, #find_or_create_by, #find_or_initialize_by, #nil?, #respond_to?, #serializable_hash, #size
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.
175 176 177 |
# File 'lib/mongoid/relations/referenced/many.rb', line 175 def initialize(base, target, ) init(base, target, ) 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.
340 341 342 343 344 345 346 |
# File 'lib/mongoid/relations/referenced/many.rb', line 340 def method_missing(name, *args, &block) load!(:binding => true) and return super if [].respond_to?(name) klass = .klass klass.send(:with_scope, criteria) do criteria.send(name, *args) end end |
Class Method Details
.builder(meta, object) ⇒ Builder
Return the builder that is responsible for generating the documents that will be used by this relation.
363 364 365 |
# File 'lib/mongoid/relations/referenced/many.rb', line 363 def builder(, object) Builders::Referenced::Many.new(, object || []) end |
.embedded? ⇒ false
Returns true if the relation is an embedded one. In this case always false.
376 377 378 |
# File 'lib/mongoid/relations/referenced/many.rb', line 376 def false end |
.foreign_key_default ⇒ nil
Get the default value for the foreign key.
388 389 390 |
# File 'lib/mongoid/relations/referenced/many.rb', line 388 def foreign_key_default nil end |
.foreign_key_suffix ⇒ String
Returns the suffix of the foreign key field, either “_id” or “_ids”.
400 401 402 |
# File 'lib/mongoid/relations/referenced/many.rb', line 400 def foreign_key_suffix "_id" end |
.macro ⇒ Symbol
Returns the macro for this relation. Used mostly as a helper in reflection.
411 412 413 |
# File 'lib/mongoid/relations/referenced/many.rb', line 411 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.
437 438 439 |
# File 'lib/mongoid/relations/referenced/many.rb', line 437 def nested_builder(, attributes, ) Builders::NestedAttributes::Many.new(, attributes, ) end |
.stores_foreign_key? ⇒ false
Tells the caller if this relation is one that stores the foreign key on its own objects.
450 451 452 |
# File 'lib/mongoid/relations/referenced/many.rb', line 450 def stores_foreign_key? false 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.
27 28 29 30 |
# File 'lib/mongoid/relations/referenced/many.rb', line 27 def bind( = {}) binding.bind() target.map(&:save) if base.persisted? && ![:binding] end |
#clear(options = {}) ⇒ Many
Clear the relation. Will delete the documents from the db if they are already persisted.
42 43 44 45 46 47 |
# File 'lib/mongoid/relations/referenced/many.rb', line 42 def clear( = {}) load! and tap do |relation| relation.unbind(()) target.clear end end |
#count ⇒ Integer
Returns a count of the number of documents in the association that have actually been persisted to the database.
Use #size if you want the total number of documents in memory.
58 59 60 |
# File 'lib/mongoid/relations/referenced/many.rb', line 58 def count criteria.count end |
#create(attributes = nil, type = nil, &block) ⇒ Document
Creates a new document on the references many relation. This will save the document if the parent has been persisted.
72 73 74 75 76 |
# File 'lib/mongoid/relations/referenced/many.rb', line 72 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, type = nil, &block) ⇒ 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.
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 |
#delete_all(conditions = nil) ⇒ Integer
Deletes all related documents from the database given the supplied conditions.
109 110 111 112 113 114 115 116 |
# File 'lib/mongoid/relations/referenced/many.rb', line 109 def delete_all(conditions = nil) raise_mixed if klass. selector = (conditions || {})[:conditions] || {} target.delete_if { |doc| doc.matches?(selector) } .klass.delete_all( :conditions => criteria.selector.merge(selector) ) end |
#destroy_all(conditions = nil) ⇒ Integer
Destroys all related documents from the database given the supplied conditions.
130 131 132 133 134 135 136 137 |
# File 'lib/mongoid/relations/referenced/many.rb', line 130 def destroy_all(conditions = nil) raise_mixed if klass. selector = (conditions || {})[:conditions] || {} target.delete_if { |doc| doc.matches?(selector) } .klass.destroy_all( :conditions => criteria.selector.merge(selector) ) end |
#find(*args) ⇒ Document, Criteria
Find the matchind document on the association, either based on id or conditions.
162 163 164 |
# File 'lib/mongoid/relations/referenced/many.rb', line 162 def find(*args) criteria.find(*args) end |
#load!(options = {}) ⇒ Many
Will load the target into an array if the target had not already been loaded.
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/mongoid/relations/referenced/many.rb', line 188 def load!( = {}) raise_mixed if klass. tap do |relation| unless relation.loaded? relation.target = target.entries relation.bind() relation.loaded = true end end 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.
207 208 209 |
# File 'lib/mongoid/relations/referenced/many.rb', line 207 def nullify clear(:binding => true, :continue => true, :nullify => true) end |
#substitute(target, options = {}) ⇒ Many
Substitutes the supplied target documents for the existing documents in the relation. If the new target is nil, perform the necessary deletion.
229 230 231 |
# File 'lib/mongoid/relations/referenced/many.rb', line 229 def substitute(target, = {}) tap { target ? (@target = target.to_a; bind()) : (@target = unbind()) } end |
#unbind(options = {}) ⇒ Object
Unbinds the base object to the inverse of the relation. This occurs when setting a side of the relation to nil.
Will delete the object if necessary.
248 249 250 251 252 253 254 255 |
# File 'lib/mongoid/relations/referenced/many.rb', line 248 def unbind( = {}) binding.unbind() unless base.new_record? target.each(&:delete) unless [:binding] target.each(&:save) if [:nullify] end [] end |