Class: Mongoid::Association::Referenced::HasMany::Proxy
- Extended by:
- Forwardable
- Defined in:
- build/mongoid-master/lib/mongoid/association/referenced/has_many/proxy.rb
Overview
This class defines the behavior for all associations that are a one-to-many between documents in different collections.
Direct Known Subclasses
Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy
Instance Attribute Summary
Attributes inherited from Proxy
#_association, #_base, #_target
Class Method Summary collapse
- .eager_loader(association, docs) ⇒ Object
-
.embedded? ⇒ false
Returns true if the association is an embedded one.
Instance Method Summary collapse
-
#<<(*args) ⇒ Array<Document>
(also: #push)
Appends a document or array of documents to the association.
-
#build(attributes = {}, type = nil) {|doc| ... } ⇒ Document
(also: #new)
Build a new document from the attributes and append it to this association without saving.
-
#concat(documents) ⇒ Array<Document>
Appends an array of documents to the association.
-
#delete(document) ⇒ Document
Delete the document from the association.
-
#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.
-
#each ⇒ Array<Document>
Iterate over each document in the association and yield to the provided block.
-
#exists? ⇒ true | false
Determine if any documents in this association exist in the database.
-
#find(*args) {|Object| ... } ⇒ Document | Array<Document> | nil
Find the matching document on the association, either based on id or conditions.
-
#initialize(base, target, association) ⇒ Proxy
constructor
Instantiate a new references_many association.
-
#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.
-
#purge ⇒ Many
(also: #clear)
Clear the association.
-
#substitute(replacement) ⇒ Many
Substitutes the supplied target documents for the existing documents in the association.
-
#unscoped ⇒ Criteria
Get a criteria for the documents without the default scoping applied.
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, association) ⇒ Proxy
Instantiate a new references_many association. Will set the foreign key and the base on the inverse object.
222 223 224 225 226 227 |
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many/proxy.rb', line 222 def initialize(base, target, association) enum = HasMany::Enumerable.new(target, base, association) init(base, enum, association) do raise_mixed if klass. && !klass.cyclic? end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing ⇒ 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.
422 423 424 425 426 427 428 429 430 |
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many/proxy.rb', line 422 ruby2_keywords def method_missing(name, *args, &block) if _target.respond_to?(name) _target.send(name, *args, &block) else klass.send(:with_scope, criteria) do criteria.public_send(name, *args, &block) end end end |
Class Method Details
.eager_loader(association, docs) ⇒ Object
533 534 535 |
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many/proxy.rb', line 533 def eager_loader(association, docs) Eager.new(association, docs) end |
.embedded? ⇒ false
Returns true if the association is an embedded one. In this case always false.
544 545 546 |
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many/proxy.rb', line 544 def false end |
Instance Method Details
#<<(*args) ⇒ Array<Document> Also known as: push
Appends a document or array of documents to the association. Will set the parent and update the index in the process.
31 32 33 34 35 36 37 38 39 |
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many/proxy.rb', line 31 def <<(*args) docs = args.flatten return concat(docs) if docs.size > 1 if doc = docs.first append(doc) doc.save if persistable? && !_assigning? && !doc.validated? end self end |
#build(attributes = {}, type = nil) {|doc| ... } ⇒ Document Also known as: new
Build a new document from the attributes and append it to this association without saving.
73 74 75 76 77 78 79 80 81 |
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many/proxy.rb', line 73 def build(attributes = {}, type = nil) doc = Factory.execute_build(type || klass, attributes, execute_callbacks: false) append(doc) doc.apply_post_processed_defaults yield(doc) if block_given? doc.run_pending_callbacks doc.run_callbacks(:build) { doc } doc end |
#concat(documents) ⇒ Array<Document>
Appends an array of documents to the association. Performs a batch insert of the documents instead of persisting one at a time.
52 53 54 55 56 57 58 59 60 61 |
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many/proxy.rb', line 52 def concat(documents) docs, inserts = [], [] documents.each do |doc| next unless doc append(doc) save_or_delay(doc, docs, inserts) if persistable? end persist_delayed(docs, inserts) self end |
#delete(document) ⇒ Document
Delete the document from the association. This will set the foreign key on the document to nil. If the dependent options on the association are :delete_all or :destroy the appropriate removal will occur.
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many/proxy.rb', line 95 def delete(document) execute_callbacks_around(:remove, document) do _target.delete(document) do |doc| if doc unbind_one(doc) cascade!(doc) if !_assigning? end end.tap do reset_unloaded end end end |
#delete_all(conditions = nil) ⇒ Integer
Deletes all related documents from the database given the supplied conditions.
120 121 122 |
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many/proxy.rb', line 120 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.
136 137 138 |
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many/proxy.rb', line 136 def destroy_all(conditions = nil) remove_all(conditions, :destroy_all) end |
#each ⇒ Array<Document>
This will load the entire association into memory.
Iterate over each document in the association and yield to the provided block.
151 152 153 154 155 156 157 |
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many/proxy.rb', line 151 def each if block_given? _target.each { |doc| yield(doc) } else to_enum end end |
#exists? ⇒ true | false
Determine if any documents in this association exist in the database.
If the association contains documents but all of the documents exist only in the application, i.e. have not been persisted to the database, this method returns false.
This method queries the database on each invocation even if the association is already loaded into memory.
172 173 174 |
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many/proxy.rb', line 172 def exists? criteria.exists? end |
#find(*args) {|Object| ... } ⇒ Document | Array<Document> | nil
Each argument can be an individual id, an array of ids or a nested array. Each array will be flattened.
This will keep matching documents in memory for iteration later.
Find the matching document on the association, either based on id or conditions.
This method delegates to Mongoid::Criteria#find. If this method is not given a block, it returns one or many documents for the provided _id values.
If this method is given a block, it returns the first document of those found by the current Criteria object for which the block returns a truthy value.
207 208 209 210 211 |
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many/proxy.rb', line 207 def find(*args, &block) matching = criteria.find(*args, &block) Array(matching).each { |doc| _target.push(doc) } matching 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.
235 236 237 238 239 240 241 |
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many/proxy.rb', line 235 def nullify criteria.update_all(foreign_key => nil) _target.clear do |doc| unbind_one(doc) doc.changed_attributes.delete(foreign_key) end end |
#purge ⇒ Many Also known as: clear
Clear the association. Will delete the documents from the db if they are already persisted.
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many/proxy.rb', line 252 def purge unless _association.destructive? nullify else after_remove_error = nil criteria.delete_all many = _target.clear do |doc| execute_callback :before_remove, doc unbind_one(doc) doc.destroyed = true begin execute_callback :after_remove, doc rescue => e after_remove_error = e end end raise after_remove_error if after_remove_error many end end |
#substitute(replacement) ⇒ Many
Substitutes the supplied target documents for the existing documents in the association. If the new target is nil, perform the necessary deletion.
285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many/proxy.rb', line 285 def substitute(replacement) if replacement new_docs, docs = replacement.compact, [] new_ids = new_docs.map { |doc| doc._id } remove_not_in(new_ids) new_docs.each do |doc| docs.push(doc) if doc.send(foreign_key) != _base.send(_association.primary_key) end concat(docs) else purge end self end |
#unscoped ⇒ Criteria
Get a criteria for the documents without the default scoping applied.
307 308 309 |
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many/proxy.rb', line 307 def unscoped klass.unscoped.where(foreign_key => _base.send(_association.primary_key)) end |