mongo_mapper 0.13.0.beta2 → 0.15.0
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 +64 -0
- data/examples/keys.rb +3 -3
- data/examples/modifiers/set.rb +2 -2
- data/examples/querying.rb +3 -3
- data/examples/safe.rb +2 -2
- data/examples/scopes.rb +1 -1
- data/lib/mongo_mapper.rb +5 -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 +14 -4
- data/lib/mongo_mapper/plugins/associations.rb +7 -6
- data/lib/mongo_mapper/plugins/associations/base.rb +18 -13
- 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 +24 -23
- 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 +17 -14
- 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 +31 -28
- data/lib/mongo_mapper/plugins/callbacks.rb +14 -1
- 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 +1 -1
- data/lib/mongo_mapper/plugins/indexes.rb +37 -2
- data/lib/mongo_mapper/plugins/keys.rb +202 -142
- data/lib/mongo_mapper/plugins/keys/key.rb +22 -13
- data/lib/mongo_mapper/plugins/keys/static.rb +45 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +59 -28
- 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 +32 -9
- 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 +4 -1
- data/lib/mongo_mapper/plugins/scopes.rb +78 -7
- data/lib/mongo_mapper/plugins/stats.rb +17 -0
- data/lib/mongo_mapper/plugins/timestamps.rb +1 -0
- data/lib/mongo_mapper/plugins/touch.rb +1 -1
- data/lib/mongo_mapper/plugins/validations.rb +7 -2
- data/lib/mongo_mapper/railtie.rb +20 -0
- data/lib/mongo_mapper/railtie/database.rake +1 -1
- 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 +1643 -0
- data/spec/functional/accessible_spec.rb +13 -13
- data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +13 -13
- data/spec/functional/associations/belongs_to_proxy_spec.rb +18 -19
- data/spec/functional/associations/in_array_proxy_spec.rb +10 -10
- 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 -2
- data/spec/functional/dumpable_spec.rb +1 -1
- data/spec/functional/embedded_document_spec.rb +18 -18
- data/spec/functional/identity_map_spec.rb +27 -14
- data/spec/functional/indexes_spec.rb +44 -19
- data/spec/functional/keys_spec.rb +117 -15
- data/spec/functional/logger_spec.rb +3 -3
- data/spec/functional/modifiers_spec.rb +67 -19
- data/spec/functional/partial_updates_spec.rb +577 -0
- data/spec/functional/protected_spec.rb +14 -14
- data/spec/functional/querying_spec.rb +55 -28
- data/spec/functional/safe_spec.rb +23 -27
- data/spec/functional/sci_spec.rb +49 -14
- 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/touch_spec.rb +6 -6
- 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 -16
- 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/inspect_spec.rb +4 -4
- data/spec/unit/key_spec.rb +28 -26
- data/spec/unit/keys_spec.rb +10 -10
- data/spec/unit/model_generator_spec.rb +2 -4
- 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 +28 -15
- metadata +188 -161
- data/README.rdoc +0 -55
- data/lib/mongo_mapper/extensions/ordered_hash.rb +0 -23
@@ -39,12 +39,12 @@ describe "Accessible" do
|
|
39
39
|
|
40
40
|
it "should assign inaccessible attribute through accessor" do
|
41
41
|
@doc.admin = true
|
42
|
-
@doc.admin.should
|
42
|
+
@doc.admin.should be_truthy
|
43
43
|
end
|
44
44
|
|
45
45
|
it "should ignore inaccessible attribute on #initialize" do
|
46
46
|
doc = @doc_class.new(:name => 'John', :admin => true)
|
47
|
-
doc.admin.should
|
47
|
+
doc.admin.should be_falsey
|
48
48
|
doc.name.should == 'John'
|
49
49
|
end
|
50
50
|
|
@@ -54,7 +54,7 @@ describe "Accessible" do
|
|
54
54
|
doc.save!
|
55
55
|
|
56
56
|
doc = @doc_class.first(:name => 'John')
|
57
|
-
doc.admin.should
|
57
|
+
doc.admin.should be_truthy
|
58
58
|
doc.name.should == 'John'
|
59
59
|
end
|
60
60
|
|
@@ -64,37 +64,37 @@ describe "Accessible" do
|
|
64
64
|
doc.save!
|
65
65
|
|
66
66
|
doc.reload
|
67
|
-
doc.admin.should
|
67
|
+
doc.admin.should be_truthy
|
68
68
|
doc.name.should == 'John'
|
69
69
|
end
|
70
70
|
|
71
71
|
it "should not ignore inaccessible attribute on #update_attribute" do
|
72
72
|
@doc.update_attribute('admin', true)
|
73
|
-
@doc.admin.should
|
73
|
+
@doc.admin.should be_truthy
|
74
74
|
end
|
75
75
|
|
76
76
|
it "should ignore inaccessible attribute on #update_attributes" do
|
77
77
|
@doc.update_attributes(:name => 'Ren Hoek', :admin => true)
|
78
78
|
@doc.name.should == 'Ren Hoek'
|
79
|
-
@doc.admin.should
|
79
|
+
@doc.admin.should be_falsey
|
80
80
|
end
|
81
81
|
|
82
82
|
it "should ignore inaccessible attribute on #update_attributes!" do
|
83
83
|
@doc.update_attributes!(:name => 'Stimpson J. Cat', :admin => true)
|
84
84
|
@doc.name.should == 'Stimpson J. Cat'
|
85
|
-
@doc.admin.should
|
85
|
+
@doc.admin.should be_falsey
|
86
86
|
end
|
87
87
|
|
88
88
|
it "should ignore inaccessible attribute on #attributes=" do
|
89
89
|
@doc.attributes = {:name => 'Ren Hoek', :admin => true}
|
90
90
|
@doc.name.should == 'Ren Hoek'
|
91
|
-
@doc.admin.should
|
91
|
+
@doc.admin.should be_falsey
|
92
92
|
end
|
93
93
|
|
94
94
|
it "should be indifferent to whether the accessible keys are strings or symbols" do
|
95
95
|
@doc.update_attributes!("name" => 'Stimpson J. Cat', "admin" => true)
|
96
96
|
@doc.name.should == 'Stimpson J. Cat'
|
97
|
-
@doc.admin.should
|
97
|
+
@doc.admin.should be_falsey
|
98
98
|
end
|
99
99
|
|
100
100
|
it "should accept nil as constructor's argument without raising exception" do
|
@@ -119,7 +119,7 @@ describe "Accessible" do
|
|
119
119
|
key :name, String
|
120
120
|
key :site_id, ObjectId
|
121
121
|
end
|
122
|
-
GrandParent.collection.
|
122
|
+
GrandParent.collection.drop
|
123
123
|
|
124
124
|
class ::Child < ::GrandParent
|
125
125
|
attr_accessible :position
|
@@ -180,19 +180,19 @@ describe "Accessible" do
|
|
180
180
|
|
181
181
|
it "should assign inaccessible attribute through accessor" do
|
182
182
|
@edoc.admin = true
|
183
|
-
@edoc.admin.should
|
183
|
+
@edoc.admin.should be_truthy
|
184
184
|
end
|
185
185
|
|
186
186
|
it "should ignore inaccessible attribute on #update_attributes" do
|
187
187
|
@edoc.update_attributes(:name => 'Ren Hoek', :admin => true)
|
188
188
|
@edoc.name.should == 'Ren Hoek'
|
189
|
-
@edoc.admin.should
|
189
|
+
@edoc.admin.should be_falsey
|
190
190
|
end
|
191
191
|
|
192
192
|
it "should ignore inaccessible attribute on #update_attributes!" do
|
193
193
|
@edoc.update_attributes!(:name => 'Stimpson J. Cat', :admin => true)
|
194
194
|
@edoc.name.should == 'Stimpson J. Cat'
|
195
|
-
@edoc.admin.should
|
195
|
+
@edoc.admin.should be_falsey
|
196
196
|
end
|
197
197
|
end
|
198
198
|
end
|
@@ -3,32 +3,32 @@ require 'spec_helper'
|
|
3
3
|
|
4
4
|
describe "BelongsToPolymorphicProxy" do
|
5
5
|
before do
|
6
|
-
Status.collection.
|
7
|
-
Project.collection.
|
6
|
+
Status.collection.drop
|
7
|
+
Project.collection.drop
|
8
8
|
end
|
9
9
|
|
10
10
|
it "should default to nil" do
|
11
11
|
status = Status.new
|
12
|
-
status.target.nil?.should
|
12
|
+
status.target.nil?.should be_truthy
|
13
13
|
status.target.inspect.should == "nil"
|
14
14
|
end
|
15
15
|
|
16
16
|
it "should have boolean presence method" do
|
17
17
|
status = Status.new
|
18
|
-
status.target?.should
|
18
|
+
status.target?.should be_falsey
|
19
19
|
|
20
20
|
status.target = Project.new(:name => 'mongomapper')
|
21
|
-
status.target?.should
|
21
|
+
status.target?.should be_truthy
|
22
22
|
end
|
23
23
|
|
24
24
|
it "should be able to replace the association" do
|
25
25
|
status = Status.new(:name => 'Foo!')
|
26
26
|
project = Project.new(:name => "mongomapper")
|
27
27
|
status.target = project
|
28
|
-
status.save.should
|
28
|
+
status.save.should be_truthy
|
29
29
|
|
30
30
|
status = status.reload
|
31
|
-
status.target.nil?.should
|
31
|
+
status.target.nil?.should be_falsey
|
32
32
|
status.target_id.should == project._id
|
33
33
|
status.target_type.should == "Project"
|
34
34
|
status.target.name.should == "mongomapper"
|
@@ -38,13 +38,13 @@ describe "BelongsToPolymorphicProxy" do
|
|
38
38
|
status = Status.new(:name => 'Foo!')
|
39
39
|
project = Project.new(:name => "mongomapper")
|
40
40
|
status.target = project
|
41
|
-
status.save.should
|
41
|
+
status.save.should be_truthy
|
42
42
|
|
43
43
|
status = status.reload
|
44
44
|
status.target = nil
|
45
|
-
status.target_type.nil?.should
|
46
|
-
status.target_id.nil?.should
|
47
|
-
status.target.nil?.should
|
45
|
+
status.target_type.nil?.should be_truthy
|
46
|
+
status.target_id.nil?.should be_truthy
|
47
|
+
status.target.nil?.should be_truthy
|
48
48
|
end
|
49
49
|
|
50
50
|
context "association id set but document not found" do
|
@@ -52,13 +52,13 @@ describe "BelongsToPolymorphicProxy" do
|
|
52
52
|
@status = Status.new(:name => 'Foo!')
|
53
53
|
project = Project.new(:name => "mongomapper")
|
54
54
|
@status.target = project
|
55
|
-
@status.save.should
|
55
|
+
@status.save.should be_truthy
|
56
56
|
project.destroy
|
57
57
|
@status.reload
|
58
58
|
end
|
59
59
|
|
60
60
|
it "should return nil instead of raising error" do
|
61
|
-
@status.target.nil?.should
|
61
|
+
@status.target.nil?.should be_truthy
|
62
62
|
end
|
63
63
|
end
|
64
64
|
end
|
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
3
|
describe "BelongsToProxy" do
|
5
4
|
before do
|
6
5
|
@post_class = Doc()
|
@@ -11,7 +10,7 @@ describe "BelongsToProxy" do
|
|
11
10
|
end
|
12
11
|
|
13
12
|
it "should default to nil" do
|
14
|
-
@comment_class.new.post.nil?.should
|
13
|
+
@comment_class.new.post.nil?.should be_truthy
|
15
14
|
end
|
16
15
|
|
17
16
|
it "should return nil instead of a proxy" do
|
@@ -20,10 +19,10 @@ describe "BelongsToProxy" do
|
|
20
19
|
|
21
20
|
it "should have boolean presence method" do
|
22
21
|
comment = @comment_class.new(:name => 'Foo!')
|
23
|
-
comment.post?.should
|
22
|
+
comment.post?.should be_falsey
|
24
23
|
|
25
24
|
comment.post = @post_class.new(:name => 'mongomapper')
|
26
|
-
comment.post?.should
|
25
|
+
comment.post?.should be_truthy
|
27
26
|
end
|
28
27
|
|
29
28
|
it "should allow overriding association methods" do
|
@@ -34,19 +33,19 @@ describe "BelongsToProxy" do
|
|
34
33
|
end
|
35
34
|
|
36
35
|
instance = @comment_class.new
|
37
|
-
instance.post?.should
|
36
|
+
instance.post?.should be_falsey
|
38
37
|
instance.post = @post_class.new
|
39
|
-
instance.post?.should
|
38
|
+
instance.post?.should be_truthy
|
40
39
|
end
|
41
40
|
|
42
41
|
it "should be able to replace the association" do
|
43
42
|
post = @post_class.new(:name => 'mongomapper')
|
44
43
|
comment = @comment_class.new(:name => 'Foo!', :post => post)
|
45
|
-
comment.save.should
|
44
|
+
comment.save.should be_truthy
|
46
45
|
|
47
46
|
comment = comment.reload
|
48
47
|
comment.post.should == post
|
49
|
-
comment.post.nil?.should
|
48
|
+
comment.post.nil?.should be_falsey
|
50
49
|
end
|
51
50
|
|
52
51
|
it "should not reload the association when replacing" do
|
@@ -71,12 +70,12 @@ describe "BelongsToProxy" do
|
|
71
70
|
post2 = @post_class.create(:name => 'post2')
|
72
71
|
|
73
72
|
comment = @comment_class.new(:name => 'Foo!', :post => post1)
|
74
|
-
comment.save.should
|
73
|
+
comment.save.should be_truthy
|
75
74
|
|
76
75
|
|
77
76
|
comment = comment.reload
|
78
77
|
comment.post.should == post1
|
79
|
-
comment.post.nil?.should
|
78
|
+
comment.post.nil?.should be_falsey
|
80
79
|
|
81
80
|
original_post = comment.post
|
82
81
|
original_post.name.should == 'post1'
|
@@ -89,23 +88,23 @@ describe "BelongsToProxy" do
|
|
89
88
|
it "should unset the association" do
|
90
89
|
post = @post_class.new(:name => 'mongomapper')
|
91
90
|
comment = @comment_class.new(:name => 'Foo!', :post => post)
|
92
|
-
comment.save.should
|
91
|
+
comment.save.should be_truthy
|
93
92
|
|
94
93
|
comment = comment.reload
|
95
94
|
comment.post = nil
|
96
|
-
comment.post.nil?.should
|
95
|
+
comment.post.nil?.should be_truthy
|
97
96
|
end
|
98
97
|
|
99
98
|
it "should return nil if id set but document not found" do
|
100
99
|
id = BSON::ObjectId.new
|
101
|
-
@comment_class.new(:name => 'Foo', :post_id => id).post.nil?.should
|
100
|
+
@comment_class.new(:name => 'Foo', :post_id => id).post.nil?.should be_truthy
|
102
101
|
end
|
103
102
|
|
104
103
|
it "should define foreign key if it doesn't exist" do
|
105
104
|
@category_class = Doc()
|
106
105
|
@post_class.belongs_to :category, :class => @category_class
|
107
106
|
|
108
|
-
@post_class.key?(:category_id).should
|
107
|
+
@post_class.key?(:category_id).should be_truthy
|
109
108
|
end
|
110
109
|
|
111
110
|
it "should not define foreign key if it already exists" do
|
@@ -122,13 +121,13 @@ describe "BelongsToProxy" do
|
|
122
121
|
class ::Property
|
123
122
|
include MongoMapper::Document
|
124
123
|
end
|
125
|
-
Property.collection.
|
124
|
+
Property.collection.drop
|
126
125
|
|
127
126
|
class ::Thing
|
128
127
|
include MongoMapper::Document
|
129
128
|
key :name, String
|
130
129
|
end
|
131
|
-
Thing.collection.
|
130
|
+
Thing.collection.drop
|
132
131
|
end
|
133
132
|
|
134
133
|
after do
|
@@ -212,7 +211,7 @@ describe "BelongsToProxy" do
|
|
212
211
|
end
|
213
212
|
|
214
213
|
it "should raise exception if invalid" do
|
215
|
-
|
214
|
+
lambda { comment.create_post! }.should raise_error(MongoMapper::DocumentNotValid)
|
216
215
|
end
|
217
216
|
|
218
217
|
it "should work if valid" do
|
@@ -228,7 +227,7 @@ describe "BelongsToProxy" do
|
|
228
227
|
|
229
228
|
context 'autosave' do
|
230
229
|
it 'should not be true by default' do
|
231
|
-
@comment_class.associations[:post].options[:autosave].should_not
|
230
|
+
@comment_class.associations[:post].options[:autosave].should_not be_truthy
|
232
231
|
end
|
233
232
|
|
234
233
|
it 'should save parent changes when true' do
|
@@ -253,4 +252,4 @@ describe "BelongsToProxy" do
|
|
253
252
|
post.reload.title.should == 'Hello, world!'
|
254
253
|
end
|
255
254
|
end
|
256
|
-
end
|
255
|
+
end
|
@@ -14,8 +14,8 @@ describe "InArrayProxy" do
|
|
14
14
|
key :list_ids, Array
|
15
15
|
many :lists, :in => :list_ids
|
16
16
|
end
|
17
|
-
User.collection.
|
18
|
-
List.collection.
|
17
|
+
User.collection.drop
|
18
|
+
List.collection.drop
|
19
19
|
end
|
20
20
|
|
21
21
|
after do
|
@@ -50,7 +50,7 @@ describe "InArrayProxy" do
|
|
50
50
|
user = User.new(:name => 'John')
|
51
51
|
list = List.new(:name => 'Foo')
|
52
52
|
user.lists = [list]
|
53
|
-
user.save.should
|
53
|
+
user.save.should be_truthy
|
54
54
|
|
55
55
|
user.reload
|
56
56
|
user.list_ids.should == [list.id]
|
@@ -112,9 +112,9 @@ describe "InArrayProxy" do
|
|
112
112
|
end
|
113
113
|
|
114
114
|
it "should raise exception if invalid" do
|
115
|
-
|
115
|
+
lambda {
|
116
116
|
@user.lists.create!
|
117
|
-
}.
|
117
|
+
}.should raise_error(MongoMapper::DocumentNotValid)
|
118
118
|
end
|
119
119
|
|
120
120
|
it "should reset cache" do
|
@@ -195,9 +195,9 @@ describe "InArrayProxy" do
|
|
195
195
|
end
|
196
196
|
|
197
197
|
it "should raise error when using ! and not found" do
|
198
|
-
|
198
|
+
lambda {
|
199
199
|
@user.lists.find!(@list3.id)
|
200
|
-
}.
|
200
|
+
}.should raise_error(MongoMapper::DocumentNotFound)
|
201
201
|
end
|
202
202
|
end
|
203
203
|
|
@@ -249,9 +249,9 @@ describe "InArrayProxy" do
|
|
249
249
|
end
|
250
250
|
|
251
251
|
it "should raise error when using ! and not found" do
|
252
|
-
|
252
|
+
lambda {
|
253
253
|
@user.lists.find_by_name!('Foo 3')
|
254
|
-
}.
|
254
|
+
}.should raise_error(MongoMapper::DocumentNotFound)
|
255
255
|
end
|
256
256
|
|
257
257
|
context "find_or_create_by" do
|
@@ -346,4 +346,4 @@ describe "InArrayProxy" do
|
|
346
346
|
end
|
347
347
|
end
|
348
348
|
end
|
349
|
-
end
|
349
|
+
end
|
@@ -2,8 +2,8 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe "ManyDocumentsAsProxy" do
|
4
4
|
before do
|
5
|
-
Post.collection.
|
6
|
-
PostComment.collection.
|
5
|
+
Post.collection.drop
|
6
|
+
PostComment.collection.drop
|
7
7
|
end
|
8
8
|
|
9
9
|
it "should default reader to empty array" do
|
@@ -155,9 +155,9 @@ describe "ManyDocumentsAsProxy" do
|
|
155
155
|
end
|
156
156
|
|
157
157
|
it "should not work for id not in association" do
|
158
|
-
|
158
|
+
lambda {
|
159
159
|
@post.comments.find!(@comment5._id)
|
160
|
-
}.
|
160
|
+
}.should raise_error(MongoMapper::DocumentNotFound)
|
161
161
|
end
|
162
162
|
end
|
163
163
|
|
@@ -168,9 +168,9 @@ describe "ManyDocumentsAsProxy" do
|
|
168
168
|
end
|
169
169
|
|
170
170
|
it "should not work for ids not in association" do
|
171
|
-
|
171
|
+
lambda {
|
172
172
|
@post.comments.find!(@comment1._id, @comment2._id, @comment4._id)
|
173
|
-
}.
|
173
|
+
}.should raise_error(MongoMapper::DocumentNotFound)
|
174
174
|
end
|
175
175
|
end
|
176
176
|
|
@@ -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
|
@@ -105,7 +105,7 @@ describe "ManyDocumentsProxy" do
|
|
105
105
|
owner.pets[1].name.should == 'Sasha'
|
106
106
|
owner.pets[1].species.should == 'Siberian Husky'
|
107
107
|
|
108
|
-
owner.save.should
|
108
|
+
owner.save.should be_truthy
|
109
109
|
owner.reload
|
110
110
|
|
111
111
|
owner.name.should == 'Mr. Pet Lover'
|
@@ -128,7 +128,7 @@ describe "ManyDocumentsProxy" do
|
|
128
128
|
it "should work" do
|
129
129
|
project = Project.new
|
130
130
|
project.statuses = [Status.new(:name => "ready")]
|
131
|
-
project.save.should
|
131
|
+
project.save.should be_truthy
|
132
132
|
|
133
133
|
project.reload
|
134
134
|
project.statuses.size.should == 1
|
@@ -140,7 +140,7 @@ describe "ManyDocumentsProxy" do
|
|
140
140
|
it "should convert to objects of the class and work" do
|
141
141
|
project = Project.new
|
142
142
|
project.statuses = [{ 'name' => 'ready' }]
|
143
|
-
project.save.should
|
143
|
+
project.save.should be_truthy
|
144
144
|
|
145
145
|
project.reload
|
146
146
|
project.statuses.size.should == 1
|
@@ -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
|
@@ -491,19 +491,19 @@ describe "ManyDocumentsProxy" do
|
|
491
491
|
context "empty?" do
|
492
492
|
it "should be true with no associated docs" do
|
493
493
|
project = Project.create
|
494
|
-
project.statuses.empty?.should
|
494
|
+
project.statuses.empty?.should be_truthy
|
495
495
|
end
|
496
496
|
|
497
497
|
it "should be false if a document is built" do
|
498
498
|
project = Project.create
|
499
499
|
project.statuses.build(:name => 'Foo!')
|
500
|
-
project.statuses.empty?.should
|
500
|
+
project.statuses.empty?.should be_falsey
|
501
501
|
end
|
502
502
|
|
503
503
|
it "should be false if a document is created" do
|
504
504
|
project = Project.create
|
505
505
|
project.statuses.create(:name => 'Foo!')
|
506
|
-
project.statuses.empty?.should
|
506
|
+
project.statuses.empty?.should be_falsey
|
507
507
|
end
|
508
508
|
end
|
509
509
|
|
@@ -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
|
@@ -950,4 +950,75 @@ describe "ManyDocumentsProxy" do
|
|
950
950
|
@paper.articles.should include(article)
|
951
951
|
end
|
952
952
|
end
|
953
|
+
|
954
|
+
describe "regression with association proxy scoping" do
|
955
|
+
before do
|
956
|
+
@job_title_class = Doc do
|
957
|
+
set_collection_name "job_titles"
|
958
|
+
end
|
959
|
+
|
960
|
+
@training_class = Doc do
|
961
|
+
set_collection_name "trainings"
|
962
|
+
key :slug, String
|
963
|
+
key :is_active, Boolean, default: true
|
964
|
+
|
965
|
+
def self.find_by_slug!(the_slug)
|
966
|
+
if res = first(:slug => the_slug)
|
967
|
+
res
|
968
|
+
else
|
969
|
+
raise "MissingSlugError"
|
970
|
+
end
|
971
|
+
end
|
972
|
+
|
973
|
+
validates_presence_of :job_title_id
|
974
|
+
validates_presence_of :slug
|
975
|
+
validates_uniqueness_of :slug, :scope => :job_title_id
|
976
|
+
end
|
977
|
+
|
978
|
+
@job_title_class.has_many :trainings, :class => @training_class, :foreign_key => :job_title_id
|
979
|
+
@training_class.belongs_to :job_title, :class => @job_title_class
|
980
|
+
end
|
981
|
+
|
982
|
+
it "should scope queries that return a single method on has many association with the right parent id" do
|
983
|
+
@job_title_1 = @job_title_class.create!
|
984
|
+
@job_title_2 = @job_title_class.create!
|
985
|
+
|
986
|
+
@training_1 = @training_class.create!(:slug => 'foo', :job_title_id => @job_title_1.id)
|
987
|
+
@training_2 = @training_class.create!(:slug => 'foo', :job_title_id => @job_title_2.id)
|
988
|
+
|
989
|
+
@job_title_1.reload
|
990
|
+
@job_title_2.reload
|
991
|
+
|
992
|
+
@job_title_1.trainings.count.should == 1
|
993
|
+
@job_title_2.trainings.count.should == 1
|
994
|
+
|
995
|
+
@job_title_1.trainings.find_by_slug!('foo').should == @training_1
|
996
|
+
@job_title_2.trainings.find_by_slug!('foo').should == @training_2
|
997
|
+
|
998
|
+
lambda do
|
999
|
+
@job_title_2.trainings.find_by_slug!('bar')
|
1000
|
+
end.should raise_error(RuntimeError)
|
1001
|
+
end
|
1002
|
+
|
1003
|
+
it "should scope with an extra where clause on the proxy (regression #2)" do
|
1004
|
+
@job_title_1 = @job_title_class.create!
|
1005
|
+
@job_title_2 = @job_title_class.create!
|
1006
|
+
|
1007
|
+
@training_1 = @training_class.create!(:slug => 'foo', :job_title_id => @job_title_1.id)
|
1008
|
+
@training_2 = @training_class.create!(:slug => 'foo', :job_title_id => @job_title_2.id)
|
1009
|
+
|
1010
|
+
@job_title_1.reload
|
1011
|
+
@job_title_2.reload
|
1012
|
+
|
1013
|
+
@job_title_1.trainings.count.should == 1
|
1014
|
+
@job_title_2.trainings.count.should == 1
|
1015
|
+
|
1016
|
+
@job_title_1.trainings.where(:is_active => true).find_by_slug!('foo').should == @training_1
|
1017
|
+
@job_title_2.trainings.where(:is_active => true).find_by_slug!('foo').should == @training_2
|
1018
|
+
|
1019
|
+
lambda do
|
1020
|
+
@job_title_2.trainings.find_by_slug!('bar')
|
1021
|
+
end.should raise_error(RuntimeError)
|
1022
|
+
end
|
1023
|
+
end
|
953
1024
|
end
|