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.
Files changed (132) hide show
  1. checksums.yaml +5 -5
  2. data/LICENSE +1 -1
  3. data/README.md +64 -0
  4. data/examples/keys.rb +3 -3
  5. data/examples/modifiers/set.rb +2 -2
  6. data/examples/querying.rb +3 -3
  7. data/examples/safe.rb +2 -2
  8. data/examples/scopes.rb +1 -1
  9. data/lib/mongo_mapper.rb +5 -0
  10. data/lib/mongo_mapper/connection.rb +16 -37
  11. data/lib/mongo_mapper/document.rb +4 -0
  12. data/lib/mongo_mapper/extensions/array.rb +14 -6
  13. data/lib/mongo_mapper/extensions/hash.rb +15 -3
  14. data/lib/mongo_mapper/extensions/object.rb +4 -0
  15. data/lib/mongo_mapper/extensions/object_id.rb +5 -1
  16. data/lib/mongo_mapper/extensions/string.rb +13 -5
  17. data/lib/mongo_mapper/extensions/symbol.rb +18 -0
  18. data/lib/mongo_mapper/plugins/accessible.rb +14 -4
  19. data/lib/mongo_mapper/plugins/associations.rb +7 -6
  20. data/lib/mongo_mapper/plugins/associations/base.rb +18 -13
  21. data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +10 -1
  22. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +9 -8
  23. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +12 -11
  24. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +4 -4
  25. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +24 -23
  26. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +18 -16
  27. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +55 -48
  28. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +14 -13
  29. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +7 -6
  30. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +7 -5
  31. data/lib/mongo_mapper/plugins/associations/one_as_proxy.rb +17 -14
  32. data/lib/mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb +14 -13
  33. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +9 -9
  34. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +27 -26
  35. data/lib/mongo_mapper/plugins/associations/proxy.rb +31 -28
  36. data/lib/mongo_mapper/plugins/callbacks.rb +14 -1
  37. data/lib/mongo_mapper/plugins/counter_cache.rb +97 -0
  38. data/lib/mongo_mapper/plugins/dirty.rb +29 -37
  39. data/lib/mongo_mapper/plugins/document.rb +1 -1
  40. data/lib/mongo_mapper/plugins/dynamic_querying.rb +10 -9
  41. data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +18 -17
  42. data/lib/mongo_mapper/plugins/embedded_callbacks.rb +2 -1
  43. data/lib/mongo_mapper/plugins/embedded_document.rb +1 -1
  44. data/lib/mongo_mapper/plugins/identity_map.rb +1 -1
  45. data/lib/mongo_mapper/plugins/indexes.rb +37 -2
  46. data/lib/mongo_mapper/plugins/keys.rb +202 -142
  47. data/lib/mongo_mapper/plugins/keys/key.rb +22 -13
  48. data/lib/mongo_mapper/plugins/keys/static.rb +45 -0
  49. data/lib/mongo_mapper/plugins/modifiers.rb +59 -28
  50. data/lib/mongo_mapper/plugins/partial_updates.rb +86 -0
  51. data/lib/mongo_mapper/plugins/persistence.rb +13 -8
  52. data/lib/mongo_mapper/plugins/protected.rb +6 -5
  53. data/lib/mongo_mapper/plugins/querying.rb +85 -42
  54. data/lib/mongo_mapper/plugins/querying/decorated_plucky_query.rb +32 -9
  55. data/lib/mongo_mapper/plugins/rails.rb +1 -0
  56. data/lib/mongo_mapper/plugins/safe.rb +10 -4
  57. data/lib/mongo_mapper/plugins/sci.rb +4 -1
  58. data/lib/mongo_mapper/plugins/scopes.rb +78 -7
  59. data/lib/mongo_mapper/plugins/stats.rb +17 -0
  60. data/lib/mongo_mapper/plugins/timestamps.rb +1 -0
  61. data/lib/mongo_mapper/plugins/touch.rb +1 -1
  62. data/lib/mongo_mapper/plugins/validations.rb +7 -2
  63. data/lib/mongo_mapper/railtie.rb +20 -0
  64. data/lib/mongo_mapper/railtie/database.rake +1 -1
  65. data/lib/mongo_mapper/utils.rb +2 -2
  66. data/lib/mongo_mapper/version.rb +1 -1
  67. data/lib/rails/generators/mongo_mapper/config/config_generator.rb +12 -13
  68. data/lib/rails/generators/mongo_mapper/model/model_generator.rb +9 -9
  69. data/spec/examples.txt +1643 -0
  70. data/spec/functional/accessible_spec.rb +13 -13
  71. data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +13 -13
  72. data/spec/functional/associations/belongs_to_proxy_spec.rb +18 -19
  73. data/spec/functional/associations/in_array_proxy_spec.rb +10 -10
  74. data/spec/functional/associations/many_documents_as_proxy_spec.rb +6 -6
  75. data/spec/functional/associations/many_documents_proxy_spec.rb +85 -14
  76. data/spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb +13 -13
  77. data/spec/functional/associations/many_embedded_proxy_spec.rb +1 -1
  78. data/spec/functional/associations/many_polymorphic_proxy_spec.rb +4 -4
  79. data/spec/functional/associations/one_as_proxy_spec.rb +10 -10
  80. data/spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb +9 -9
  81. data/spec/functional/associations/one_embedded_proxy_spec.rb +3 -3
  82. data/spec/functional/associations/one_proxy_spec.rb +10 -10
  83. data/spec/functional/associations_spec.rb +3 -3
  84. data/spec/functional/binary_spec.rb +2 -2
  85. data/spec/functional/caching_spec.rb +8 -15
  86. data/spec/functional/callbacks_spec.rb +89 -2
  87. data/spec/functional/counter_cache_spec.rb +235 -0
  88. data/spec/functional/dirty_spec.rb +63 -46
  89. data/spec/functional/document_spec.rb +30 -2
  90. data/spec/functional/dumpable_spec.rb +1 -1
  91. data/spec/functional/embedded_document_spec.rb +18 -18
  92. data/spec/functional/identity_map_spec.rb +27 -14
  93. data/spec/functional/indexes_spec.rb +44 -19
  94. data/spec/functional/keys_spec.rb +117 -15
  95. data/spec/functional/logger_spec.rb +3 -3
  96. data/spec/functional/modifiers_spec.rb +67 -19
  97. data/spec/functional/partial_updates_spec.rb +577 -0
  98. data/spec/functional/protected_spec.rb +14 -14
  99. data/spec/functional/querying_spec.rb +55 -28
  100. data/spec/functional/safe_spec.rb +23 -27
  101. data/spec/functional/sci_spec.rb +49 -14
  102. data/spec/functional/scopes_spec.rb +235 -2
  103. data/spec/functional/static_keys_spec.rb +153 -0
  104. data/spec/functional/stats_spec.rb +86 -0
  105. data/spec/functional/touch_spec.rb +6 -6
  106. data/spec/functional/validations_spec.rb +51 -57
  107. data/spec/quality_spec.rb +51 -0
  108. data/spec/spec_helper.rb +37 -9
  109. data/spec/support/matchers.rb +5 -14
  110. data/spec/unit/associations/base_spec.rb +12 -12
  111. data/spec/unit/associations/belongs_to_association_spec.rb +2 -2
  112. data/spec/unit/associations/many_association_spec.rb +2 -2
  113. data/spec/unit/associations/one_association_spec.rb +2 -2
  114. data/spec/unit/associations/proxy_spec.rb +19 -16
  115. data/spec/unit/clone_spec.rb +1 -1
  116. data/spec/unit/document_spec.rb +8 -8
  117. data/spec/unit/dynamic_finder_spec.rb +8 -8
  118. data/spec/unit/embedded_document_spec.rb +18 -19
  119. data/spec/unit/extensions_spec.rb +41 -17
  120. data/spec/unit/identity_map_middleware_spec.rb +65 -96
  121. data/spec/unit/inspect_spec.rb +4 -4
  122. data/spec/unit/key_spec.rb +28 -26
  123. data/spec/unit/keys_spec.rb +10 -10
  124. data/spec/unit/model_generator_spec.rb +2 -4
  125. data/spec/unit/mongo_mapper_spec.rb +38 -85
  126. data/spec/unit/rails_spec.rb +5 -0
  127. data/spec/unit/serialization_spec.rb +1 -1
  128. data/spec/unit/time_zones_spec.rb +2 -2
  129. data/spec/unit/validations_spec.rb +28 -15
  130. metadata +188 -161
  131. data/README.rdoc +0 -55
  132. 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 be_true
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 be_false
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 be_true
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 be_true
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 be_true
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 be_false
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 be_false
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 be_false
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 be_false
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.remove
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 be_true
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 be_false
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 be_false
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.remove
7
- Project.collection.remove
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 be_true
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 be_false
18
+ status.target?.should be_falsey
19
19
 
