Class: Mongoid::Relations::Embedded::Many
- Includes:
- Atomic
- Defined in:
- lib/mongoid/relations/embedded/many.rb
Overview
This class handles the behaviour for a document that embeds many other documents within in it as an array.
Constant Summary
Constants included from Atomic
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? ⇒ true
Returns true if the relation is an embedded one.
-
.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
-
#<<(*args) ⇒ Object
Appends a document or array of documents to the relation.
-
#as_document ⇒ Array<Hash>
Get this relation as as its representation in the database.
-
#bind(options = {}) ⇒ Object
Binds the base object to the inverse of the relation.
-
#bind_one(document, options = {}) ⇒ Object
Bind the inverse relation between a single document in this proxy instead of the entire target.
-
#clear ⇒ 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 = {}, type = nil, &block) ⇒ Document
Create a new document in the relation.
-
#create!(attributes = {}, type = nil, &block) ⇒ Document
Create a new document in the relation.
-
#delete(document) ⇒ Document?
Delete the supplied document from the target.
-
#delete_all(conditions = {}) ⇒ Integer
Delete all the documents in the association without running callbacks.
-
#destroy_all(conditions = {}) ⇒ Integer
Destroy all the documents in the association whilst running callbacks.
-
#find(*args) ⇒ Array<Document>, Document
Finds a document in this association through several different methods.
-
#initialize(base, target, metadata) ⇒ Many
constructor
Instantiate a new embeds_many relation.
-
#load!(options = {}) ⇒ Many
Will load the target into an array if the target had not already been loaded.
-
#substitute(new_target, options = {}) ⇒ Many
Substitutes the supplied target documents for the existing documents in the relation.
-
#unbind(old_target, options = {}) ⇒ Object
Unbind the inverse relation from this set of documents.
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 embeds_many relation.
207 208 209 210 211 212 213 214 215 |
# File 'lib/mongoid/relations/embedded/many.rb', line 207 def initialize(base, target, ) init(base, target, ) do target.each_with_index do |doc, index| characterize_one(doc) doc.parentize(base) doc._index = index end 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.
353 354 355 356 357 358 359 |
# File 'lib/mongoid/relations/embedded/many.rb', line 353 def method_missing(name, *args, &block) load!(:binding => true) and return super if target.respond_to?(name) klass = .klass klass.send(:with_scope, criteria) do criteria.send(name, *args, &block) 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.
427 428 429 |
# File 'lib/mongoid/relations/embedded/many.rb', line 427 def builder(, object) Builders::Embedded::Many.new(, object) end |
.embedded? ⇒ true
Returns true if the relation is an embedded one. In this case always true.
440 441 442 |
# File 'lib/mongoid/relations/embedded/many.rb', line 440 def true end |
.macro ⇒ Symbol
Returns the macro for this relation. Used mostly as a helper in reflection.
453 454 455 |
# File 'lib/mongoid/relations/embedded/many.rb', line 453 def macro :embeds_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.
479 480 481 |
# File 'lib/mongoid/relations/embedded/many.rb', line 479 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.
492 493 494 |
# File 'lib/mongoid/relations/embedded/many.rb', line 492 def stores_foreign_key? false end |
Instance Method Details
#<<(*args) ⇒ Object
Appends a document or array of documents to the relation. Will set the parent and update the index in the process.
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/mongoid/relations/embedded/many.rb', line 24 def <<(*args) = (args) atomically(:$pushAll) do args.flatten.each do |doc| return doc unless doc append(doc, ) doc.save if base.persisted? && ![:binding] end end end |
#as_document ⇒ Array<Hash>
Get this relation as as its representation in the database.
267 268 269 270 271 272 273 |
# File 'lib/mongoid/relations/embedded/many.rb', line 267 def as_document target.inject([]) do |attributes, doc| attributes.tap do |attr| attr << doc.as_document end end end |
#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.
52 53 54 55 56 57 |
# File 'lib/mongoid/relations/embedded/many.rb', line 52 def bind( = {}) binding.bind() if base.persisted? && ![:binding] atomically(:$set) { target.each(&:save) } end end |
#bind_one(document, options = {}) ⇒ Object
Bind the inverse relation between a single document in this proxy instead of the entire target.
Used when appending to the target instead of setting the entire thing.
71 72 73 |
# File 'lib/mongoid/relations/embedded/many.rb', line 71 def bind_one(document, = {}) binding.bind_one(document, ) end |
#clear ⇒ Many
Clear the relation. Will delete the documents from the db if they are already persisted.
82 83 84 |
# File 'lib/mongoid/relations/embedded/many.rb', line 82 def clear load! and substitute(nil) 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.
96 97 98 |
# File 'lib/mongoid/relations/embedded/many.rb', line 96 def count target.select(&:persisted?).size end |
#create(attributes = {}, type = nil, &block) ⇒ Document
Create a new document in the relation. This is essentially the same as doing a #build then #save on the new document.
110 111 112 |
# File 'lib/mongoid/relations/embedded/many.rb', line 110 def create(attributes = {}, type = nil, &block) build(attributes, type, &block).tap(&:save) end |
#create!(attributes = {}, type = nil, &block) ⇒ Document
Create a new document in the relation. This is essentially the same as doing a #build then #save on the new document. If validation failed on the document an error will get raised.
127 128 129 |
# File 'lib/mongoid/relations/embedded/many.rb', line 127 def create!(attributes = {}, type = nil, &block) build(attributes, type, &block).tap(&:save!) end |
#delete(document) ⇒ Document?
Delete the supplied document from the target. This method is proxied in order to reindex the array after the operation occurs.
142 143 144 |
# File 'lib/mongoid/relations/embedded/many.rb', line 142 def delete(document) target.delete(document).tap { reindex } end |
#delete_all(conditions = {}) ⇒ Integer
Delete all the documents in the association without running callbacks.
157 158 159 |
# File 'lib/mongoid/relations/embedded/many.rb', line 157 def delete_all(conditions = {}) atomically(:$pull) { remove_all(conditions, :delete) } end |
#destroy_all(conditions = {}) ⇒ Integer
Destroy all the documents in the association whilst running callbacks.
172 173 174 |
# File 'lib/mongoid/relations/embedded/many.rb', line 172 def destroy_all(conditions = {}) atomically(:$pull) { remove_all(conditions, :destroy) } end |
#find(*args) ⇒ Array<Document>, Document
Finds a document in this association through several different methods.
193 194 195 |
# File 'lib/mongoid/relations/embedded/many.rb', line 193 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.
226 227 228 229 230 231 232 233 |
# File 'lib/mongoid/relations/embedded/many.rb', line 226 def load!( = {}) tap do |relation| unless relation.loaded? relation.bind() relation.loaded = true end end end |
#substitute(new_target, options = {}) ⇒ Many
Substitutes the supplied target documents for the existing documents in the relation.
247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/mongoid/relations/embedded/many.rb', line 247 def substitute(new_target, = {}) old_target = target tap do |relation| relation.target = new_target || [] if !new_target.blank? atomically(:$set) { rebind(old_target, ) } else atomically(:$unset) { unbind(old_target, ) } end end end |
#unbind(old_target, options = {}) ⇒ Object
Unbind the inverse relation from this set of documents. Used when the entire proxy has been cleared, set to nil or empty, or replaced.
289 290 291 292 293 294 295 296 |
# File 'lib/mongoid/relations/embedded/many.rb', line 289 def unbind(old_target, = {}) binding(old_target).unbind() if base.persisted? old_target.each do |doc| doc.delete unless doc.destroyed? end end end |