Class: MongoMapper::Plugins::Associations::InArrayProxy
Instance Attribute Summary
Attributes inherited from Proxy
#association, #proxy_owner, #target
Instance Method Summary
collapse
#dynamic_find
Methods inherited from Collection
#include?, #reset, #to_ary
Methods inherited from Proxy
#===, #as_json, #blank?, #initialize, #inspect, #loaded, #loaded?, #nil?, #present?, #proxy_respond_to?, #reload, #reset, #respond_to?, #send, #to_json
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
99
100
101
102
103
104
105
106
107
|
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 99
def <<(*docs)
flatten_deeper(docs).each do |doc|
doc.save if doc.new?
unless ids.include?(doc.id)
ids << doc.id
end
end
reset
end
|
#all(options = {}) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 21
def all(options={})
return [] if ids.blank?
default_results = query(options).all
if association.options[:preserve_order]
ordered_results = Array.new
criteria[:_id].each do |id|
default_results.each do |element|
if id.eql?(element._id)
ordered_results << element
break
end
end
end
ordered_results
else
default_results
end
end
|
#count(options = {}) ⇒ Object
54
55
56
57
|
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 54
def count(options={})
return 0 if ids.blank?
options.blank? ? ids.size : query(options).count
end
|
#create(attrs = {}) ⇒ Object
79
80
81
82
83
84
85
86
87
|
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 79
def create(attrs={})
doc = klass.create(attrs)
unless doc.new?
ids << doc.id
proxy_owner.save
reset
end
doc
end
|
#create!(attrs = {}) ⇒ Object
89
90
91
92
93
94
95
96
97
|
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 89
def create!(attrs={})
doc = klass.create!(attrs)
unless doc.new?
ids << doc.id
proxy_owner.save
reset
end
doc
end
|
#delete_all(options = {}) ⇒ Object
67
68
69
70
71
72
|
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 67
def delete_all(options={})
docs = query(options).fields(:_id).all
docs.each { |doc| ids.delete(doc.id) }
klass.delete(docs.map(&:id))
reset
end
|
#destroy_all(options = {}) ⇒ Object
59
60
61
62
63
64
65
|
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 59
def destroy_all(options={})
all(options).each do |doc|
ids.delete(doc.id)
doc.destroy
end
reset
end
|
#find(*args) ⇒ Object
8
9
10
|
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 8
def find(*args)
query.find(*scoped_ids(args))
end
|
#find!(*args) ⇒ Object
12
13
14
|
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 12
def find!(*args)
query.find!(*scoped_ids(args))
end
|
#first(options = {}) ⇒ Object
44
45
46
47
|
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 44
def first(options={})
return nil if ids.blank?
query(options).first
end
|
#last(options = {}) ⇒ Object
49
50
51
52
|
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 49
def last(options={})
return nil if ids.blank?
query(options).last
end
|
74
75
76
77
|
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 74
def nullify
replace([])
reset
end
|
#paginate(options) ⇒ Object
16
17
18
19
|
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 16
def paginate(options)
return [] if ids.blank?
query.paginate(options)
end
|
#replace(docs) ⇒ Object
111
112
113
114
115
116
117
118
|
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 111
def replace(docs)
doc_ids = docs.map do |doc|
doc.save if doc.new?
doc.id
end
ids.replace(doc_ids.uniq)
reset
end
|