Class: Mongoid::Associations::HasMany

Inherits:
Object
  • Object
show all
Includes:
Proxy
Defined in:
lib/mongoid/associations/has_many.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Proxy

included

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, options)
  @parent, @association_name, @klass, @options = document, options.name, options.klass, options
  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_nameObject

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

#klassObject

Returns the value of attribute klass.



7
8
9
# File 'lib/mongoid/associations/has_many.rb', line 7

def klass
  @klass
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/mongoid/associations/has_many.rb', line 7

def options
  @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, options)
  new(document, options)
end

.macroObject

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, options)
  parent.remove_attribute(options.name)
  children.assimilate(parent, options)
  new(parent, options)
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

#clearObject

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