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, loading = false) ⇒ 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.
-
.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: #concat, #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) ⇒ Document
(also: #new)
Builds a new document in the relation and appends it to the 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.
-
#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.
-
#substitute(replacement) ⇒ Many
Substitutes the supplied target documents for the existing documents in the relation.
-
#unscoped ⇒ Criteria
Get a criteria for the embedded documents without the default scoping applied.
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
#initialize(base, target, metadata) ⇒ Many
Instantiate a new embeds_many relation.
199 200 201 202 203 204 205 206 |
# File 'lib/mongoid/relations/embedded/many.rb', line 199 def initialize(base, target, ) init(base, target, ) do target.each_with_index do |doc, index| integrate(doc) 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.
355 356 357 358 359 360 |
# File 'lib/mongoid/relations/embedded/many.rb', line 355 def method_missing(name, *args, &block) return super if target.respond_to?(name) klass.send(:with_scope, criteria) do criteria.send(name, *args, &block) end end |
Class Method Details
.builder(meta, object, loading = false) ⇒ Builder
Return the builder that is responsible for generating the documents that will be used by this relation.
425 426 427 |
# File 'lib/mongoid/relations/embedded/many.rb', line 425 def builder(, object, loading = false) Builders::Embedded::Many.new(, object, loading) end |
.embedded? ⇒ true
Returns true if the relation is an embedded one. In this case always true.
438 439 440 |
# File 'lib/mongoid/relations/embedded/many.rb', line 438 def true end |
.macro ⇒ Symbol
Returns the macro for this relation. Used mostly as a helper in reflection.
451 452 453 |
# File 'lib/mongoid/relations/embedded/many.rb', line 451 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.
477 478 479 |
# File 'lib/mongoid/relations/embedded/many.rb', line 477 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.
492 493 494 |
# File 'lib/mongoid/relations/embedded/many.rb', line 492 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.
505 506 507 |
# File 'lib/mongoid/relations/embedded/many.rb', line 505 def stores_foreign_key? false end |
.valid_options ⇒ Array<Symbol>
Get the valid options allowed with this relation.
517 518 519 |
# File 'lib/mongoid/relations/embedded/many.rb', line 517 def [ :as, :cyclic, :order, :versioned ] end |
.validation_default ⇒ true, false
Get the default validation setting for the relation. Determines if by default a validates associated will occur.
530 531 532 |
# File 'lib/mongoid/relations/embedded/many.rb', line 530 def validation_default 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.
24 25 26 27 28 29 30 31 32 |
# File 'lib/mongoid/relations/embedded/many.rb', line 24 def <<(*args) atomically(:$pushAll) do args.flatten.each do |doc| next unless doc append(doc) doc.save if persistable? && !assigning? end end end |
#as_document ⇒ Array<Hash>
Get this relation as as its representation in the database.
266 267 268 269 270 271 272 |
# File 'lib/mongoid/relations/embedded/many.rb', line 266 def as_document [].tap do |attributes| target.each do |doc| attributes << doc.as_document end end end |
#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.
46 47 48 49 50 51 52 |
# File 'lib/mongoid/relations/embedded/many.rb', line 46 def build(attributes = {}, type = nil) Factory.build(type || .klass, attributes).tap do |doc| doc.identify append(doc) yield(doc) if block_given? end end |
#clear ⇒ Many
Clear the relation. Will delete the documents from the db if they are already persisted.
62 63 64 65 66 |
# File 'lib/mongoid/relations/embedded/many.rb', line 62 def clear tap do |proxy| atomically(:$unset) { proxy.delete_all } end 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.
78 79 80 |
# File 'lib/mongoid/relations/embedded/many.rb', line 78 def count target.select { |doc| doc.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.
92 93 94 |
# File 'lib/mongoid/relations/embedded/many.rb', line 92 def create(attributes = {}, type = nil, &block) build(attributes, type, &block).tap { |doc| doc.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.
109 110 111 |
# File 'lib/mongoid/relations/embedded/many.rb', line 109 def create!(attributes = {}, type = nil, &block) build(attributes, type, &block).tap { |doc| doc.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.
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/mongoid/relations/embedded/many.rb', line 124 def delete(document) target.delete_one(document).tap do |doc| if doc && !binding? if assigning? base.add_atomic_pull(doc) else doc.delete(:suppress => true) end unbind_one(doc) end reindex end end |
#delete_all(conditions = {}) ⇒ Integer
Delete all the documents in the association without running callbacks.
149 150 151 |
# File 'lib/mongoid/relations/embedded/many.rb', line 149 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.
164 165 166 |
# File 'lib/mongoid/relations/embedded/many.rb', line 164 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.
185 186 187 |
# File 'lib/mongoid/relations/embedded/many.rb', line 185 def find(*args) criteria.find(*args) end |
#in_memory ⇒ Array<Document>
Get all the documents in the relation that are loaded into memory.
216 217 218 |
# File 'lib/mongoid/relations/embedded/many.rb', line 216 def in_memory target end |
#substitute(replacement) ⇒ Many
Substitutes the supplied target documents for the existing documents in the relation.
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/mongoid/relations/embedded/many.rb', line 232 def substitute(replacement) tap do |proxy| if replacement.blank? if assigning? && !proxy.empty? base.atomic_unsets.push(proxy.first.atomic_path) end proxy.clear else atomically(:$set) do if replacement.first.is_a?(Hash) replacement = Many.builder(, replacement).build end proxy.target = replacement.compact if assigning? base.delayed_atomic_sets[.name.to_s] = proxy.as_document end proxy.target.each_with_index do |doc, index| integrate(doc) doc._index = index doc.save if base.persisted? && !assigning? end end end end end |
#unscoped ⇒ Criteria
Get a criteria for the embedded documents without the default scoping applied.
283 284 285 |
# File 'lib/mongoid/relations/embedded/many.rb', line 283 def unscoped criteria(false) end |