mongo_mapper 0.10.1 → 0.11.0
Sign up to get free protection for your applications and to get access to all the features.
- data/UPGRADES +3 -0
- data/examples/plugins.rb +2 -5
- data/lib/mongo_mapper.rb +1 -0
- data/lib/mongo_mapper/connection.rb +4 -0
- data/lib/mongo_mapper/embedded_document.rb +1 -0
- data/lib/mongo_mapper/extensions/object.rb +5 -6
- data/lib/mongo_mapper/plugins/accessible.rb +14 -16
- data/lib/mongo_mapper/plugins/associations.rb +21 -23
- data/lib/mongo_mapper/plugins/associations/base.rb +1 -1
- data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +4 -0
- data/lib/mongo_mapper/plugins/associations/many_association.rb +0 -4
- data/lib/mongo_mapper/plugins/associations/one_association.rb +1 -1
- data/lib/mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb +30 -0
- data/lib/mongo_mapper/plugins/caching.rb +11 -13
- data/lib/mongo_mapper/plugins/callbacks.rb +12 -14
- data/lib/mongo_mapper/plugins/clone.rb +11 -13
- data/lib/mongo_mapper/plugins/dirty.rb +36 -38
- data/lib/mongo_mapper/plugins/document.rb +20 -22
- data/lib/mongo_mapper/plugins/embedded_callbacks.rb +11 -13
- data/lib/mongo_mapper/plugins/embedded_document.rb +22 -24
- data/lib/mongo_mapper/plugins/equality.rb +6 -8
- data/lib/mongo_mapper/plugins/identity_map.rb +11 -13
- data/lib/mongo_mapper/plugins/inspect.rb +6 -8
- data/lib/mongo_mapper/plugins/keys.rb +109 -110
- data/lib/mongo_mapper/plugins/logger.rb +2 -4
- data/lib/mongo_mapper/plugins/modifiers.rb +32 -34
- data/lib/mongo_mapper/plugins/persistence.rb +5 -7
- data/lib/mongo_mapper/plugins/protected.rb +14 -16
- data/lib/mongo_mapper/plugins/querying.rb +29 -31
- data/lib/mongo_mapper/plugins/rails.rb +28 -22
- data/lib/mongo_mapper/plugins/rails/active_record_association_adapter.rb +33 -0
- data/lib/mongo_mapper/plugins/safe.rb +3 -5
- data/lib/mongo_mapper/plugins/sci.rb +3 -5
- data/lib/mongo_mapper/plugins/serialization.rb +49 -52
- data/lib/mongo_mapper/plugins/timestamps.rb +4 -6
- data/lib/mongo_mapper/plugins/validations.rb +8 -10
- data/lib/mongo_mapper/railtie/database.rake +1 -1
- data/lib/mongo_mapper/version.rb +1 -1
- data/lib/rails/generators/mongo_mapper/model/model_generator.rb +1 -1
- data/lib/rails/generators/mongo_mapper/model/templates/model.rb +2 -0
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +63 -0
- data/test/functional/associations/test_one_embedded_polymorphic_proxy.rb +208 -0
- data/test/functional/test_accessible.rb +5 -0
- data/test/functional/test_embedded_document.rb +12 -0
- data/test/functional/test_modifiers.rb +19 -19
- data/test/functional/test_protected.rb +10 -0
- data/test/functional/test_querying.rb +15 -1
- data/test/functional/test_validations.rb +33 -0
- data/test/models.rb +14 -0
- data/test/unit/associations/test_one_association.rb +11 -0
- data/test/unit/test_mongo_mapper.rb +9 -0
- data/test/unit/test_plugins.rb +2 -4
- data/test/unit/test_rails_reflect_on_association.rb +118 -0
- metadata +15 -11
@@ -10,23 +10,21 @@ module MongoMapper
|
|
10
10
|
define_model_callbacks :save, :create, :update, :destroy, :only => [:before, :after]
|
11
11
|
end
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
embedded_docs = []
|
13
|
+
def run_callbacks(callback, *args, &block)
|
14
|
+
embedded_docs = []
|
16
15
|
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
embedded_associations.each do |association|
|
17
|
+
embedded_docs += Array(get_proxy(association).send(:load_target))
|
18
|
+
end
|
20
19
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end
|
20
|
+
block = embedded_docs.inject(block) do |chain, doc|
|
21
|
+
if doc.class.respond_to?("_#{callback}_callbacks")
|
22
|
+
lambda { doc.run_callbacks(callback, *args, &chain) }
|
23
|
+
else
|
24
|
+
chain
|
27
25
|
end
|
28
|
-
super callback, *args, &block
|
29
26
|
end
|
27
|
+
super callback, *args, &block
|
30
28
|
end
|
31
29
|
end
|
32
30
|
end
|
@@ -18,37 +18,35 @@ module MongoMapper
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
end
|
21
|
+
def new?
|
22
|
+
_root_document.try(:new?) || @_new
|
23
|
+
end
|
25
24
|
|
26
|
-
|
27
|
-
|
28
|
-
|
25
|
+
def destroyed?
|
26
|
+
!!_root_document.try(:destroyed?)
|
27
|
+
end
|
29
28
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
end
|
29
|
+
def save(options={})
|
30
|
+
_root_document.try(:save, options).tap do |result|
|
31
|
+
persist(options) if result
|
34
32
|
end
|
33
|
+
end
|
35
34
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
end
|
35
|
+
def save!(options={})
|
36
|
+
valid? || raise(DocumentNotValid.new(self))
|
37
|
+
_root_document.try(:save!, options).tap do |result|
|
38
|
+
persist(options) if result
|
41
39
|
end
|
40
|
+
end
|
42
41
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
42
|
+
def persist(options={})
|
43
|
+
@_new = false
|
44
|
+
clear_changes if respond_to?(:clear_changes)
|
45
|
+
save_to_collection(options)
|
46
|
+
end
|
48
47
|
|
49
|
-
|
50
|
-
|
51
|
-
end
|
48
|
+
def _root_document
|
49
|
+
@_root_document ||= _parent_document.try(:_root_document)
|
52
50
|
end
|
53
51
|
end
|
54
52
|
end
|
@@ -10,15 +10,13 @@ module MongoMapper
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
alias :== :eql?
|
13
|
+
def eql?(other)
|
14
|
+
other.is_a?(self.class) && _id == other._id
|
15
|
+
end
|
16
|
+
alias :== :eql?
|
18
17
|
|
19
|
-
|
20
|
-
|
21
|
-
end
|
18
|
+
def hash
|
19
|
+
_id.hash
|
22
20
|
end
|
23
21
|
end
|
24
22
|
end
|
@@ -108,22 +108,20 @@ module MongoMapper
|
|
108
108
|
end
|
109
109
|
end
|
110
110
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
end
|
111
|
+
def identity_map
|
112
|
+
self.class.identity_map
|
113
|
+
end
|
115
114
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
end
|
120
|
-
result
|
115
|
+
def save(*args)
|
116
|
+
if result = super
|
117
|
+
identity_map[_id] = self if self.class.identity_map_on?
|
121
118
|
end
|
119
|
+
result
|
120
|
+
end
|
122
121
|
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
end
|
122
|
+
def delete
|
123
|
+
identity_map.delete(_id) if self.class.identity_map_on?
|
124
|
+
super
|
127
125
|
end
|
128
126
|
end
|
129
127
|
end
|
@@ -4,14 +4,12 @@ module MongoMapper
|
|
4
4
|
module Inspect
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
"#<#{self.class} #{attributes_as_nice_string}>"
|
14
|
-
end
|
7
|
+
def inspect(include_nil = false)
|
8
|
+
keys = include_nil ? key_names : attributes.keys
|
9
|
+
attributes_as_nice_string = keys.sort.collect do |name|
|
10
|
+
"#{name}: #{self[name].inspect}"
|
11
|
+
end.join(", ")
|
12
|
+
"#<#{self.class} #{attributes_as_nice_string}>"
|
15
13
|
end
|
16
14
|
end
|
17
15
|
end
|
@@ -159,151 +159,150 @@ module MongoMapper
|
|
159
159
|
end
|
160
160
|
end
|
161
161
|
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
end
|
162
|
+
def initialize(attrs={})
|
163
|
+
@_new = true
|
164
|
+
self.attributes = attrs
|
165
|
+
end
|
167
166
|
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
167
|
+
def initialize_from_database(attrs={})
|
168
|
+
@_new = false
|
169
|
+
load_from_database(attrs)
|
170
|
+
self
|
171
|
+
end
|
173
172
|
|
174
|
-
|
175
|
-
|
176
|
-
|
173
|
+
def persisted?
|
174
|
+
!new? && !destroyed?
|
175
|
+
end
|
177
176
|
|
178
|
-
|
179
|
-
|
177
|
+
def attributes=(attrs)
|
178
|
+
return if attrs.blank?
|
180
179
|
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
end
|
180
|
+
attrs.each_pair do |key, value|
|
181
|
+
if respond_to?(:"#{key}=")
|
182
|
+
self.send(:"#{key}=", value)
|
183
|
+
else
|
184
|
+
self[key] = value
|
187
185
|
end
|
188
186
|
end
|
187
|
+
end
|
189
188
|
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
189
|
+
def attributes
|
190
|
+
HashWithIndifferentAccess.new.tap do |attrs|
|
191
|
+
keys.select { |name,key| !self[key.name].nil? || key.type == ObjectId }.each do |name, key|
|
192
|
+
value = key.set(self[key.name])
|
193
|
+
attrs[name] = value
|
194
|
+
end
|
196
195
|
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
end
|
196
|
+
embedded_associations.each do |association|
|
197
|
+
if documents = instance_variable_get(association.ivar)
|
198
|
+
if association.is_a?(Associations::OneAssociation)
|
199
|
+
attrs[association.name] = documents.to_mongo
|
200
|
+
else
|
201
|
+
attrs[association.name] = documents.map { |document| document.to_mongo }
|
204
202
|
end
|
205
203
|
end
|
206
204
|
end
|
207
205
|
end
|
208
|
-
|
209
|
-
|
210
|
-
def assign(attrs={})
|
211
|
-
warn "[DEPRECATION] #assign is deprecated, use #attributes="
|
212
|
-
self.attributes = attrs
|
213
|
-
end
|
214
|
-
|
215
|
-
def update_attributes(attrs={})
|
216
|
-
self.attributes = attrs
|
217
|
-
save
|
218
|
-
end
|
206
|
+
end
|
207
|
+
alias :to_mongo :attributes
|
219
208
|
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
209
|
+
def assign(attrs={})
|
210
|
+
warn "[DEPRECATION] #assign is deprecated, use #attributes="
|
211
|
+
self.attributes = attrs
|
212
|
+
end
|
224
213
|
|
225
|
-
|
226
|
-
|
227
|
-
|
214
|
+
def update_attributes(attrs={})
|
215
|
+
self.attributes = attrs
|
216
|
+
save
|
217
|
+
end
|
228
218
|
|
229
|
-
|
230
|
-
|
231
|
-
|
219
|
+
def update_attributes!(attrs={})
|
220
|
+
self.attributes = attrs
|
221
|
+
save!
|
222
|
+
end
|
232
223
|
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
224
|
+
def update_attribute(name, value)
|
225
|
+
self.send(:"#{name}=", value)
|
226
|
+
save(:validate => false)
|
227
|
+
end
|
237
228
|
|
238
|
-
|
239
|
-
|
229
|
+
def id
|
230
|
+
_id
|
231
|
+
end
|
240
232
|
|
241
|
-
|
242
|
-
|
233
|
+
def id=(value)
|
234
|
+
if self.class.using_object_id?
|
235
|
+
value = ObjectId.to_mongo(value)
|
243
236
|
end
|
244
237
|
|
245
|
-
|
246
|
-
|
247
|
-
write_key(name, value)
|
248
|
-
end
|
238
|
+
self[:_id] = value
|
239
|
+
end
|
249
240
|
|
250
|
-
|
251
|
-
|
252
|
-
|
241
|
+
def [](name)
|
242
|
+
read_key(name)
|
243
|
+
end
|
253
244
|
|
254
|
-
|
255
|
-
|
256
|
-
|
245
|
+
def []=(name, value)
|
246
|
+
ensure_key_exists(name)
|
247
|
+
write_key(name, value)
|
248
|
+
end
|
257
249
|
|
258
|
-
|
259
|
-
|
260
|
-
|
250
|
+
def keys
|
251
|
+
self.class.keys
|
252
|
+
end
|
261
253
|
|
262
|
-
|
263
|
-
|
264
|
-
|
254
|
+
def key_names
|
255
|
+
keys.keys
|
256
|
+
end
|
265
257
|
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
attrs.each do |key, value|
|
270
|
-
if respond_to?(:"#{key}=") && !self.class.key?(key)
|
271
|
-
self.send(:"#{key}=", value)
|
272
|
-
else
|
273
|
-
self[key] = value
|
274
|
-
end
|
275
|
-
end
|
276
|
-
end
|
258
|
+
def non_embedded_keys
|
259
|
+
keys.values.select { |key| !key.embeddable? }
|
260
|
+
end
|
277
261
|
|
278
|
-
|
279
|
-
|
280
|
-
|
262
|
+
def embedded_keys
|
263
|
+
keys.values.select { |key| key.embeddable? }
|
264
|
+
end
|
281
265
|
|
282
|
-
|
283
|
-
|
284
|
-
|
266
|
+
private
|
267
|
+
def load_from_database(attrs)
|
268
|
+
return if attrs.blank?
|
269
|
+
attrs.each do |key, value|
|
270
|
+
if respond_to?(:"#{key}=") && !self.class.key?(key)
|
271
|
+
self.send(:"#{key}=", value)
|
272
|
+
else
|
273
|
+
self[key] = value
|
285
274
|
end
|
286
275
|
end
|
276
|
+
end
|
287
277
|
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
set_parent_document(key, value)
|
292
|
-
instance_variable_set(:"@#{key_name}", value)
|
293
|
-
end
|
294
|
-
end
|
278
|
+
def ensure_key_exists(name)
|
279
|
+
self.class.key(name) unless respond_to?("#{name}=")
|
280
|
+
end
|
295
281
|
|
296
|
-
|
297
|
-
|
282
|
+
def set_parent_document(key, value)
|
283
|
+
if key.embeddable? && value.is_a?(key.type)
|
284
|
+
value._parent_document = self
|
298
285
|
end
|
286
|
+
end
|
299
287
|
|
300
|
-
|
301
|
-
|
288
|
+
def read_key(key_name)
|
289
|
+
if key = keys[key_name.to_s]
|
290
|
+
value = key.get(instance_variable_get(:"@#{key_name}"))
|
302
291
|
set_parent_document(key, value)
|
303
|
-
instance_variable_set
|
304
|
-
instance_variable_set :"@#{name}", key.set(value)
|
292
|
+
instance_variable_set(:"@#{key_name}", value)
|
305
293
|
end
|
306
|
-
|
294
|
+
end
|
295
|
+
|
296
|
+
def read_key_before_type_cast(name)
|
297
|
+
instance_variable_get(:"@#{name}_before_type_cast")
|
298
|
+
end
|
299
|
+
|
300
|
+
def write_key(name, value)
|
301
|
+
key = keys[name.to_s]
|
302
|
+
set_parent_document(key, value)
|
303
|
+
instance_variable_set :"@#{name}_before_type_cast", value
|
304
|
+
instance_variable_set :"@#{name}", key.set(value)
|
305
|
+
end
|
307
306
|
end
|
308
307
|
end
|
309
308
|
end
|
@@ -75,47 +75,45 @@ module MongoMapper
|
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
end
|
78
|
+
def unset(*keys)
|
79
|
+
self.class.unset(id, *keys)
|
80
|
+
end
|
82
81
|
|
83
|
-
|
84
|
-
|
85
|
-
|
82
|
+
def increment(hash)
|
83
|
+
self.class.increment(id, hash)
|
84
|
+
end
|
86
85
|
|
87
|
-
|
88
|
-
|
89
|
-
|
86
|
+
def decrement(hash)
|
87
|
+
self.class.decrement(id, hash)
|
88
|
+
end
|
90
89
|
|
91
|
-
|
92
|
-
|
93
|
-
|
90
|
+
def set(hash)
|
91
|
+
self.class.set(id, hash)
|
92
|
+
end
|
94
93
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
def push_all(hash)
|
100
|
-
self.class.push_all(id, hash)
|
101
|
-
end
|
94
|
+
def push(hash)
|
95
|
+
self.class.push(id, hash)
|
96
|
+
end
|
102
97
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
def pull_all(hash)
|
108
|
-
self.class.pull_all(id, hash)
|
109
|
-
end
|
98
|
+
def push_all(hash)
|
99
|
+
self.class.push_all(id, hash)
|
100
|
+
end
|
110
101
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
alias push_uniq add_to_set
|
102
|
+
def pull(hash)
|
103
|
+
self.class.pull(id, hash)
|
104
|
+
end
|
115
105
|
|
116
|
-
|
117
|
-
|
118
|
-
|
106
|
+
def pull_all(hash)
|
107
|
+
self.class.pull_all(id, hash)
|
108
|
+
end
|
109
|
+
|
110
|
+
def add_to_set(hash)
|
111
|
+
self.class.push_uniq(id, hash)
|
112
|
+
end
|
113
|
+
alias push_uniq add_to_set
|
114
|
+
|
115
|
+
def pop(hash)
|
116
|
+
self.class.pop(id, hash)
|
119
117
|
end
|
120
118
|
end
|
121
119
|
end
|