mongo_mapper 0.14.0 → 0.15.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/LICENSE +1 -1
- data/{README.rdoc → README.md} +26 -21
- 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/connection.rb +16 -38
- data/lib/mongo_mapper/extensions/object_id.rb +5 -1
- data/lib/mongo_mapper/plugins/accessible.rb +1 -1
- data/lib/mongo_mapper/plugins/associations/base.rb +10 -2
- data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +1 -1
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +36 -6
- 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/proxy.rb +5 -1
- data/lib/mongo_mapper/plugins/associations/single_association.rb +5 -4
- 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/dynamic_finder.rb +1 -1
- data/lib/mongo_mapper/plugins/embedded_callbacks.rb +1 -0
- data/lib/mongo_mapper/plugins/embedded_document.rb +1 -1
- data/lib/mongo_mapper/plugins/identity_map.rb +3 -1
- data/lib/mongo_mapper/plugins/indexes.rb +13 -6
- data/lib/mongo_mapper/plugins/keys/key.rb +13 -8
- data/lib/mongo_mapper/plugins/keys.rb +9 -2
- data/lib/mongo_mapper/plugins/modifiers.rb +39 -14
- data/lib/mongo_mapper/plugins/persistence.rb +6 -2
- data/lib/mongo_mapper/plugins/querying/decorated_plucky_query.rb +6 -6
- data/lib/mongo_mapper/plugins/querying.rb +9 -3
- data/lib/mongo_mapper/plugins/safe.rb +10 -4
- data/lib/mongo_mapper/plugins/stats.rb +1 -3
- data/lib/mongo_mapper/plugins/strong_parameters.rb +26 -0
- data/lib/mongo_mapper/railtie.rb +1 -0
- data/lib/mongo_mapper/utils.rb +2 -2
- data/lib/mongo_mapper/version.rb +1 -1
- data/lib/mongo_mapper.rb +3 -0
- data/spec/examples.txt +1717 -0
- data/spec/functional/accessible_spec.rb +7 -1
- data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +2 -2
- data/spec/functional/associations/belongs_to_proxy_spec.rb +22 -5
- data/spec/functional/associations/in_array_proxy_spec.rb +149 -14
- 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 +22 -22
- data/spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb +2 -2
- data/spec/functional/associations/many_polymorphic_proxy_spec.rb +4 -4
- data/spec/functional/associations/one_as_proxy_spec.rb +8 -8
- data/spec/functional/associations/one_proxy_spec.rb +8 -8
- data/spec/functional/associations_spec.rb +3 -3
- data/spec/functional/binary_spec.rb +2 -2
- data/spec/functional/caching_spec.rb +15 -22
- data/spec/functional/callbacks_spec.rb +2 -2
- data/spec/functional/counter_cache_spec.rb +10 -10
- data/spec/functional/dirty_spec.rb +27 -10
- data/spec/functional/document_spec.rb +5 -8
- data/spec/functional/dumpable_spec.rb +1 -1
- data/spec/functional/embedded_document_spec.rb +5 -5
- data/spec/functional/identity_map_spec.rb +8 -8
- data/spec/functional/indexes_spec.rb +19 -18
- data/spec/functional/keys_spec.rb +51 -33
- data/spec/functional/logger_spec.rb +2 -2
- data/spec/functional/modifiers_spec.rb +81 -19
- data/spec/functional/partial_updates_spec.rb +8 -8
- data/spec/functional/protected_spec.rb +1 -1
- data/spec/functional/querying_spec.rb +70 -22
- data/spec/functional/safe_spec.rb +23 -27
- data/spec/functional/sci_spec.rb +7 -7
- data/spec/functional/scopes_spec.rb +1 -1
- data/spec/functional/static_keys_spec.rb +2 -2
- data/spec/functional/stats_spec.rb +28 -12
- data/spec/functional/strong_parameters_spec.rb +49 -0
- data/spec/functional/validations_spec.rb +8 -16
- data/spec/quality_spec.rb +1 -1
- data/spec/spec_helper.rb +32 -8
- data/spec/support/matchers.rb +1 -1
- data/spec/unit/associations/proxy_spec.rb +1 -5
- data/spec/unit/clone_spec.rb +1 -1
- data/spec/unit/document_spec.rb +3 -3
- data/spec/unit/embedded_document_spec.rb +4 -5
- data/spec/unit/extensions_spec.rb +3 -4
- data/spec/unit/identity_map_middleware_spec.rb +65 -96
- data/spec/unit/key_spec.rb +16 -17
- data/spec/unit/keys_spec.rb +17 -8
- data/spec/unit/mongo_mapper_spec.rb +41 -88
- data/spec/unit/rails_spec.rb +2 -2
- data/spec/unit/validations_spec.rb +18 -18
- metadata +41 -24
- data/lib/mongo_mapper/extensions/ordered_hash.rb +0 -23
@@ -2,8 +2,8 @@ require 'spec_helper.rb'
|
|
2
2
|
|
3
3
|
describe "ManyDocumentsProxy" do
|
4
4
|
before do
|
5
|
-
Project.collection.
|
6
|
-
Status.collection.
|
5
|
+
Project.collection.drop
|
6
|
+
Status.collection.drop
|
7
7
|
|
8
8
|
@pet_class = Doc do
|
9
9
|
key :name, String
|
@@ -171,9 +171,9 @@ describe "ManyDocumentsProxy" do
|
|
171
171
|
end
|
172
172
|
|
173
173
|
it "should call destroy the existing documents" do
|
174
|
-
|
175
|
-
|
176
|
-
|
174
|
+
@broker.properties[0].should_receive(:destroy).once
|
175
|
+
@broker.properties[1].should_receive(:destroy).once
|
176
|
+
@broker.properties[2].should_receive(:destroy).once
|
177
177
|
@broker.properties = [@property_class.new]
|
178
178
|
end
|
179
179
|
|
@@ -184,9 +184,9 @@ describe "ManyDocumentsProxy" do
|
|
184
184
|
end
|
185
185
|
|
186
186
|
it "should skip over documents that are the same" do
|
187
|
-
|
188
|
-
|
189
|
-
|
187
|
+
@broker.properties[0].should_receive(:destroy).never
|
188
|
+
@broker.properties[1].should_receive(:destroy).once
|
189
|
+
@broker.properties[2].should_receive(:destroy).never
|
190
190
|
@broker.properties = [@property3, @property1]
|
191
191
|
end
|
192
192
|
end
|
@@ -205,9 +205,9 @@ describe "ManyDocumentsProxy" do
|
|
205
205
|
end
|
206
206
|
|
207
207
|
it "should call delete the existing documents" do
|
208
|
-
|
209
|
-
|
210
|
-
|
208
|
+
@broker.properties[0].should_receive(:delete).once
|
209
|
+
@broker.properties[1].should_receive(:delete).once
|
210
|
+
@broker.properties[2].should_receive(:delete).once
|
211
211
|
@broker.properties = [@property_class.new]
|
212
212
|
end
|
213
213
|
|
@@ -218,9 +218,9 @@ describe "ManyDocumentsProxy" do
|
|
218
218
|
end
|
219
219
|
|
220
220
|
it "should skip over documents that are the same" do
|
221
|
-
|
222
|
-
|
223
|
-
|
221
|
+
@broker.properties[0].should_receive(:delete).never
|
222
|
+
@broker.properties[1].should_receive(:delete).once
|
223
|
+
@broker.properties[2].should_receive(:delete).never
|
224
224
|
@broker.properties = [@property3, @property1]
|
225
225
|
end
|
226
226
|
end
|
@@ -430,9 +430,9 @@ describe "ManyDocumentsProxy" do
|
|
430
430
|
|
431
431
|
it "should raise exception if not valid" do
|
432
432
|
project = Project.create
|
433
|
-
|
433
|
+
lambda {
|
434
434
|
project.statuses.create!(:name => nil)
|
435
|
-
}.
|
435
|
+
}.should raise_error(MongoMapper::DocumentNotValid)
|
436
436
|
end
|
437
437
|
|
438
438
|
it "should reset cache" do
|
@@ -754,9 +754,9 @@ describe "ManyDocumentsProxy" do
|
|
754
754
|
end
|
755
755
|
|
756
756
|
it "should not work for ids not in association" do
|
757
|
-
|
757
|
+
lambda {
|
758
758
|
@project1.statuses.find!(@brand_new.id, @complete.id, @archived.id)
|
759
|
-
}.
|
759
|
+
}.should raise_error(MongoMapper::DocumentNotFound)
|
760
760
|
end
|
761
761
|
end
|
762
762
|
|
@@ -810,13 +810,13 @@ describe "ManyDocumentsProxy" do
|
|
810
810
|
class ::Property
|
811
811
|
include MongoMapper::Document
|
812
812
|
end
|
813
|
-
Property.collection.
|
813
|
+
Property.collection.drop
|
814
814
|
|
815
815
|
class ::Thing
|
816
816
|
include MongoMapper::Document
|
817
817
|
key :name, String
|
818
818
|
end
|
819
|
-
Thing.collection.
|
819
|
+
Thing.collection.drop
|
820
820
|
end
|
821
821
|
|
822
822
|
after do
|
@@ -997,7 +997,7 @@ describe "ManyDocumentsProxy" do
|
|
997
997
|
|
998
998
|
lambda do
|
999
999
|
@job_title_2.trainings.find_by_slug!('bar')
|
1000
|
-
end.should raise_error
|
1000
|
+
end.should raise_error(RuntimeError)
|
1001
1001
|
end
|
1002
1002
|
|
1003
1003
|
it "should scope with an extra where clause on the proxy (regression #2)" do
|
@@ -1018,7 +1018,7 @@ describe "ManyDocumentsProxy" do
|
|
1018
1018
|
|
1019
1019
|
lambda do
|
1020
1020
|
@job_title_2.trainings.find_by_slug!('bar')
|
1021
|
-
end.should raise_error
|
1021
|
+
end.should raise_error(RuntimeError)
|
1022
1022
|
end
|
1023
1023
|
end
|
1024
1024
|
end
|
@@ -2,8 +2,8 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe "ManyPolymorphicProxy" do
|
4
4
|
before do
|
5
|
-
Room.collection.
|
6
|
-
Message.collection.
|
5
|
+
Room.collection.drop
|
6
|
+
Message.collection.drop
|
7
7
|
end
|
8
8
|
|
9
9
|
it "should default reader to empty array" do
|
@@ -247,9 +247,9 @@ describe "ManyPolymorphicProxy" do
|
|
247
247
|
end
|
248
248
|
|
249
249
|
it "should not work for ids not in association" do
|
250
|
-
|
250
|
+
lambda {
|
251
251
|
@lounge.messages.find!(@lm1._id, @lm2._id, @hm2._id)
|
252
|
-
}.
|
252
|
+
}.should raise_error(MongoMapper::DocumentNotFound)
|
253
253
|
end
|
254
254
|
end
|
255
255
|
|
@@ -132,7 +132,7 @@ describe "OneAsProxy" do
|
|
132
132
|
end
|
133
133
|
|
134
134
|
it "should call delete on the existing document" do
|
135
|
-
|
135
|
+
@author_class.any_instance.should_receive(:delete).once
|
136
136
|
@post.author = @author_class.new
|
137
137
|
end
|
138
138
|
|
@@ -142,7 +142,7 @@ describe "OneAsProxy" do
|
|
142
142
|
end
|
143
143
|
|
144
144
|
it "should do nothing if it's the same document" do
|
145
|
-
|
145
|
+
@author_class.any_instance.should_receive(:delete).never
|
146
146
|
@post.author = @author
|
147
147
|
end
|
148
148
|
end
|
@@ -157,7 +157,7 @@ describe "OneAsProxy" do
|
|
157
157
|
end
|
158
158
|
|
159
159
|
it "should call destroy the existing document" do
|
160
|
-
|
160
|
+
@author_class.any_instance.should_receive(:destroy).once
|
161
161
|
@post.author = @author_class.new
|
162
162
|
end
|
163
163
|
|
@@ -167,7 +167,7 @@ describe "OneAsProxy" do
|
|
167
167
|
end
|
168
168
|
|
169
169
|
it "should do nothing if it's the same document" do
|
170
|
-
|
170
|
+
@author_class.any_instance.should_receive(:destroy).never
|
171
171
|
@post.author = @author
|
172
172
|
end
|
173
173
|
end
|
@@ -302,7 +302,7 @@ describe "OneAsProxy" do
|
|
302
302
|
end
|
303
303
|
|
304
304
|
it "should should call destroy on the associated documents" do
|
305
|
-
|
305
|
+
@author_class.any_instance.should_receive(:destroy).once
|
306
306
|
@post.destroy
|
307
307
|
end
|
308
308
|
|
@@ -324,7 +324,7 @@ describe "OneAsProxy" do
|
|
324
324
|
end
|
325
325
|
|
326
326
|
it "should should call delete the associated documents" do
|
327
|
-
|
327
|
+
@author_class.any_instance.should_receive(:delete).once
|
328
328
|
@post.destroy
|
329
329
|
end
|
330
330
|
|
@@ -445,7 +445,7 @@ describe "OneAsProxy" do
|
|
445
445
|
end
|
446
446
|
|
447
447
|
it "should raise exception if invalid" do
|
448
|
-
|
448
|
+
lambda { @post.create_author! }.should raise_error(MongoMapper::DocumentNotValid)
|
449
449
|
end
|
450
450
|
|
451
451
|
it "should work if valid" do
|
@@ -486,4 +486,4 @@ describe "OneAsProxy" do
|
|
486
486
|
article.articleable_id.should == @paper.id
|
487
487
|
end
|
488
488
|
end
|
489
|
-
end
|
489
|
+
end
|
@@ -97,7 +97,7 @@ describe "OneProxy" do
|
|
97
97
|
end
|
98
98
|
|
99
99
|
it "should call delete on the existing document" do
|
100
|
-
|
100
|
+
@author_class.any_instance.should_receive(:delete).once
|
101
101
|
@post.author = @author_class.new
|
102
102
|
end
|
103
103
|
|
@@ -107,7 +107,7 @@ describe "OneProxy" do
|
|
107
107
|
end
|
108
108
|
|
109
109
|
it "should do nothing if it's the same document" do
|
110
|
-
|
110
|
+
@author_class.any_instance.should_receive(:delete).never
|
111
111
|
@post.author = @author
|
112
112
|
end
|
113
113
|
end
|
@@ -122,7 +122,7 @@ describe "OneProxy" do
|
|
122
122
|
end
|
123
123
|
|
124
124
|
it "should call destroy the existing document" do
|
125
|
-
|
125
|
+
@author_class.any_instance.should_receive(:destroy).once
|
126
126
|
@post.author = @author_class.new
|
127
127
|
end
|
128
128
|
|
@@ -132,7 +132,7 @@ describe "OneProxy" do
|
|
132
132
|
end
|
133
133
|
|
134
134
|
it "should do nothing if it's the same document" do
|
135
|
-
|
135
|
+
@author_class.any_instance.should_receive(:destroy).never
|
136
136
|
@post.author = @author
|
137
137
|
end
|
138
138
|
end
|
@@ -246,7 +246,7 @@ describe "OneProxy" do
|
|
246
246
|
end
|
247
247
|
|
248
248
|
it "should should call destroy on the associated documents" do
|
249
|
-
|
249
|
+
@author_class.any_instance.should_receive(:destroy).once
|
250
250
|
@post.destroy
|
251
251
|
end
|
252
252
|
|
@@ -268,7 +268,7 @@ describe "OneProxy" do
|
|
268
268
|
end
|
269
269
|
|
270
270
|
it "should should call delete the associated documents" do
|
271
|
-
|
271
|
+
@author_class.any_instance.should_receive(:delete).once
|
272
272
|
@post.destroy
|
273
273
|
end
|
274
274
|
|
@@ -366,9 +366,9 @@ describe "OneProxy" do
|
|
366
366
|
end
|
367
367
|
|
368
368
|
it "should raise exception if invalid" do
|
369
|
-
|
369
|
+
lambda {
|
370
370
|
post.create_author!
|
371
|
-
}.
|
371
|
+
}.should raise_error(MongoMapper::DocumentNotValid)
|
372
372
|
end
|
373
373
|
|
374
374
|
it "should work if valid" do
|
@@ -9,7 +9,7 @@ module AssociationsSpec
|
|
9
9
|
|
10
10
|
many :posts, :class_name => 'AssociationsSpec::AwesomePost', :foreign_key => :creator_id
|
11
11
|
end
|
12
|
-
AwesomeUser.collection.
|
12
|
+
AwesomeUser.collection.drop
|
13
13
|
|
14
14
|
class AwesomeTag
|
15
15
|
include MongoMapper::EmbeddedDocument
|
@@ -29,8 +29,8 @@ module AssociationsSpec
|
|
29
29
|
many :tags, :class_name => 'AssociationsSpec::AwesomeTag', :foreign_key => :post_id
|
30
30
|
end
|
31
31
|
|
32
|
-
AwesomeUser.collection.
|
33
|
-
AwesomePost.collection.
|
32
|
+
AwesomeUser.collection.drop
|
33
|
+
AwesomePost.collection.drop
|
34
34
|
|
35
35
|
user = AwesomeUser.create
|
36
36
|
tag1 = AwesomeTag.new(:name => 'awesome')
|
@@ -10,7 +10,7 @@ describe "Binary" do
|
|
10
10
|
doc.save
|
11
11
|
|
12
12
|
doc = doc.reload
|
13
|
-
doc.contents.
|
13
|
+
doc.contents.data.should == BSON::Binary.new('010101').data
|
14
14
|
end
|
15
15
|
|
16
16
|
context "Saving a document with a blank binary value" do
|
@@ -21,7 +21,7 @@ describe "Binary" do
|
|
21
21
|
end
|
22
22
|
|
23
23
|
it "not fail" do
|
24
|
-
|
24
|
+
lambda { @document.new(:file => nil).save }.should_not raise_error
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
@@ -6,16 +6,16 @@ describe "Caching" do
|
|
6
6
|
extend MongoMapper::Plugins
|
7
7
|
plugin MongoMapper::Plugins::Caching
|
8
8
|
end
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
@klass.stub(:name).and_return('Post')
|
10
|
+
@klass.any_instance.stub(:persisted?).and_return(true)
|
11
|
+
@klass.any_instance.stub(:[]).and_return(nil)
|
12
|
+
@klass.any_instance.stub(:[]=).and_return(nil)
|
13
13
|
end
|
14
14
|
|
15
15
|
context "new" do
|
16
16
|
before do
|
17
17
|
@doc = @klass.new
|
18
|
-
|
18
|
+
@doc.stub(:persisted?).and_return(false)
|
19
19
|
end
|
20
20
|
|
21
21
|
it "should be class/new" do
|
@@ -23,11 +23,9 @@ describe "Caching" do
|
|
23
23
|
end
|
24
24
|
|
25
25
|
it "should work with suffix" do
|
26
|
-
@doc.cache_key(:foo).
|
27
|
-
should == 'Post/new/foo'
|
26
|
+
@doc.cache_key(:foo).should == 'Post/new/foo'
|
28
27
|
|
29
|
-
@doc.cache_key(:foo, :bar).
|
30
|
-
should == 'Post/new/foo/bar'
|
28
|
+
@doc.cache_key(:foo, :bar).should == 'Post/new/foo/bar'
|
31
29
|
end
|
32
30
|
end
|
33
31
|
|
@@ -35,14 +33,14 @@ describe "Caching" do
|
|
35
33
|
before do
|
36
34
|
@object_id = BSON::ObjectId.new
|
37
35
|
@doc = @klass.new
|
38
|
-
|
39
|
-
|
36
|
+
@doc.stub(:persisted?).and_return(true)
|
37
|
+
@doc.stub(:id).and_return(@object_id)
|
40
38
|
end
|
41
39
|
|
42
40
|
context "with updated_at" do
|
43
41
|
before do
|
44
42
|
time = Time.utc(2010, 6, 20, 8, 10, 7)
|
45
|
-
|
43
|
+
@doc.stub(:[]).with(:updated_at).and_return(time)
|
46
44
|
end
|
47
45
|
|
48
46
|
it "should be class/id-timestamp" do
|
@@ -50,11 +48,9 @@ describe "Caching" do
|
|
50
48
|
end
|
51
49
|
|
52
50
|
it "should work with suffix" do
|
53
|
-
@doc.cache_key(:foo).
|
54
|
-
should == "Post/#{@object_id}-20100620081007/foo"
|
51
|
+
@doc.cache_key(:foo).should == "Post/#{@object_id}-20100620081007/foo"
|
55
52
|
|
56
|
-
@doc.cache_key(:foo, :bar).
|
57
|
-
should == "Post/#{@object_id}-20100620081007/foo/bar"
|
53
|
+
@doc.cache_key(:foo, :bar).should == "Post/#{@object_id}-20100620081007/foo/bar"
|
58
54
|
end
|
59
55
|
end
|
60
56
|
|
@@ -64,12 +60,9 @@ describe "Caching" do
|
|
64
60
|
end
|
65
61
|
|
66
62
|
it "should work with suffix" do
|
67
|
-
@doc.cache_key(:foo).
|
68
|
-
|
69
|
-
|
70
|
-
@doc.cache_key(:foo, :bar, :baz).
|
71
|
-
should == "Post/#{@object_id}/foo/bar/baz"
|
63
|
+
@doc.cache_key(:foo).should == "Post/#{@object_id}/foo"
|
64
|
+
@doc.cache_key(:foo, :bar, :baz).should == "Post/#{@object_id}/foo/bar/baz"
|
72
65
|
end
|
73
66
|
end
|
74
67
|
end
|
75
|
-
end
|
68
|
+
end
|
@@ -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
|
|
@@ -171,42 +171,42 @@ describe MongoMapper::Plugins::CounterCache do
|
|
171
171
|
end
|
172
172
|
|
173
173
|
it "should update the counter cache on save" do
|
174
|
-
|
174
|
+
lambda {
|
175
175
|
@comment.save!
|
176
176
|
@article.reload
|
177
|
-
}.
|
177
|
+
}.should change(@article, :commentable_count).by(1)
|
178
178
|
end
|
179
179
|
|
180
180
|
it "should increment with a second object" do
|
181
181
|
@comment.save!
|
182
182
|
|
183
|
-
|
183
|
+
lambda {
|
184
184
|
second_comment = CounterCacheFixtureModels::Comment.new
|
185
185
|
second_comment.commentable = @article
|
186
186
|
second_comment.save!
|
187
187
|
@article.reload
|
188
|
-
}.
|
188
|
+
}.should change(@article, :commentable_count).by(1)
|
189
189
|
end
|
190
190
|
|
191
191
|
it "should decrement the counter cache on destroy" do
|
192
192
|
@comment.save!
|
193
193
|
|
194
|
-
|
194
|
+
lambda {
|
195
195
|
@comment.destroy
|
196
196
|
@article.reload
|
197
|
-
}.
|
197
|
+
}.should change(@article, :commentable_count).by(-1)
|
198
198
|
end
|
199
199
|
|
200
200
|
it "should increment with a different type of object" do
|
201
201
|
@comment.save!
|
202
202
|
|
203
|
-
|
203
|
+
lambda {
|
204
204
|
second_comment = CounterCacheFixtureModels::Comment.new
|
205
205
|
second_comment.commentable = @article
|
206
206
|
second_comment.save!
|
207
207
|
|
208
208
|
@article.reload
|
209
|
-
}.
|
209
|
+
}.should change(@article, :commentable_count).by(1)
|
210
210
|
end
|
211
211
|
|
212
212
|
describe "without a counter cache field" do
|
@@ -226,9 +226,9 @@ describe MongoMapper::Plugins::CounterCache do
|
|
226
226
|
it "should raise at save (runtime) if there is no counter cache field" do
|
227
227
|
@comment.commentable = @obj
|
228
228
|
|
229
|
-
|
229
|
+
lambda {
|
230
230
|
@comment.save!
|
231
|
-
}.
|
231
|
+
}.should raise_error(MongoMapper::Plugins::CounterCache::InvalidCounterCacheError)
|
232
232
|
end
|
233
233
|
end
|
234
234
|
end
|
@@ -9,6 +9,13 @@ 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
21
|
doc.phrase_changed?.should be_falsey
|
@@ -48,8 +55,8 @@ describe "Dirty" do
|
|
48
55
|
|
49
56
|
it "should not happen when loading from database" do
|
50
57
|
doc = @document.create(:phrase => 'Foo')
|
51
|
-
|
52
|
-
|
58
|
+
@document.any_instance.should_receive(:attribute_will_change!).never
|
59
|
+
@document.any_instance.should_receive(:attribute_changed?).never
|
53
60
|
doc = @document.find(doc.id)
|
54
61
|
doc.changed?.should be_falsey
|
55
62
|
end
|
@@ -220,19 +227,25 @@ 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.
|
244
|
+
if doc.respond_to?(:restore_a!)
|
245
|
+
doc.restore_a!
|
246
|
+
else
|
247
|
+
doc.reset_a!
|
248
|
+
end
|
236
249
|
doc.changed?.should be_falsey
|
237
250
|
doc.a.should == "b"
|
238
251
|
end
|
@@ -241,7 +254,11 @@ describe "Dirty" do
|
|
241
254
|
doc.a = "c"
|
242
255
|
doc.a = "d"
|
243
256
|
doc.a = "e"
|
244
|
-
doc.
|
257
|
+
if doc.respond_to?(:restore_a!)
|
258
|
+
doc.restore_a!
|
259
|
+
else
|
260
|
+
doc.reset_a!
|
261
|
+
end
|
245
262
|
doc.changed?.should be_falsey
|
246
263
|
doc.a.should == "b"
|
247
264
|
end
|
@@ -240,8 +240,8 @@ describe "Document" do
|
|
240
240
|
end
|
241
241
|
|
242
242
|
it "should reset many associations" do
|
243
|
-
|
244
|
-
|
243
|
+
@instance.foos.should_receive(:reset).at_least(1).times
|
244
|
+
@instance.bars.should_receive(:reset).at_least(1).times
|
245
245
|
@instance.reload
|
246
246
|
end
|
247
247
|
|
@@ -257,9 +257,6 @@ describe "Document" do
|
|
257
257
|
@instance.bar.should_not be_nil
|
258
258
|
end
|
259
259
|
|
260
|
-
it "should reset nil one association" do
|
261
|
-
end
|
262
|
-
|
263
260
|
it "should reinstantiate embedded associations" do
|
264
261
|
@instance.reload
|
265
262
|
@instance.bars.first.name.should == '1'
|
@@ -271,7 +268,7 @@ describe "Document" do
|
|
271
268
|
|
272
269
|
it "should raise DocumentNotFound if not found" do
|
273
270
|
@instance.destroy
|
274
|
-
|
271
|
+
lambda { @instance.reload }.should raise_error(MongoMapper::DocumentNotFound)
|
275
272
|
end
|
276
273
|
|
277
274
|
it "should clear keys that were removed from the database" do
|
@@ -283,7 +280,7 @@ describe "Document" do
|
|
283
280
|
context "database has keys not defined in model" do
|
284
281
|
before do
|
285
282
|
@id = BSON::ObjectId.new
|
286
|
-
@document.collection.
|
283
|
+
@document.collection.insert_one({
|
287
284
|
:_id => @id,
|
288
285
|
:first_name => 'John',
|
289
286
|
:last_name => 'Nunemaker',
|
@@ -304,7 +301,7 @@ describe "Document" do
|
|
304
301
|
end
|
305
302
|
|
306
303
|
it "should not walk ObjectSpace when creating a model" do
|
307
|
-
|
304
|
+
ObjectSpace.should_receive(:each_object).never
|
308
305
|
Doc()
|
309
306
|
end
|
310
307
|
end
|
@@ -7,7 +7,7 @@ describe "Documents with the Dumpable plugin" do
|
|
7
7
|
let(:store) { ActiveSupport::Cache::MemoryStore.new(:size => 1.megabyte) }
|
8
8
|
|
9
9
|
it "should be able to be marshalled" do
|
10
|
-
|
10
|
+
lambda { Marshal.dump(answer) }.should_not raise_error
|
11
11
|
end
|
12
12
|
|
13
13
|
it "should be able to be unmarshalled" do
|
@@ -227,7 +227,7 @@ describe "EmbeddedDocument" do
|
|
227
227
|
person.pets << pet
|
228
228
|
pet.should be_new
|
229
229
|
|
230
|
-
|
230
|
+
person.should_receive(:save!)
|
231
231
|
pet.save!
|
232
232
|
end
|
233
233
|
|
@@ -272,8 +272,8 @@ describe "EmbeddedDocument" do
|
|
272
272
|
pet = person.pets.first
|
273
273
|
|
274
274
|
attributes = {:name => 'koda'}
|
275
|
-
|
276
|
-
|
275
|
+
pet.should_receive(:attributes=).with(attributes)
|
276
|
+
pet.should_receive(:save!)
|
277
277
|
pet.update_attributes!(attributes)
|
278
278
|
end
|
279
279
|
|
@@ -288,8 +288,8 @@ describe "EmbeddedDocument" do
|
|
288
288
|
end
|
289
289
|
|
290
290
|
it "should not fail if the source document contains nils in the embedded document list" do
|
291
|
-
@klass.collection.
|
292
|
-
|
291
|
+
@klass.collection.insert_one(:pets => [nil, {:name => "Sasha"}])
|
292
|
+
lambda { @klass.all.first.pets }.should_not raise_error
|
293
293
|
@klass.all.first.pets.tap do |pets|
|
294
294
|
pets.length.should == 1
|
295
295
|
pets[0].name.should == "Sasha"
|