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