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(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 ⇒ 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.
-
.stores_foreign_key? ⇒ true
Tells the caller if this relation is one that stores the foreign key on its own objects.
Instance Method Summary collapse
-
#<<(*args) ⇒ Object
(also: #concat, #push)
Appends a document or array of documents to the relation.
-
#build(attributes = {}, type = nil, &block) ⇒ Document
(also: #new)
Builds a new document in the relation and appends it to the target.
-
#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(document, options = {}) ⇒ Object
Delete a single 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.
-
#initialize(base, target, metadata) ⇒ ManyToMany
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.
-
#substitute(new_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
#bind, #clear, #count, #find, #load!
Methods inherited from Many
#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) ⇒ ManyToMany
Instantiate a new references_many relation. Will set the foreign key and the base on the inverse object.
146 147 148 149 150 151 152 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 146 def initialize(base, target, ) init(base, target, ) do unless base.frozen? base.send(.foreign_key_setter, target.map(&:id)) end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Mongoid::Relations::Referenced::Many
Class Method Details
.builder(meta, object) ⇒ Builder
Return the builder that is responsible for generating the documents that will be used by this relation.
302 303 304 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 302 def builder(, object) Builders::Referenced::ManyToMany.new(, object) end |
.embedded? ⇒ false
Returns true if the relation is an embedded one. In this case always false.
315 316 317 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 315 def false end |
.foreign_key_default ⇒ Array
Get the default value for the foreign key.
327 328 329 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 327 def foreign_key_default [] end |
.foreign_key_suffix ⇒ String
Returns the suffix of the foreign key field, either “_id” or “_ids”.
339 340 341 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 339 def foreign_key_suffix "_ids" end |
.macro ⇒ Symbol
Returns the macro for this relation. Used mostly as a helper in reflection.
350 351 352 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 350 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.
376 377 378 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 376 def nested_builder(, attributes, ) Builders::NestedAttributes::Many.new(, attributes, ) end |
.stores_foreign_key? ⇒ true
Tells the caller if this relation is one that stores the foreign key on its own objects.
389 390 391 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 389 def stores_foreign_key? true end |
Instance Method Details
#<<(*args) ⇒ Object 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.
23 24 25 26 27 28 29 30 31 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 23 def <<(*args) = (args) args.flatten.each do |doc| return doc unless doc append(doc, ) base.add_to_set(.foreign_key, doc.id) doc.save if base.persisted? && ![:binding] end end |
#build(attributes = {}, type = nil, &block) ⇒ Document Also known as: new
Builds a new document in the relation and appends it to the target. Takes an optional type if you want to specify a subclass.
45 46 47 48 49 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 45 def build(attributes = {}, type = nil, &block) super.tap do |doc| base.send(.foreign_key).push(doc.id) 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.
62 63 64 65 66 67 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 62 def create(attributes = nil, type = nil, &block) build(attributes, type, &block).tap do |doc| base.add_to_set(.foreign_key, doc.id) doc.save if base.persisted? 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.
82 83 84 85 86 87 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 82 def create!(attributes = nil, type = nil, &block) build(attributes, type, &block).tap do |doc| base.add_to_set(.foreign_key, doc.id) doc.save! if base.persisted? end end |
#delete(document, options = {}) ⇒ Object
Delete a single document from the relation.
97 98 99 100 101 102 103 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 97 def delete(document, = {}) target.delete(document).tap do |doc| if doc binding.unbind_one(doc, .merge!()) end end end |
#delete_all(conditions = nil) ⇒ Integer
Deletes all related documents from the database given the supplied conditions.
117 118 119 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 117 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.
133 134 135 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 133 def destroy_all(conditions = nil) remove_all(conditions, :destroy_all) 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.
162 163 164 165 166 167 168 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 162 def nullify load! and target.each do |doc| base.send(.foreign_key).delete(doc.id) dereference(doc) end target.clear end |
#substitute(new_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.
188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 188 def substitute(new_target, = {}) tap do |relation| if new_target binding.unbind() relation.target = new_target.to_a base.send(.foreign_key_setter, new_target.map(&:id)) bind() else relation.target = unbind() end base.save if base.persisted? && ![:binding] end 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.
217 218 219 220 221 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 217 def unbind( = {}) target.each(&:delete) if base.persisted? binding.unbind() [] end |