Class: Mongoid::Relations::Referenced::ManyToMany
- Defined in:
- lib/mongoid/relations/referenced/many_to_many.rb
Overview
This class defines the behaviour for all relations that are a many-to-many between documents in different collections.
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
Create the standard criteria for this relation given the supplied metadata and object.
-
.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 ⇒ Array
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
-
#<<(*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.
-
#delete(document) ⇒ Document
Delete the document from the relation.
-
#nullify ⇒ Object
(also: #nullify_all, #clear, #purge)
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(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
#create, #create!, #delete_all, #destroy_all, #each, #find, #initialize
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
This class inherits a constructor from Mongoid::Relations::Referenced::Many
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Mongoid::Relations::Referenced::Many
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.
221 222 223 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 221 def builder(base, , object) Builders::Referenced::ManyToMany.new(base, , object) end |
.criteria(metadata, object, type = nil) ⇒ Criteria
Create the standard criteria for this relation given the supplied metadata and object.
238 239 240 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 238 def criteria(, object, type = nil) .klass.any_in(:_id => object) end |
.eager_load(metadata, criteria) ⇒ Criteria
Get the criteria that is used to eager load a relation of this type.
254 255 256 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 254 def eager_load(, criteria) raise Errors::EagerLoad.new(.name) end |
.embedded? ⇒ false
Returns true if the relation is an embedded one. In this case always false.
267 268 269 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 267 def false end |
.foreign_key_default ⇒ Array
Get the default value for the foreign key.
279 280 281 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 279 def foreign_key_default [] end |
.foreign_key_suffix ⇒ String
Returns the suffix of the foreign key field, either “_id” or “_ids”.
291 292 293 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 291 def foreign_key_suffix "_ids" end |
.macro ⇒ Symbol
Returns the macro for this relation. Used mostly as a helper in reflection.
302 303 304 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 302 def macro :references_and_referenced_in_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.
328 329 330 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 328 def nested_builder(, attributes, ) Builders::NestedAttributes::Many.new(, attributes, ) end |
.path(document) ⇒ Root
Get the path calculator for the supplied document.
342 343 344 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 342 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.
355 356 357 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 355 def stores_foreign_key? true end |
.valid_options ⇒ Array<Symbol>
Get the valid options allowed with this relation.
367 368 369 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 367 def [ :autosave, :dependent, :foreign_key, :index, :order ] end |
.validation_default ⇒ true, false
Get the default validation setting for the relation. Determines if by default a validates associated will occur.
380 381 382 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 380 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.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 27 def <<(*args) batched do [].tap do |ids| args.flatten.each do |doc| next unless doc append(doc) if persistable? || _creating? ids.push(doc.id) doc.save else base.send(.foreign_key).push(doc.id) base.synced[.foreign_key] = false end end if persistable? || _creating? base.push_all(.foreign_key, ids) base.synced[.foreign_key] = false end 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.
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 70 def build(attributes = {}, = {}, type = nil) if .is_a? Class , type = {}, end Factory.build(type || klass, attributes, ).tap do |doc| base.send(.foreign_key).push(doc.id) append(doc) doc.synced[.inverse_foreign_key] = false yield(doc) if block_given? 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.
96 97 98 99 100 101 102 103 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 96 def delete(document) super.tap do |doc| if doc && persistable? base.pull(.foreign_key, doc.id) base.synced[.foreign_key] = false end end end |
#nullify ⇒ Object Also known as: nullify_all, clear, purge
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.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 113 def nullify unless .forced_nil_inverse? criteria.pull(.inverse_foreign_key, base.id) end if persistable? base.set( .foreign_key, base.send(.foreign_key).clear ) end target.clear do |doc| unbind_one(doc) 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.
person.preferences.substitute([ new_post ])
143 144 145 146 147 148 149 150 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 143 def substitute(replacement) tap do |proxy| if replacement != proxy.in_memory proxy.purge proxy.push(replacement.compact.uniq) if replacement end end end |
#unscoped ⇒ Criteria
Get a criteria for the documents without the default scoping applied.
161 162 163 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 161 def unscoped klass.unscoped.any_in(:_id => base.send(.foreign_key)) end |