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.
144 145 146 147 148 149 150 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 144 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.
299 300 301 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 299 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.
312 313 314 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 312 def false end |
.foreign_key_default ⇒ Array
Get the default value for the foreign key.
324 325 326 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 324 def foreign_key_default [] end |
.foreign_key_suffix ⇒ String
Returns the suffix of the foreign key field, either “_id” or “_ids”.
336 337 338 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 336 def foreign_key_suffix "_ids" end |
.macro ⇒ Symbol
Returns the macro for this relation. Used mostly as a helper in reflection.
347 348 349 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 347 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.
373 374 375 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 373 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.
386 387 388 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 386 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 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 97 def delete(document, = {}) target.delete(document).tap do |doc| binding.unbind_one(doc, .merge!()) if doc end end |
#delete_all(conditions = nil) ⇒ Integer
Deletes all related documents from the database given the supplied conditions.
115 116 117 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 115 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.
131 132 133 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 131 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.
160 161 162 163 164 165 166 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 160 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.
186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 186 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 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.
214 215 216 217 218 |
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 214 def unbind( = {}) target.each(&:delete) if base.persisted? binding.unbind() [] end |