Class: Mongoid::Relations::Embedded::Many
- Includes:
- Batchable
- 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 collapse
- VALID_OPTIONS =
The allowed options when defining this relation.
[ :as, :cascade_callbacks, :cyclic, :order, :store_as, :before_add, :after_add, :before_remove, :after_remove ].freeze
Instance Attribute Summary
Attributes inherited from Proxy
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.
-
.embedded? ⇒ true
Returns true if the relation is an embedded one.
-
.foreign_key_suffix ⇒ nil
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) ⇒ Mongoid::Atomic::Paths::Embedded::Many
Get the path calculator for the supplied document.
-
.stores_foreign_key? ⇒ false
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) ⇒ Object
(also: #push)
Appends a document or array of documents to the relation.
-
#as_document ⇒ Array<Hash>
Get this relation as as its representation in the database.
-
#build(attributes = {}, type = nil) {|doc| ... } ⇒ Document
(also: #new)
Builds a new document in the relation and appends it to the target.
-
#clear ⇒ Many
Clear the relation.
-
#concat(docs) ⇒ Array<Document>
Appends an array of documents to the relation.
-
#count ⇒ Integer
Returns a count of the number of documents in the association that have actually been persisted to the database.
-
#delete(document) ⇒ Document?
Delete the supplied document from the target.
-
#delete_all(conditions = {}) ⇒ Integer
Delete all the documents in the association without running callbacks.
-
#delete_if ⇒ Many, Enumerator
Delete all the documents for which the provided block returns true.
-
#destroy_all(conditions = {}) ⇒ Integer
Destroy all the documents in the association whilst running callbacks.
-
#exists? ⇒ true, false
Determine if any documents in this relation exist in the database.
-
#find(*args) ⇒ Array<Document>, Document
Finds a document in this association through several different methods.
-
#in_memory ⇒ Array<Document>
Get all the documents in the relation that are loaded into memory.
-
#initialize(base, target, metadata) ⇒ Many
constructor
Instantiate a new embeds_many relation.
-
#pop(count = nil) ⇒ Document+
Pop documents off the relation.
-
#shift(count = nil) ⇒ Document+
Shift documents off the relation.
-
#substitute(docs) ⇒ Many
Substitutes the supplied target documents for the existing documents in the relation.
-
#unscoped ⇒ Criteria
Return the relation with all previous scoping removed.
Methods included from Batchable
#batch_clear, #batch_insert, #batch_remove, #batch_replace
Methods included from Positional
Methods inherited from Many
#blank?, #create, #create!, #find_or_create_by, #find_or_create_by!, #find_or_initialize_by, #nil?, #respond_to?, #scoped, #serializable_hash
Methods inherited from Proxy
apply_ordering, #extend_proxies, #init, #klass, #reset_unloaded, #substitutable
Methods included from Marshalable
Constructor Details
#initialize(base, target, metadata) ⇒ Many
Instantiate a new embeds_many relation.
249 250 251 252 253 254 255 256 257 258 |
# File 'lib/mongoid/relations/embedded/many.rb', line 249 def initialize(base, target, ) init(base, target, ) do target.each_with_index do |doc, index| integrate(doc) doc._index = index end @_unscoped = target.dup @target = scope(target) 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.
440 441 442 443 444 445 |
# File 'lib/mongoid/relations/embedded/many.rb', line 440 def method_missing(name, *args, &block) return super if target.respond_to?(name) klass.send(:with_scope, criteria) do criteria.public_send(name, *args, &block) end end |
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.
559 560 561 |
# File 'lib/mongoid/relations/embedded/many.rb', line 559 def builder(base, , object) Builders::Embedded::Many.new(base, , object) end |
.embedded? ⇒ true
Returns true if the relation is an embedded one. In this case always true.
572 573 574 |
# File 'lib/mongoid/relations/embedded/many.rb', line 572 def true end |
.foreign_key_suffix ⇒ nil
Returns the suffix of the foreign key field, either “_id” or “_ids”.
584 585 586 |
# File 'lib/mongoid/relations/embedded/many.rb', line 584 def foreign_key_suffix nil end |
.macro ⇒ Symbol
Returns the macro for this relation. Used mostly as a helper in reflection.
597 598 599 |
# File 'lib/mongoid/relations/embedded/many.rb', line 597 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.
623 624 625 |
# File 'lib/mongoid/relations/embedded/many.rb', line 623 def nested_builder(, attributes, ) Builders::NestedAttributes::Many.new(, attributes, ) end |
.path(document) ⇒ Mongoid::Atomic::Paths::Embedded::Many
Get the path calculator for the supplied document.
638 639 640 |
# File 'lib/mongoid/relations/embedded/many.rb', line 638 def path(document) Mongoid::Atomic::Paths::Embedded::Many.new(document) end |
.stores_foreign_key? ⇒ false
Tells the caller if this relation is one that stores the foreign key on its own objects.
651 652 653 |
# File 'lib/mongoid/relations/embedded/many.rb', line 651 def stores_foreign_key? false end |
.valid_options ⇒ Array<Symbol>
Get the valid options allowed with this relation.
663 664 665 |
# File 'lib/mongoid/relations/embedded/many.rb', line 663 def VALID_OPTIONS end |
.validation_default ⇒ true, false
Get the default validation setting for the relation. Determines if by default a validates associated will occur.
676 677 678 |
# File 'lib/mongoid/relations/embedded/many.rb', line 676 def validation_default true end |
Instance Method Details
#<<(*args) ⇒ Object Also known as: push
Appends a document or array of documents to the relation. Will set the parent and update the index in the process.
41 42 43 44 45 46 47 48 49 |
# File 'lib/mongoid/relations/embedded/many.rb', line 41 def <<(*args) docs = args.flatten return concat(docs) if docs.size > 1 if doc = docs.first append(doc) doc.save if persistable? && !_assigning? end self end |
#as_document ⇒ Array<Hash>
Get this relation as as its representation in the database.
60 61 62 |
# File 'lib/mongoid/relations/embedded/many.rb', line 60 def as_document as_attributes.collect { |attrs| BSON::Document.new(attrs) } end |
#build(attributes = {}, options = {}, type = nil) ⇒ Document #build(attributes = {}, type = nil) ⇒ 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.
95 96 97 98 99 100 101 102 103 |
# File 'lib/mongoid/relations/embedded/many.rb', line 95 def build(attributes = {}, type = nil) doc = Factory.build(type || .klass, attributes) append(doc) doc.apply_post_processed_defaults yield(doc) if block_given? doc.run_callbacks(:build) { doc } base._reset_memoized_children! doc end |
#clear ⇒ Many
Clear the relation. Will delete the documents from the db if they are already persisted.
113 114 115 116 |
# File 'lib/mongoid/relations/embedded/many.rb', line 113 def clear batch_clear(target.dup) self end |
#concat(docs) ⇒ Array<Document>
Appends an array of documents to the relation. Performs a batch insert of the documents instead of persisting one at a time.
75 76 77 78 |
# File 'lib/mongoid/relations/embedded/many.rb', line 75 def concat(docs) batch_insert(docs) unless docs.empty? self 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.
128 129 130 |
# File 'lib/mongoid/relations/embedded/many.rb', line 128 def count target.select { |doc| doc.persisted? }.size 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.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/mongoid/relations/embedded/many.rb', line 143 def delete(document) execute_callback :before_remove, document doc = target.delete_one(document) if doc && !_binding? _unscoped.delete_one(doc) if _assigning? base.add_atomic_pull(doc) else doc.delete(suppress: true) unbind_one(doc) end end reindex execute_callback :after_remove, document doc end |
#delete_all(conditions = {}) ⇒ Integer
Delete all the documents in the association without running callbacks.
171 172 173 |
# File 'lib/mongoid/relations/embedded/many.rb', line 171 def delete_all(conditions = {}) remove_all(conditions, :delete) end |
#delete_if ⇒ Many, Enumerator
Delete all the documents for which the provided block returns true.
186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/mongoid/relations/embedded/many.rb', line 186 def delete_if if block_given? dup_target = target.dup dup_target.each do |doc| delete(doc) if yield(doc) end self else super end end |
#destroy_all(conditions = {}) ⇒ Integer
Destroy all the documents in the association whilst running callbacks.
209 210 211 |
# File 'lib/mongoid/relations/embedded/many.rb', line 209 def destroy_all(conditions = {}) remove_all(conditions, :destroy) end |
#exists? ⇒ true, false
Determine if any documents in this relation exist in the database.
219 220 221 |
# File 'lib/mongoid/relations/embedded/many.rb', line 219 def exists? count > 0 end |
#find(*args) ⇒ Array<Document>, Document
Finds a document in this association through several different methods.
235 236 237 |
# File 'lib/mongoid/relations/embedded/many.rb', line 235 def find(*args) criteria.find(*args) end |
#in_memory ⇒ Array<Document>
Get all the documents in the relation that are loaded into memory.
268 269 270 |
# File 'lib/mongoid/relations/embedded/many.rb', line 268 def in_memory target end |
#pop(count = nil) ⇒ Document+
Pop documents off the relation. This can be a single document or multiples, and will automatically persist the changes.
287 288 289 290 291 292 293 294 295 |
# File 'lib/mongoid/relations/embedded/many.rb', line 287 def pop(count = nil) if count if docs = target[target.size - count, target.size] docs.each { |doc| delete(doc) } end else delete(target[-1]) end end |
#shift(count = nil) ⇒ Document+
Shift documents off the relation. This can be a single document or multiples, and will automatically persist the changes.
310 311 312 313 314 315 316 317 318 |
# File 'lib/mongoid/relations/embedded/many.rb', line 310 def shift(count = nil) if count if target.size > 0 && docs = target[0, count] docs.each { |doc| delete(doc) } end else delete(target[0]) end end |
#substitute(docs) ⇒ Many
Substitutes the supplied target documents for the existing documents in the relation.
331 332 333 334 |
# File 'lib/mongoid/relations/embedded/many.rb', line 331 def substitute(docs) batch_replace(docs) self end |
#unscoped ⇒ Criteria
Return the relation with all previous scoping removed. This is the exact representation of the docs in the database.
345 346 347 348 349 350 |
# File 'lib/mongoid/relations/embedded/many.rb', line 345 def unscoped criterion = klass.unscoped criterion. = true criterion.documents = _unscoped.delete_if(&:marked_for_destruction?) criterion end |