20
20
  status.target = Project.new(:name => 'mongomapper')
21
- status.target?.should be_true
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 be_true
28
+ status.save.should be_truthy
29
29
 
30
30
  status = status.reload
31
- status.target.nil?.should be_false
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 be_true
41
+ status.save.should be_truthy
42
42
 
43
43
  status = status.reload
44
44
  status.target = nil
45
- status.target_type.nil?.should be_true
46
- status.target_id.nil?.should be_true
47
- status.target.nil?.should be_true
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 be_true
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 be_true
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 be_true
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 be_false
22
+ comment.post?.should be_falsey
24
23
 
25
24
  comment.post = @post_class.new(:name => 'mongomapper')
26
- comment.post?.should be_true
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 be_false
36
+ instance.post?.should be_falsey
38
37
  instance.post = @post_class.new
39
- instance.post?.should be_true
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 be_true
44
+ comment.save.should be_truthy
46
45
 
47
46
  comment = comment.reload
48
47
  comment.post.should == post
49
- comment.post.nil?.should be_false
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 be_true
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 be_false
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 be_true
91
+ comment.save.should be_truthy
93
92
 
94
93
  comment = comment.reload
95
94
  comment.post = nil
96
- comment.post.nil?.should be_true
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 be_true
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 be_true
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.remove
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.remove
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
- expect { comment.create_post! }.to raise_error(MongoMapper::DocumentNotValid)
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 be_true
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.remove
18
- List.collection.remove
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 be_true
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
- expect {
115
+ lambda {
116
116
  @user.lists.create!
117
- }.to raise_error(MongoMapper::DocumentNotValid)
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
- expect {
198
+ lambda {
199
199
  @user.lists.find!(@list3.id)
200
- }.to raise_error(MongoMapper::DocumentNotFound)
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
- expect {
252
+ lambda {
253
253
  @user.lists.find_by_name!('Foo 3')
254
- }.to raise_error(MongoMapper::DocumentNotFound)
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.remove
6
- PostComment.collection.remove
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
- expect {
158
+ lambda {
159
159
  @post.comments.find!(@comment5._id)
160
- }.to raise_error(MongoMapper::DocumentNotFound)
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
- expect {
171
+ lambda {
172
172
  @post.comments.find!(@comment1._id, @comment2._id, @comment4._id)
173
- }.to raise_error(MongoMapper::DocumentNotFound)
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.remove
6
- Status.collection.remove
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 be_true
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 be_true
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 be_true
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
- expect {
433
+ lambda {
434
434
  project.statuses.create!(:name => nil)
435
- }.to raise_error(MongoMapper::DocumentNotValid)
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 be_true
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 be_false
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 be_false
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
- expect {
757
+ lambda {
758
758
  @project1.statuses.find!(@brand_new.id, @complete.id, @archived.id)
759
- }.to raise_error(MongoMapper::DocumentNotFound)
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.remove
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.remove
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