mongo_mapper 0.13.0 → 0.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/LICENSE +1 -1
- data/README.md +61 -0
- data/examples/keys.rb +1 -1
- data/examples/modifiers/set.rb +1 -1
- data/examples/querying.rb +1 -1
- data/examples/safe.rb +2 -2
- data/examples/scopes.rb +1 -1
- data/lib/mongo_mapper.rb +7 -0
- data/lib/mongo_mapper/connection.rb +16 -37
- data/lib/mongo_mapper/document.rb +4 -0
- data/lib/mongo_mapper/extensions/array.rb +14 -6
- data/lib/mongo_mapper/extensions/hash.rb +15 -3
- data/lib/mongo_mapper/extensions/object.rb +4 -0
- data/lib/mongo_mapper/extensions/object_id.rb +5 -1
- data/lib/mongo_mapper/extensions/string.rb +13 -5
- data/lib/mongo_mapper/extensions/symbol.rb +18 -0
- data/lib/mongo_mapper/plugins/accessible.rb +15 -5
- data/lib/mongo_mapper/plugins/associations.rb +7 -6
- data/lib/mongo_mapper/plugins/associations/base.rb +27 -14
- data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +10 -1
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +9 -8
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +12 -11
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +4 -4
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +60 -29
- data/lib/mongo_mapper/plugins/associations/in_foreign_array_proxy.rb +136 -0
- data/lib/mongo_mapper/plugins/associations/many_association.rb +4 -2
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +18 -16
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +55 -48
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +14 -13
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +7 -6
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +7 -5
- data/lib/mongo_mapper/plugins/associations/one_as_proxy.rb +14 -11
- data/lib/mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb +14 -13
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +9 -9
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +27 -26
- data/lib/mongo_mapper/plugins/associations/proxy.rb +36 -29
- data/lib/mongo_mapper/plugins/associations/single_association.rb +5 -4
- data/lib/mongo_mapper/plugins/callbacks.rb +13 -0
- data/lib/mongo_mapper/plugins/counter_cache.rb +97 -0
- data/lib/mongo_mapper/plugins/dirty.rb +29 -37
- data/lib/mongo_mapper/plugins/document.rb +1 -1
- data/lib/mongo_mapper/plugins/dynamic_querying.rb +10 -9
- data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +18 -17
- data/lib/mongo_mapper/plugins/embedded_callbacks.rb +2 -1
- data/lib/mongo_mapper/plugins/embedded_document.rb +1 -1
- data/lib/mongo_mapper/plugins/identity_map.rb +4 -2
- data/lib/mongo_mapper/plugins/indexes.rb +14 -7
- data/lib/mongo_mapper/plugins/keys.rb +170 -151
- data/lib/mongo_mapper/plugins/keys/key.rb +27 -16
- data/lib/mongo_mapper/plugins/keys/static.rb +45 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +64 -38
- data/lib/mongo_mapper/plugins/partial_updates.rb +86 -0
- data/lib/mongo_mapper/plugins/persistence.rb +13 -8
- data/lib/mongo_mapper/plugins/protected.rb +6 -5
- data/lib/mongo_mapper/plugins/querying.rb +85 -42
- data/lib/mongo_mapper/plugins/querying/decorated_plucky_query.rb +20 -15
- data/lib/mongo_mapper/plugins/rails.rb +1 -0
- data/lib/mongo_mapper/plugins/safe.rb +10 -4
- data/lib/mongo_mapper/plugins/sci.rb +0 -0
- data/lib/mongo_mapper/plugins/scopes.rb +78 -7
- data/lib/mongo_mapper/plugins/stats.rb +17 -0
- data/lib/mongo_mapper/plugins/strong_parameters.rb +26 -0
- data/lib/mongo_mapper/plugins/timestamps.rb +1 -0
- data/lib/mongo_mapper/plugins/validations.rb +1 -1
- data/lib/mongo_mapper/railtie.rb +4 -3
- data/lib/mongo_mapper/utils.rb +2 -2
- data/lib/mongo_mapper/version.rb +1 -1
- data/lib/rails/generators/mongo_mapper/config/config_generator.rb +12 -13
- data/lib/rails/generators/mongo_mapper/model/model_generator.rb +9 -9
- data/spec/examples.txt +1717 -0
- data/spec/functional/accessible_spec.rb +19 -13
- data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +13 -13
- data/spec/functional/associations/belongs_to_proxy_spec.rb +36 -20
- data/spec/functional/associations/in_array_proxy_spec.rb +145 -10
- data/spec/functional/associations/in_foreign_array_proxy_spec.rb +321 -0
- data/spec/functional/associations/many_documents_as_proxy_spec.rb +6 -6
- data/spec/functional/associations/many_documents_proxy_spec.rb +85 -14
- data/spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb +13 -13
- data/spec/functional/associations/many_embedded_proxy_spec.rb +1 -1
- data/spec/functional/associations/many_polymorphic_proxy_spec.rb +4 -4
- data/spec/functional/associations/one_as_proxy_spec.rb +10 -10
- data/spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb +9 -9
- data/spec/functional/associations/one_embedded_proxy_spec.rb +3 -3
- data/spec/functional/associations/one_proxy_spec.rb +10 -10
- data/spec/functional/associations_spec.rb +3 -3
- data/spec/functional/binary_spec.rb +2 -2
- data/spec/functional/caching_spec.rb +8 -15
- data/spec/functional/callbacks_spec.rb +89 -2
- data/spec/functional/counter_cache_spec.rb +235 -0
- data/spec/functional/dirty_spec.rb +63 -46
- data/spec/functional/document_spec.rb +30 -5
- data/spec/functional/dumpable_spec.rb +1 -1
- data/spec/functional/embedded_document_spec.rb +17 -17
- data/spec/functional/identity_map_spec.rb +29 -16
- data/spec/functional/indexes_spec.rb +19 -18
- data/spec/functional/keys_spec.rb +86 -28
- data/spec/functional/logger_spec.rb +3 -3
- data/spec/functional/modifiers_spec.rb +81 -19
- data/spec/functional/partial_updates_spec.rb +577 -0
- data/spec/functional/protected_spec.rb +14 -14
- data/spec/functional/querying_spec.rb +77 -28
- data/spec/functional/safe_spec.rb +23 -27
- data/spec/functional/sci_spec.rb +9 -9
- data/spec/functional/scopes_spec.rb +235 -2
- data/spec/functional/static_keys_spec.rb +153 -0
- data/spec/functional/stats_spec.rb +86 -0
- data/spec/functional/strong_parameters_spec.rb +49 -0
- data/spec/functional/touch_spec.rb +1 -1
- data/spec/functional/validations_spec.rb +51 -57
- data/spec/quality_spec.rb +51 -0
- data/spec/spec_helper.rb +37 -9
- data/spec/support/matchers.rb +5 -14
- data/spec/unit/associations/base_spec.rb +12 -12
- data/spec/unit/associations/belongs_to_association_spec.rb +2 -2
- data/spec/unit/associations/many_association_spec.rb +2 -2
- data/spec/unit/associations/one_association_spec.rb +2 -2
- data/spec/unit/associations/proxy_spec.rb +19 -20
- data/spec/unit/clone_spec.rb +1 -1
- data/spec/unit/document_spec.rb +8 -8
- data/spec/unit/dynamic_finder_spec.rb +8 -8
- data/spec/unit/embedded_document_spec.rb +18 -19
- data/spec/unit/extensions_spec.rb +41 -17
- data/spec/unit/identity_map_middleware_spec.rb +65 -96
- data/spec/unit/key_spec.rb +28 -26
- data/spec/unit/keys_spec.rb +20 -11
- data/spec/unit/model_generator_spec.rb +0 -0
- data/spec/unit/mongo_mapper_spec.rb +38 -85
- data/spec/unit/rails_spec.rb +5 -0
- data/spec/unit/serialization_spec.rb +1 -1
- data/spec/unit/time_zones_spec.rb +2 -2
- data/spec/unit/validations_spec.rb +46 -33
- metadata +66 -37
- data/README.rdoc +0 -59
- data/lib/mongo_mapper/connections/10gen.rb +0 -0
- data/lib/mongo_mapper/connections/moped.rb +0 -0
- data/lib/mongo_mapper/extensions/ordered_hash.rb +0 -23
@@ -148,11 +148,11 @@ describe "Callbacks" do
|
|
148
148
|
@root_class.define_callbacks :after_publish
|
149
149
|
@root_class.after_save { |d| d.run_callbacks(:after_publish) }
|
150
150
|
|
151
|
-
|
151
|
+
lambda {
|
152
152
|
child = @child_class.new(:name => 'Child')
|
153
153
|
root = @root_class.create(:name => 'Parent', :children => [child])
|
154
154
|
child.history.should_not include(:after_publish)
|
155
|
-
}.
|
155
|
+
}.should_not raise_error
|
156
156
|
end
|
157
157
|
end
|
158
158
|
|
@@ -240,4 +240,91 @@ describe "Callbacks" do
|
|
240
240
|
doc.message.should == 'Ho!'
|
241
241
|
end
|
242
242
|
end
|
243
|
+
|
244
|
+
describe "after_find" do
|
245
|
+
before do
|
246
|
+
@found_objects = []
|
247
|
+
found_objects = @found_objects # use a local for closure
|
248
|
+
|
249
|
+
@doc_class = Doc("User") do
|
250
|
+
after_find :set_found_object
|
251
|
+
|
252
|
+
define_method :set_found_object do
|
253
|
+
found_objects << self
|
254
|
+
end
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
it "should run after finding an object with find!" do
|
259
|
+
@doc = @doc_class.create!
|
260
|
+
|
261
|
+
@doc_class.find!(@doc.id)
|
262
|
+
@found_objects.should == [@doc]
|
263
|
+
end
|
264
|
+
|
265
|
+
it "should not have run if nothing was queried" do
|
266
|
+
@found_objects.should == []
|
267
|
+
end
|
268
|
+
|
269
|
+
it "should run for multiple objects" do
|
270
|
+
@doc1 = @doc_class.create!
|
271
|
+
@doc2 = @doc_class.create!
|
272
|
+
|
273
|
+
@doc_class.all
|
274
|
+
@found_objects.should == [@doc1, @doc2]
|
275
|
+
end
|
276
|
+
|
277
|
+
it "should run after finding an object through the query proxy" do
|
278
|
+
@doc = @doc_class.create!
|
279
|
+
@doc_class.where(:_id => @doc.id).first
|
280
|
+
@found_objects.should == [@doc]
|
281
|
+
end
|
282
|
+
|
283
|
+
it "should still return the object" do
|
284
|
+
@doc = @doc_class.create!
|
285
|
+
@doc_class.where(:_id => @doc.id).first.should == @doc
|
286
|
+
end
|
287
|
+
|
288
|
+
it "should not bail if the method return false" do
|
289
|
+
@doc_class = Doc("User") do
|
290
|
+
after_find :set_found_object
|
291
|
+
|
292
|
+
define_method :set_found_object do
|
293
|
+
false
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
297
|
+
@doc = @doc_class.create!
|
298
|
+
@doc_class.where(:_id => @doc.id).first.should == @doc
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
302
|
+
describe "after_initialize" do
|
303
|
+
before do
|
304
|
+
@objects = []
|
305
|
+
objects = @objects
|
306
|
+
|
307
|
+
@doc_class = Doc("User") do
|
308
|
+
after_initialize :set_initialized_object
|
309
|
+
|
310
|
+
define_method :set_initialized_object do
|
311
|
+
objects << self
|
312
|
+
end
|
313
|
+
end
|
314
|
+
end
|
315
|
+
|
316
|
+
it "should be triggered for objects created with new" do
|
317
|
+
@objects.should == []
|
318
|
+
obj = @doc_class.new
|
319
|
+
@objects.should == [obj]
|
320
|
+
end
|
321
|
+
|
322
|
+
it "should be triggered for objects found in the db" do
|
323
|
+
@doc = @doc_class.create!
|
324
|
+
@objects.clear # don't re-assign as we want the operation to be in place
|
325
|
+
|
326
|
+
@doc_class.all
|
327
|
+
@objects.should == [@doc]
|
328
|
+
end
|
329
|
+
end
|
243
330
|
end
|
@@ -0,0 +1,235 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module CounterCacheFixtureModels
|
4
|
+
class User
|
5
|
+
include MongoMapper::Document
|
6
|
+
|
7
|
+
key :posts_count, Integer, :default => 0
|
8
|
+
|
9
|
+
has_many :posts,
|
10
|
+
:class_name => "CounterCacheFixtureModels::Post"
|
11
|
+
end
|
12
|
+
|
13
|
+
class Post
|
14
|
+
include MongoMapper::Document
|
15
|
+
|
16
|
+
key :comments_count, Integer, :default => 0
|
17
|
+
key :some_custom_comments_count, Integer, :default => 0
|
18
|
+
key :commentable_count, Integer, :default => 0
|
19
|
+
|
20
|
+
has_many :comments,
|
21
|
+
:class_name => "CounterCacheFixtureModels::Comment"
|
22
|
+
|
23
|
+
belongs_to :user,
|
24
|
+
:counter_cache => true,
|
25
|
+
:class_name => "CounterCacheFixtureModels::User"
|
26
|
+
|
27
|
+
many :polymorphic_comments,
|
28
|
+
:as => :commentable,
|
29
|
+
:class_name => "CounterCacheFixtureModels::Comment"
|
30
|
+
end
|
31
|
+
|
32
|
+
class Article
|
33
|
+
include MongoMapper::Document
|
34
|
+
|
35
|
+
key :commentable_count, Integer, :default => 0
|
36
|
+
|
37
|
+
many :polymorphic_comments,
|
38
|
+
:as => :commentable,
|
39
|
+
:class_name => "CounterCacheFixtureModels::Comment"
|
40
|
+
end
|
41
|
+
|
42
|
+
class Comment
|
43
|
+
include MongoMapper::Document
|
44
|
+
|
45
|
+
belongs_to :post,
|
46
|
+
:counter_cache => true,
|
47
|
+
:class_name => "CounterCacheFixtureModels::Post"
|
48
|
+
|
49
|
+
belongs_to :commentable,
|
50
|
+
:polymorphic => true,
|
51
|
+
:counter_cache => :commentable_count
|
52
|
+
end
|
53
|
+
|
54
|
+
class CustomComment
|
55
|
+
include MongoMapper::Document
|
56
|
+
|
57
|
+
belongs_to :post,
|
58
|
+
:counter_cache => :some_custom_comments_count,
|
59
|
+
:class_name => "CounterCacheFixtureModels::Post"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe MongoMapper::Plugins::CounterCache do
|
64
|
+
before do
|
65
|
+
@post_class = CounterCacheFixtureModels::Post
|
66
|
+
@comment_class = CounterCacheFixtureModels::Comment
|
67
|
+
@user_class = CounterCacheFixtureModels::User
|
68
|
+
@custom_comment_class = CounterCacheFixtureModels::CustomComment
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should have a key with posts_count defaulting to 0" do
|
72
|
+
@post_class.new.comments_count.should == 0
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should update the count when a new object is created" do
|
76
|
+
post = @post_class.new
|
77
|
+
comment = @comment_class.new
|
78
|
+
|
79
|
+
post.save!
|
80
|
+
|
81
|
+
comment.post = post
|
82
|
+
comment.save!
|
83
|
+
|
84
|
+
post.reload
|
85
|
+
post.comments_count.should == 1
|
86
|
+
|
87
|
+
second_comment = @comment_class.new
|
88
|
+
second_comment.post = post
|
89
|
+
second_comment.save!
|
90
|
+
|
91
|
+
post.reload
|
92
|
+
post.comments_count.should == 2
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should decrease the count by one when an object is destroyed" do
|
96
|
+
post = @post_class.new
|
97
|
+
comment = @comment_class.new
|
98
|
+
|
99
|
+
post.save!
|
100
|
+
|
101
|
+
comment.post = post
|
102
|
+
comment.save!
|
103
|
+
|
104
|
+
post.reload
|
105
|
+
post.comments_count.should == 1
|
106
|
+
|
107
|
+
comment.destroy
|
108
|
+
post.reload
|
109
|
+
post.comments_count.should == 0
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should use the correct association name" do
|
113
|
+
@user = @user_class.new
|
114
|
+
@post = @post_class.new
|
115
|
+
|
116
|
+
@user.save!
|
117
|
+
@post.user = @user
|
118
|
+
@post.save!
|
119
|
+
|
120
|
+
@user.reload
|
121
|
+
@user.posts_count.should == 1
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should be able to use a custom field name" do
|
125
|
+
@post = @post_class.new
|
126
|
+
@custom_comment = @custom_comment_class.new
|
127
|
+
|
128
|
+
@post.save!
|
129
|
+
@custom_comment.post = @post
|
130
|
+
@custom_comment.save!
|
131
|
+
|
132
|
+
@post.reload
|
133
|
+
@post.some_custom_comments_count.should == 1
|
134
|
+
end
|
135
|
+
|
136
|
+
it "should thrown an error if there is no association" do
|
137
|
+
lambda {
|
138
|
+
CounterCacheFixtureModels.module_eval do
|
139
|
+
class CommentWithInvalidAssociation
|
140
|
+
include MongoMapper::Document
|
141
|
+
|
142
|
+
belongs_to :post,
|
143
|
+
:class_name => "CounterCacheFixtureModels::Post"
|
144
|
+
|
145
|
+
counter_cache :foo
|
146
|
+
end
|
147
|
+
end
|
148
|
+
}.should raise_error(MongoMapper::Plugins::CounterCache::InvalidCounterCacheError, "You must define an association with name `foo' on model CommentWithInvalidAssociation")
|
149
|
+
end
|
150
|
+
|
151
|
+
it "should thown a sensible error if the field is not defined on the target object" do
|
152
|
+
lambda {
|
153
|
+
CounterCacheFixtureModels.module_eval do
|
154
|
+
class CommentWithBadRefenceField
|
155
|
+
include MongoMapper::Document
|
156
|
+
|
157
|
+
belongs_to :post,
|
158
|
+
:class_name => "CounterCacheFixtureModels::Post"
|
159
|
+
|
160
|
+
counter_cache :post, :field => :invalid_field
|
161
|
+
end
|
162
|
+
end
|
163
|
+
}.should raise_error(MongoMapper::Plugins::CounterCache::InvalidCounterCacheError, "Missing `key :invalid_field, Integer, :default => 0' on model CounterCacheFixtureModels::Post")
|
164
|
+
end
|
165
|
+
|
166
|
+
describe "with polymorphic associations" do
|
167
|
+
before do
|
168
|
+
@article = CounterCacheFixtureModels::Article.new
|
169
|
+
@comment = CounterCacheFixtureModels::Comment.new
|
170
|
+
@comment.commentable = @article
|
171
|
+
end
|
172
|
+
|
173
|
+
it "should update the counter cache on save" do
|
174
|
+
lambda {
|
175
|
+
@comment.save!
|
176
|
+
@article.reload
|
177
|
+
}.should change(@article, :commentable_count).by(1)
|
178
|
+
end
|
179
|
+
|
180
|
+
it "should increment with a second object" do
|
181
|
+
@comment.save!
|
182
|
+
|
183
|
+
lambda {
|
184
|
+
second_comment = CounterCacheFixtureModels::Comment.new
|
185
|
+
second_comment.commentable = @article
|
186
|
+
second_comment.save!
|
187
|
+
@article.reload
|
188
|
+
}.should change(@article, :commentable_count).by(1)
|
189
|
+
end
|
190
|
+
|
191
|
+
it "should decrement the counter cache on destroy" do
|
192
|
+
@comment.save!
|
193
|
+
|
194
|
+
lambda {
|
195
|
+
@comment.destroy
|
196
|
+
@article.reload
|
197
|
+
}.should change(@article, :commentable_count).by(-1)
|
198
|
+
end
|
199
|
+
|
200
|
+
it "should increment with a different type of object" do
|
201
|
+
@comment.save!
|
202
|
+
|
203
|
+
lambda {
|
204
|
+
second_comment = CounterCacheFixtureModels::Comment.new
|
205
|
+
second_comment.commentable = @article
|
206
|
+
second_comment.save!
|
207
|
+
|
208
|
+
@article.reload
|
209
|
+
}.should change(@article, :commentable_count).by(1)
|
210
|
+
end
|
211
|
+
|
212
|
+
describe "without a counter cache field" do
|
213
|
+
before do
|
214
|
+
@comment = CounterCacheFixtureModels::Comment.new
|
215
|
+
@klass = Class.new do
|
216
|
+
include MongoMapper::Document
|
217
|
+
|
218
|
+
many :polymorphic_comments,
|
219
|
+
:as => :commentable,
|
220
|
+
:class_name => "CounterCacheFixtureModels::Comment"
|
221
|
+
end
|
222
|
+
|
223
|
+
@obj = @klass.new
|
224
|
+
end
|
225
|
+
|
226
|
+
it "should raise at save (runtime) if there is no counter cache field" do
|
227
|
+
@comment.commentable = @obj
|
228
|
+
|
229
|
+
lambda {
|
230
|
+
@comment.save!
|
231
|
+
}.should raise_error(MongoMapper::Plugins::CounterCache::InvalidCounterCacheError)
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
235
|
+
end
|
@@ -9,40 +9,47 @@ describe "Dirty" do
|
|
9
9
|
end
|
10
10
|
|
11
11
|
context "marking changes" do
|
12
|
+
it "should have the changed? methods" do
|
13
|
+
obj = @document.new
|
14
|
+
obj.should respond_to(:changed?)
|
15
|
+
obj.should respond_to(:phrase_changed?)
|
16
|
+
obj.should respond_to(:paragraph_changed?)
|
17
|
+
end
|
18
|
+
|
12
19
|
it "should not happen if there are none" do
|
13
20
|
doc = @document.new
|
14
|
-
doc.phrase_changed?.should
|
21
|
+
doc.phrase_changed?.should be_falsey
|
15
22
|
doc.phrase_change.should be_nil
|
16
23
|
end
|
17
24
|
|
18
25
|
it "should happen when change happens" do
|
19
26
|
doc = @document.new
|
20
27
|
doc.phrase = 'Golly Gee Willikers Batman'
|
21
|
-
doc.phrase_changed?.should
|
28
|
+
doc.phrase_changed?.should be_truthy
|
22
29
|
doc.phrase_was.should be_nil
|
23
30
|
doc.phrase_change.should == [nil, 'Golly Gee Willikers Batman']
|
24
31
|
end
|
25
32
|
|
26
33
|
it "should happen when initializing" do
|
27
34
|
doc = @document.new(:phrase => 'Foo')
|
28
|
-
doc.changed?.should
|
35
|
+
doc.changed?.should be_truthy
|
29
36
|
end
|
30
37
|
|
31
38
|
it "should clear changes on save" do
|
32
39
|
doc = @document.new
|
33
40
|
doc.phrase = 'Golly Gee Willikers Batman'
|
34
|
-
doc.phrase_changed?.should
|
41
|
+
doc.phrase_changed?.should be_truthy
|
35
42
|
doc.save
|
36
|
-
doc.phrase_changed?.should_not
|
43
|
+
doc.phrase_changed?.should_not be_truthy
|
37
44
|
doc.phrase_change.should be_nil
|
38
45
|
end
|
39
46
|
|
40
47
|
it "should clear changes on save!" do
|
41
48
|
doc = @document.new
|
42
49
|
doc.phrase = 'Golly Gee Willikers Batman'
|
43
|
-
doc.phrase_changed?.should
|
50
|
+
doc.phrase_changed?.should be_truthy
|
44
51
|
doc.save!
|
45
|
-
doc.phrase_changed?.should_not
|
52
|
+
doc.phrase_changed?.should_not be_truthy
|
46
53
|
doc.phrase_change.should be_nil
|
47
54
|
end
|
48
55
|
|
@@ -51,33 +58,33 @@ describe "Dirty" do
|
|
51
58
|
@document.any_instance.should_receive(:attribute_will_change!).never
|
52
59
|
@document.any_instance.should_receive(:attribute_changed?).never
|
53
60
|
doc = @document.find(doc.id)
|
54
|
-
doc.changed?.should
|
61
|
+
doc.changed?.should be_falsey
|
55
62
|
end
|
56
63
|
|
57
64
|
it "should not happen when reloading from database" do
|
58
65
|
doc = @document.create(:phrase => 'Foo')
|
59
66
|
doc = @document.find(doc.id)
|
60
67
|
|
61
|
-
doc.changed?.should
|
68
|
+
doc.changed?.should be_falsey
|
62
69
|
doc.phrase = 'Fart'
|
63
|
-
doc.changed?.should
|
70
|
+
doc.changed?.should be_truthy
|
64
71
|
doc.reload
|
65
|
-
doc.changed?.should
|
72
|
+
doc.changed?.should be_falsey
|
66
73
|
end
|
67
74
|
|
68
75
|
it "should happen if changed after loading from database" do
|
69
76
|
doc = @document.create(:phrase => 'Foo')
|
70
77
|
doc.reload
|
71
|
-
doc.changed?.should
|
78
|
+
doc.changed?.should be_falsey
|
72
79
|
doc.phrase = 'Bar'
|
73
|
-
doc.changed?.should
|
80
|
+
doc.changed?.should be_truthy
|
74
81
|
end
|
75
82
|
|
76
83
|
it "should happen with aliased keys" do
|
77
84
|
doc = @document.create(:paragraph => 'Wibbly')
|
78
85
|
doc.paragraph = "Wobbly"
|
79
|
-
doc.changed?.should
|
80
|
-
doc.paragraph_changed?.should
|
86
|
+
doc.changed?.should be_truthy
|
87
|
+
doc.paragraph_changed?.should be_truthy
|
81
88
|
end
|
82
89
|
end
|
83
90
|
|
@@ -88,7 +95,7 @@ describe "Dirty" do
|
|
88
95
|
[nil, ''].each do |value|
|
89
96
|
doc = @document.new
|
90
97
|
doc.age = value
|
91
|
-
doc.age_changed?.should
|
98
|
+
doc.age_changed?.should be_falsey
|
92
99
|
doc.age_change.should be_nil
|
93
100
|
end
|
94
101
|
end
|
@@ -101,7 +108,7 @@ describe "Dirty" do
|
|
101
108
|
[nil, ''].each do |value|
|
102
109
|
doc = @document.new
|
103
110
|
doc.amount = value
|
104
|
-
doc.amount_changed?.should
|
111
|
+
doc.amount_changed?.should be_falsey
|
105
112
|
doc.amount_change.should be_nil
|
106
113
|
end
|
107
114
|
end
|
@@ -111,27 +118,27 @@ describe "Dirty" do
|
|
111
118
|
it "should be true if key changed" do
|
112
119
|
doc = @document.new
|
113
120
|
doc.phrase = 'A penny saved is a penny earned.'
|
114
|
-
doc.changed?.should
|
121
|
+
doc.changed?.should be_truthy
|
115
122
|
end
|
116
123
|
|
117
124
|
it "should be false if no keys changed" do
|
118
|
-
@document.new.changed?.should
|
125
|
+
@document.new.changed?.should be_falsey
|
119
126
|
end
|
120
127
|
|
121
128
|
it "should not raise when key name is 'value'" do
|
122
129
|
@document.key :value, Integer
|
123
130
|
|
124
131
|
doc = @document.new
|
125
|
-
doc.value_changed?.should
|
132
|
+
doc.value_changed?.should be_falsey
|
126
133
|
end
|
127
134
|
|
128
135
|
it "should be false if the same ObjectId was assigned in String format" do
|
129
136
|
@document.key :doc_id, ObjectId
|
130
137
|
|
131
138
|
doc = @document.create!(:doc_id => BSON::ObjectId.new)
|
132
|
-
doc.changed?.should
|
139
|
+
doc.changed?.should be_falsey
|
133
140
|
doc.doc_id = doc.doc_id.to_s
|
134
|
-
doc.changed?.should
|
141
|
+
doc.changed?.should be_falsey
|
135
142
|
end
|
136
143
|
end
|
137
144
|
|
@@ -164,14 +171,14 @@ describe "Dirty" do
|
|
164
171
|
doc = @document.create(:phrase => 'Foo')
|
165
172
|
|
166
173
|
doc.phrase << 'bar'
|
167
|
-
doc.phrase_changed?.should
|
174
|
+
doc.phrase_changed?.should be_falsey
|
168
175
|
|
169
176
|
doc.phrase_will_change!
|
170
|
-
doc.phrase_changed?.should
|
177
|
+
doc.phrase_changed?.should be_truthy
|
171
178
|
doc.phrase_change.should == ['Foobar', 'Foobar']
|
172
179
|
|
173
180
|
doc.phrase << '!'
|
174
|
-
doc.phrase_changed?.should
|
181
|
+
doc.phrase_changed?.should be_truthy
|
175
182
|
doc.phrase_change.should == ['Foobar', 'Foobar!']
|
176
183
|
end
|
177
184
|
end
|
@@ -190,7 +197,7 @@ describe "Dirty" do
|
|
190
197
|
|
191
198
|
milestone = milestone_class.create(:name => 'Launch')
|
192
199
|
milestone.project = project_class.create(:name => 'Harmony')
|
193
|
-
milestone.changed?.should
|
200
|
+
milestone.changed?.should be_truthy
|
194
201
|
milestone.changed.should == %w(project_id)
|
195
202
|
end
|
196
203
|
end
|
@@ -204,11 +211,11 @@ describe "Dirty" do
|
|
204
211
|
validated_doc = validated_class.new
|
205
212
|
validated_doc.name = "I'm a changin"
|
206
213
|
validated_doc.save
|
207
|
-
validated_doc.changed?.should
|
214
|
+
validated_doc.changed?.should be_truthy
|
208
215
|
|
209
216
|
validated_doc.required = 1
|
210
217
|
validated_doc.save
|
211
|
-
validated_doc.changed?.should
|
218
|
+
validated_doc.changed?.should be_falsey
|
212
219
|
end
|
213
220
|
end
|
214
221
|
|
@@ -220,20 +227,26 @@ describe "Dirty" do
|
|
220
227
|
doc.a = "d"
|
221
228
|
doc.a_change.should == ["b","d"]
|
222
229
|
end
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
230
|
+
|
231
|
+
# TODO: ? Is this consistent with how ActiveRecord now works with changes?
|
232
|
+
# it "should reset changes when set back to the original value" do
|
233
|
+
# doc = @document.create(:a=>"b")
|
234
|
+
# doc.a = "c"
|
235
|
+
# doc.a = "b"
|
236
|
+
# doc.changed?.should be_falsey
|
237
|
+
# end
|
229
238
|
end
|
230
239
|
|
231
240
|
context "reset_attribute!" do
|
232
241
|
it "should reset the attribute back to the previous value" do
|
233
242
|
doc = @document.create(:a=>"b")
|
234
243
|
doc.a = "c"
|
235
|
-
doc.
|
236
|
-
|
244
|
+
if doc.respond_to?(:restore_a!)
|
245
|
+
doc.restore_a!
|
246
|
+
else
|
247
|
+
doc.reset_a!
|
248
|
+
end
|
249
|
+
doc.changed?.should be_falsey
|
237
250
|
doc.a.should == "b"
|
238
251
|
end
|
239
252
|
it "should reset the attribute back to the original value after several changes" do
|
@@ -241,8 +254,12 @@ describe "Dirty" do
|
|
241
254
|
doc.a = "c"
|
242
255
|
doc.a = "d"
|
243
256
|
doc.a = "e"
|
244
|
-
doc.
|
245
|
-
|
257
|
+
if doc.respond_to?(:restore_a!)
|
258
|
+
doc.restore_a!
|
259
|
+
else
|
260
|
+
doc.reset_a!
|
261
|
+
end
|
262
|
+
doc.changed?.should be_falsey
|
246
263
|
doc.a.should == "b"
|
247
264
|
end
|
248
265
|
end
|
@@ -275,21 +292,21 @@ describe "Dirty" do
|
|
275
292
|
|
276
293
|
it "should track changes" do
|
277
294
|
@duck.name = "hi"
|
278
|
-
@duck.changed?.should
|
295
|
+
@duck.changed?.should be_truthy
|
279
296
|
end
|
280
297
|
|
281
298
|
it "should clear changes when saved" do
|
282
299
|
@duck.name = "hi"
|
283
|
-
@duck.changed?.should
|
300
|
+
@duck.changed?.should be_truthy
|
284
301
|
@duck.save!
|
285
|
-
@duck.changed?.should_not
|
302
|
+
@duck.changed?.should_not be_truthy
|
286
303
|
end
|
287
304
|
|
288
305
|
it "should clear changes when the parent is saved" do
|
289
306
|
@duck.name = "hi"
|
290
|
-
@duck.changed?.should
|
307
|
+
@duck.changed?.should be_truthy
|
291
308
|
@doc.save!
|
292
|
-
@duck.changed?.should_not
|
309
|
+
@duck.changed?.should_not be_truthy
|
293
310
|
end
|
294
311
|
|
295
312
|
context "with nested embedded documents" do
|
@@ -302,14 +319,14 @@ describe "Dirty" do
|
|
302
319
|
|
303
320
|
it "should track changes" do
|
304
321
|
@dong.name = "hi"
|
305
|
-
@dong.changed?.should
|
322
|
+
@dong.changed?.should be_truthy
|
306
323
|
end
|
307
324
|
|
308
325
|
it "should clear changes when the root saves" do
|
309
326
|
@dong.name = "hi"
|
310
|
-
@dong.changed?.should
|
327
|
+
@dong.changed?.should be_truthy
|
311
328
|
@doc.save!
|
312
|
-
@dong.changed?.should
|
329
|
+
@dong.changed?.should be_falsey
|
313
330
|
end
|
314
331
|
end
|
315
332
|
end
|