Class: Mongoid::Associations::HasMany
- Includes:
- Proxy
- Defined in:
- lib/mongoid/associations/has_many.rb
Instance Attribute Summary collapse
-
#association_name ⇒ Object
Returns the value of attribute association_name.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
-
.instantiate(document, options) ⇒ Object
Preferred method of creating a new
HasMany
association. -
.macro ⇒ Object
Returns the macro used to create the association.
-
.update(children, parent, options) ⇒ Object
Perform an update of the relationship of the parent and child.
Instance Method Summary collapse
-
#<<(*objects) ⇒ Object
Appends the object to the
Array
, setting its parent in the process. -
#build(attrs = {}, type = nil) ⇒ Object
Builds a new Document and adds it to the association collection.
-
#clear ⇒ Object
Clears the association, and notifies the parents of the removal.
-
#concat(*objects) ⇒ Object
Appends the object to the
Array
, setting its parent in the process. -
#create(attrs = {}, type = nil) ⇒ Object
Creates a new Document and adds it to the association collection.
-
#find(param) ⇒ Object
Finds a document in this association.
-
#initialize(document, options) ⇒ HasMany
constructor
Creates the new association by finding the attributes in the parent document with its name, and instantiating a new document for each one found.
-
#method_missing(name, *args, &block) ⇒ Object
Delegate all missing methods over to the documents array.
-
#nested_build(attributes) ⇒ Object
Used for setting associations via a nested attributes setter from the parent
Document
. -
#push(*objects) ⇒ Object
Appends the object to the
Array
, setting its parent in the process.
Methods included from Proxy
Constructor Details
#initialize(document, options) ⇒ HasMany
Creates the new association by finding the attributes in the parent document with its name, and instantiating a new document for each one found. These will then be put in an internal array.
This then delegated all methods to the array class since this is essentially a proxy to an array itself.
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/mongoid/associations/has_many.rb', line 73 def initialize(document, ) @parent, @association_name, @klass, @options = document, .name, .klass, attributes = document.attributes[@association_name] @documents = attributes ? attributes.collect do |attrs| type = attrs[:_type] child = type ? type.constantize.instantiate(attrs) : @klass.instantiate(attrs) child.parentize(@parent, @association_name) child end : [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Delegate all missing methods over to the documents array.
85 86 87 |
# File 'lib/mongoid/associations/has_many.rb', line 85 def method_missing(name, *args, &block) @documents.send(name, *args, &block) end |
Instance Attribute Details
#association_name ⇒ Object
Returns the value of attribute association_name.
7 8 9 |
# File 'lib/mongoid/associations/has_many.rb', line 7 def association_name @association_name end |
#klass ⇒ Object
Returns the value of attribute klass.
7 8 9 |
# File 'lib/mongoid/associations/has_many.rb', line 7 def klass @klass end |
#options ⇒ Object
Returns the value of attribute options.
7 8 9 |
# File 'lib/mongoid/associations/has_many.rb', line 7 def @options end |
Class Method Details
.instantiate(document, options) ⇒ Object
Preferred method of creating a new HasMany
association. It will delegate to new.
Options:
document: The parent Document
options: The association options
116 117 118 |
# File 'lib/mongoid/associations/has_many.rb', line 116 def instantiate(document, ) new(document, ) end |
.macro ⇒ Object
Returns the macro used to create the association.
121 122 123 |
# File 'lib/mongoid/associations/has_many.rb', line 121 def macro :has_many end |
.update(children, parent, options) ⇒ Object
Perform an update of the relationship of the parent and child. This is initialized by setting the has_many to the supplied Enumerable
and setting up the parentization.
128 129 130 131 132 |
# File 'lib/mongoid/associations/has_many.rb', line 128 def update(children, parent, ) parent.remove_attribute(.name) children.assimilate(parent, ) new(parent, ) end |
Instance Method Details
#<<(*objects) ⇒ Object
Appends the object to the Array
, setting its parent in the process.
11 12 13 14 15 16 17 |
# File 'lib/mongoid/associations/has_many.rb', line 11 def <<(*objects) objects.flatten.each do |object| object.parentize(@parent, @association_name) @documents << object object.notify end end |
#build(attrs = {}, type = nil) ⇒ Object
Builds a new Document and adds it to the association collection. The document created will be of the same class as the others in the association, and the attributes will be passed into the constructor.
Returns the newly created object.
40 41 42 43 44 |
# File 'lib/mongoid/associations/has_many.rb', line 40 def build(attrs = {}, type = nil) object = type ? type.instantiate(attrs) : @klass.instantiate(attrs) push(object) object end |
#clear ⇒ Object
Clears the association, and notifies the parents of the removal.
20 21 22 23 24 25 26 27 |
# File 'lib/mongoid/associations/has_many.rb', line 20 def clear unless @documents.empty? object = @documents.first object.changed(true) object.notify_observers(object, true) @documents.clear end end |
#concat(*objects) ⇒ Object
Appends the object to the Array
, setting its parent in the process.
31 32 33 |
# File 'lib/mongoid/associations/has_many.rb', line 31 def concat(*objects) self << objects end |
#create(attrs = {}, type = nil) ⇒ Object
Creates a new Document and adds it to the association collection. The document created will be of the same class as the others in the association, and the attributes will be passed into the constructor and the new object will then be saved.
Returns the newly created object.
52 53 54 55 56 |
# File 'lib/mongoid/associations/has_many.rb', line 52 def create(attrs = {}, type = nil) object = build(attrs, type) object.save object end |
#find(param) ⇒ Object
Finds a document in this association. If :all is passed, returns all the documents If an id is passed, will return the document for that id.
61 62 63 64 |
# File 'lib/mongoid/associations/has_many.rb', line 61 def find(param) return @documents if param == :all return detect { |document| document.id == param } end |
#nested_build(attributes) ⇒ Object
Used for setting associations via a nested attributes setter from the parent Document
.
Options:
attributes: A Hash
of integer keys and Hash
values.
95 96 97 98 99 |
# File 'lib/mongoid/associations/has_many.rb', line 95 def nested_build(attributes) attributes.values.each do |attrs| build(attrs) end end |
#push(*objects) ⇒ Object
Appends the object to the Array
, setting its parent in the process.
103 104 105 |
# File 'lib/mongoid/associations/has_many.rb', line 103 def push(*objects) self << objects end |