Class: MongoMapper::Plugins::Associations::ManyDocumentsProxy

Inherits:
Collection show all
Includes:
DynamicQuerying::ClassMethods
Defined in:
lib/mongo_mapper/plugins/associations/many_documents_proxy.rb

Direct Known Subclasses

ManyDocumentsAsProxy, ManyPolymorphicProxy

Instance Attribute Summary

Attributes inherited from Proxy

#association, #owner, #target

Instance Method Summary collapse

Methods included from DynamicQuerying::ClassMethods

#dynamic_find

Methods inherited from Collection

#reset, #to_ary

Methods inherited from Proxy

#===, #blank?, #initialize, #inspect, #loaded, #loaded?, #nil?, #present?, #proxy_respond_to?, #reload, #reset, #respond_to?, #send

Constructor Details

This class inherits a constructor from MongoMapper::Plugins::Associations::Proxy

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class MongoMapper::Plugins::DynamicQuerying::ClassMethods

Instance Method Details

#<<(*docs) ⇒ Object Also known as: push, concat



45
46
47
48
49
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 45

def <<(*docs)
  ensure_owner_saved
  flatten_deeper(docs).each { |doc| prepare(doc).save }
  reset
end

#all(options = {}) ⇒ Object



22
23
24
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 22

def all(options={})
  klass.all(scoped_options(options))
end

#build(attrs = {}) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 53

def build(attrs={})
  doc = klass.new(attrs)
  apply_scope(doc)
  @target ||= [] unless loaded?
  @target << doc
  doc
end

#count(options = {}) ⇒ Object



34
35
36
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 34

def count(options={})
  klass.count(scoped_options(options))
end

#create(attrs = {}) ⇒ Object



61
62
63
64
65
66
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 61

def create(attrs={})
  doc = klass.new(attrs)
  apply_scope(doc).save
  reset
  doc
end

#create!(attrs = {}) ⇒ Object



68
69
70
71
72
73
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 68

def create!(attrs={})
  doc = klass.new(attrs)
  apply_scope(doc).save!
  reset
  doc
end

#delete_all(options = {}) ⇒ Object



80
81
82
83
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 80

def delete_all(options={})
  klass.delete_all(options.merge(scoped_conditions))
  reset
end

#destroy_all(options = {}) ⇒ Object



75
76
77
78
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 75

def destroy_all(options={})
  all(options).map(&:destroy)
  reset
end

#find(*args) ⇒ Object



8
9
10
11
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 8

def find(*args)
  options = args.extract_options!
  klass.find(*args << scoped_options(options))
end

#find!(*args) ⇒ Object



13
14
15
16
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 13

def find!(*args)
  options = args.extract_options!
  klass.find!(*args << scoped_options(options))
end

#first(options = {}) ⇒ Object



26
27
28
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 26

def first(options={})
  klass.first(scoped_options(options))
end

#last(options = {}) ⇒ Object



30
31
32
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 30

def last(options={})
  klass.last(scoped_options(options))
end

#nullifyObject



85
86
87
88
89
90
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 85

def nullify
  all.each do |doc|
    doc.update_attributes(self.foreign_key => nil)
  end
  reset
end

#paginate(options) ⇒ Object



18
19
20
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 18

def paginate(options)
  klass.paginate(scoped_options(options))
end

#replace(docs) ⇒ Object



38
39
40
41
42
43
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 38

def replace(docs)
  load_target
  target.map(&:destroy)
  docs.each { |doc| prepare(doc).save }
  reset
end

#save_to_collection(options = {}) ⇒ Object



92
93
94
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 92

def save_to_collection(options={})
  @target.each { |doc| doc.save(options) } if @target
end