Class: Mongoid::Associations::HasManyAssociation

Inherits:
Array
  • Object
show all
Defined in:
lib/mongoid/associations/has_many_association.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Extensions::Array::Parentization

#parentize

Methods included from Extensions::Array::Conversions

#mongoidize

Constructor Details

#initialize(association_name, document) ⇒ HasManyAssociation

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.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mongoid/associations/has_many_association.rb', line 54

def initialize(association_name, document)
  @parent = document
  @association_name = association_name
  @klass = @association_name.to_s.classify.constantize
  attributes = document.attributes[@association_name]
  @documents = attributes ? attributes.collect do |attribute|
    child = @klass.new(attribute)
    child.parentize(@parent, @association_name)
    child
  end : []
  super(@documents)
end

Instance Attribute Details

#association_nameObject

Returns the value of attribute association_name.



5
6
7
# File 'lib/mongoid/associations/has_many_association.rb', line 5

def association_name
  @association_name
end

Instance Method Details

#<<(object) ⇒ Object

Appends the object to the Array, setting its parent in the process.



9
10
11
12
13
# File 'lib/mongoid/associations/has_many_association.rb', line 9

def <<(object)
  object.parentize(@parent, @association_name)
  @documents << object
  object.is_a?(Array) ? object.each(&:notify) : object.notify
end

#build(attributes) ⇒ 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.



32
33
34
35
36
37
# File 'lib/mongoid/associations/has_many_association.rb', line 32

def build(attributes)
  object = @klass.new(attributes)
  object.parentize(@parent, @association_name)
  push(object)
  object
end

#concat(object) ⇒ Object

Appends the object to the Array, setting its parent in the process.



23
24
25
# File 'lib/mongoid/associations/has_many_association.rb', line 23

def concat(object)
  self << 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.



42
43
44
45
# File 'lib/mongoid/associations/has_many_association.rb', line 42

def find(param)
  return @documents if param == :all
  return detect { |document| document.id == param }
end

#push(object) ⇒ Object

Appends the object to the Array, setting its parent in the process.



17
18
19
# File 'lib/mongoid/associations/has_many_association.rb', line 17

def push(object)
  self << object
end