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.
Files changed (137) hide show
  1. checksums.yaml +5 -5
  2. data/LICENSE +1 -1
  3. data/README.md +61 -0
  4. data/examples/keys.rb +1 -1
  5. data/examples/modifiers/set.rb +1 -1
  6. data/examples/querying.rb +1 -1
  7. data/examples/safe.rb +2 -2
  8. data/examples/scopes.rb +1 -1
  9. data/lib/mongo_mapper.rb +7 -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 +15 -5
  19. data/lib/mongo_mapper/plugins/associations.rb +7 -6
  20. data/lib/mongo_mapper/plugins/associations/base.rb +27 -14
  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 +60 -29
  26. data/lib/mongo_mapper/plugins/associations/in_foreign_array_proxy.rb +136 -0
  27. data/lib/mongo_mapper/plugins/associations/many_association.rb +4 -2
  28. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +18 -16
  29. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +55 -48
  30. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +14 -13
  31. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +7 -6
  32. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +7 -5
  33. data/lib/mongo_mapper/plugins/associations/one_as_proxy.rb +14 -11
  34. data/lib/mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb +14 -13
  35. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +9 -9
  36. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +27 -26
  37. data/lib/mongo_mapper/plugins/associations/proxy.rb +36 -29
  38. data/lib/mongo_mapper/plugins/associations/single_association.rb +5 -4
  39. data/lib/mongo_mapper/plugins/callbacks.rb +13 -0
  40. data/lib/mongo_mapper/plugins/counter_cache.rb +97 -0
  41. data/lib/mongo_mapper/plugins/dirty.rb +29 -37
  42. data/lib/mongo_mapper/plugins/document.rb +1 -1
  43. data/lib/mongo_mapper/plugins/dynamic_querying.rb +10 -9
  44. data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +18 -17
  45. data/lib/mongo_mapper/plugins/embedded_callbacks.rb +2 -1
  46. data/lib/mongo_mapper/plugins/embedded_document.rb +1 -1
  47. data/lib/mongo_mapper/plugins/identity_map.rb +4 -2
  48. data/lib/mongo_mapper/plugins/indexes.rb +14 -7
  49. data/lib/mongo_mapper/plugins/keys.rb +170 -151
  50. data/lib/mongo_mapper/plugins/keys/key.rb +27 -16
  51. data/lib/mongo_mapper/plugins/keys/static.rb +45 -0
  52. data/lib/mongo_mapper/plugins/modifiers.rb +64 -38
  53. data/lib/mongo_mapper/plugins/partial_updates.rb +86 -0
  54. data/lib/mongo_mapper/plugins/persistence.rb +13 -8
  55. data/lib/mongo_mapper/plugins/protected.rb +6 -5
  56. data/lib/mongo_mapper/plugins/querying.rb +85 -42
  57. data/lib/mongo_mapper/plugins/querying/decorated_plucky_query.rb +20 -15
  58. data/lib/mongo_mapper/plugins/rails.rb +1 -0
  59. data/lib/mongo_mapper/plugins/safe.rb +10 -4
  60. data/lib/mongo_mapper/plugins/sci.rb +0 -0
  61. data/lib/mongo_mapper/plugins/scopes.rb +78 -7
  62. data/lib/mongo_mapper/plugins/stats.rb +17 -0
  63. data/lib/mongo_mapper/plugins/strong_parameters.rb +26 -0
  64. data/lib/mongo_mapper/plugins/timestamps.rb +1 -0
  65. data/lib/mongo_mapper/plugins/validations.rb +1 -1
  66. data/lib/mongo_mapper/railtie.rb +4 -3
  67. data/lib/mongo_mapper/utils.rb +2 -2
  68. data/lib/mongo_mapper/version.rb +1 -1
  69. data/lib/rails/generators/mongo_mapper/config/config_generator.rb +12 -13
  70. data/lib/rails/generators/mongo_mapper/model/model_generator.rb +9 -9
  71. data/spec/examples.txt +1717 -0
  72. data/spec/functional/accessible_spec.rb +19 -13
  73. data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +13 -13
  74. data/spec/functional/associations/belongs_to_proxy_spec.rb +36 -20
  75. data/spec/functional/associations/in_array_proxy_spec.rb +145 -10
  76. data/spec/functional/associations/in_foreign_array_proxy_spec.rb +321 -0
  77. data/spec/functional/associations/many_documents_as_proxy_spec.rb +6 -6
  78. data/spec/functional/associations/many_documents_proxy_spec.rb +85 -14
  79. data/spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb +13 -13
  80. data/spec/functional/associations/many_embedded_proxy_spec.rb +1 -1
  81. data/spec/functional/associations/many_polymorphic_proxy_spec.rb +4 -4
  82. data/spec/functional/associations/one_as_proxy_spec.rb +10 -10
  83. data/spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb +9 -9
  84. data/spec/functional/associations/one_embedded_proxy_spec.rb +3 -3
  85. data/spec/functional/associations/one_proxy_spec.rb +10 -10
  86. data/spec/functional/associations_spec.rb +3 -3
  87. data/spec/functional/binary_spec.rb +2 -2
  88. data/spec/functional/caching_spec.rb +8 -15
  89. data/spec/functional/callbacks_spec.rb +89 -2
  90. data/spec/functional/counter_cache_spec.rb +235 -0
  91. data/spec/functional/dirty_spec.rb +63 -46
  92. data/spec/functional/document_spec.rb +30 -5
  93. data/spec/functional/dumpable_spec.rb +1 -1
  94. data/spec/functional/embedded_document_spec.rb +17 -17
  95. data/spec/functional/identity_map_spec.rb +29 -16
  96. data/spec/functional/indexes_spec.rb +19 -18
  97. data/spec/functional/keys_spec.rb +86 -28
  98. data/spec/functional/logger_spec.rb +3 -3
  99. data/spec/functional/modifiers_spec.rb +81 -19
  100. data/spec/functional/partial_updates_spec.rb +577 -0
  101. data/spec/functional/protected_spec.rb +14 -14
  102. data/spec/functional/querying_spec.rb +77 -28
  103. data/spec/functional/safe_spec.rb +23 -27
  104. data/spec/functional/sci_spec.rb +9 -9
  105. data/spec/functional/scopes_spec.rb +235 -2
  106. data/spec/functional/static_keys_spec.rb +153 -0
  107. data/spec/functional/stats_spec.rb +86 -0
  108. data/spec/functional/strong_parameters_spec.rb +49 -0
  109. data/spec/functional/touch_spec.rb +1 -1
  110. data/spec/functional/validations_spec.rb +51 -57
  111. data/spec/quality_spec.rb +51 -0
  112. data/spec/spec_helper.rb +37 -9
  113. data/spec/support/matchers.rb +5 -14
  114. data/spec/unit/associations/base_spec.rb +12 -12
  115. data/spec/unit/associations/belongs_to_association_spec.rb +2 -2
  116. data/spec/unit/associations/many_association_spec.rb +2 -2
  117. data/spec/unit/associations/one_association_spec.rb +2 -2
  118. data/spec/unit/associations/proxy_spec.rb +19 -20
  119. data/spec/unit/clone_spec.rb +1 -1
  120. data/spec/unit/document_spec.rb +8 -8
  121. data/spec/unit/dynamic_finder_spec.rb +8 -8
  122. data/spec/unit/embedded_document_spec.rb +18 -19
  123. data/spec/unit/extensions_spec.rb +41 -17
  124. data/spec/unit/identity_map_middleware_spec.rb +65 -96
  125. data/spec/unit/key_spec.rb +28 -26
  126. data/spec/unit/keys_spec.rb +20 -11
  127. data/spec/unit/model_generator_spec.rb +0 -0
  128. data/spec/unit/mongo_mapper_spec.rb +38 -85
  129. data/spec/unit/rails_spec.rb +5 -0
  130. data/spec/unit/serialization_spec.rb +1 -1
  131. data/spec/unit/time_zones_spec.rb +2 -2
  132. data/spec/unit/validations_spec.rb +46 -33
  133. metadata +66 -37
  134. data/README.rdoc +0 -59
  135. data/lib/mongo_mapper/connections/10gen.rb +0 -0
  136. data/lib/mongo_mapper/connections/moped.rb +0 -0
  137. data/lib/mongo_mapper/extensions/ordered_hash.rb +0 -23
@@ -0,0 +1,321 @@
1
+ require 'spec_helper'
2
+
3
+ describe "InForeignArrayProxyTest" do
4
+ context "description" do
5
+ before do
6
+ class ::List
7
+ include MongoMapper::Document
8
+ key :name, String, :required => true
9
+ key :user_ids, Array
10
+ many :users, :in => :user_ids
11
+ end
12
+
13
+ class ::User
14
+ include MongoMapper::Document
15
+ key :name, String, :required => true
16
+ many :lists, :from => :user_ids, :as => :user
17
+ end
18
+ User.collection.drop
19
+ List.collection.drop
20
+ end
21
+
22
+ after do
23
+ Object.send :remove_const, 'List' if defined?(::List)
24
+ Object.send :remove_const, 'User' if defined?(::User)
25
+ end
26
+
27
+ it "should default reader to empty array" do
28
+ User.new.lists.should == []
29
+ end
30
+
31
+ it "should allow adding to association like it was an array" do
32
+ user = User.new(:name => 'John')
33
+ user.lists << List.new(:name => 'Foo1!')
34
+ user.lists.push List.new(:name => 'Foo2!')
35
+ user.lists.concat List.new(:name => 'Foo3!')
36
+ user.lists.size.should == 3
37
+ end
38
+
39
+ it "should ignore adding duplicate ids" do
40
+ user = User.create(:name => 'John')
41
+ list = List.create(:name => 'Foo')
42
+ user.lists << list
43
+ user.lists << list
44
+ user.lists << list
45
+
46
+ list.reload.user_ids.should == [user.id]
47
+ user.lists.count.should == 1
48
+ end
49
+
50
+ it "should be able to replace the association" do
51
+ user = User.new(:name => 'John')
52
+ list = List.new(:name => 'Foo')
53
+ user.lists = [list]
54
+ user.save.should == true
55
+
56
+ user.reload
57
+ list.reload
58
+ list.user_ids.should == [user.id]
59
+ user.lists.size.should == 1
60
+ user.lists[0].name.should == 'Foo'
61
+ end
62
+
63
+ context "create" do
64
+ before do
65
+ @user = User.create(:name => 'John')
66
+ @list = @user.lists.create(:name => 'Foo!')
67
+ end
68
+
69
+ it "should add id to key" do
70
+ @list.user_ids.should include(@user.id)
71
+ end
72
+
73
+ it "should persist id addition to key in database" do
74
+ @list.reload
75
+ @list.user_ids.should include(@user.id)
76
+ end
77
+
78
+ it "should add doc to association" do
79
+ @user.lists.should include(@list)
80
+ end
81
+
82
+ it "should save doc" do
83
+ @list.should_not be_new
84
+ end
85
+
86
+ it "should reset cache" do
87
+ @user.lists.size.should == 1
88
+ @user.lists.create(:name => 'Moo!')
89
+ @user.lists.size.should == 2
90
+ end
91
+ end
92
+
93
+ context "create!" do
94
+ before do
95
+ @user = User.create(:name => 'John')
96
+ @list = @user.lists.create!(:name => 'Foo!')
97
+ end
98
+
99
+ it "should add id to key" do
100
+ @list.user_ids.should include(@user.id)
101
+ end
102
+
103
+ it "should persist id addition to key in database" do
104
+ @list.reload
105
+ @list.user_ids.should include(@user.id)
106
+ end
107
+
108
+ it "should add doc to association" do
109
+ @user.lists.should include(@list)
110
+ end
111
+
112
+ it "should save doc" do
113
+ @list.should_not be_new
114
+ end
115
+
116
+ it "should raise exception if invalid" do
117
+ lambda do
118
+ @user.lists.create!
119
+ end.should raise_error(MongoMapper::DocumentNotValid)
120
+ end
121
+
122
+ it "should reset cache" do
123
+ @user.lists.size.should == 1
124
+ @user.lists.create!(:name => 'Moo!')
125
+ @user.lists.size.should == 2
126
+ end
127
+ end
128
+
129
+ context "Finding scoped to association" do
130
+ before do
131
+ @user = User.create(:name => 'John')
132
+ @user2 = User.create(:name => 'Brandon')
133
+ @list1 = @user.lists.create!(:name => 'Foo 1', :position => 1)
134
+ @list2 = @user.lists.create!(:name => 'Foo 2', :position => 2)
135
+ @list3 = @user2.lists.create!(:name => 'Foo 3', :position => 1)
136
+ end
137
+
138
+ context "all" do
139
+ it "should work" do
140
+ @user.lists.all(:order => :position.asc).should == [@list1, @list2]
141
+ end
142
+
143
+ it "should work with conditions" do
144
+ @user.lists.all(:name => 'Foo 1').should == [@list1]
145
+ end
146
+ end
147
+
148
+ context "first" do
149
+ it "should work" do
150
+ @user.lists.first(:order => 'position').should == @list1
151
+ end
152
+
153
+ it "should work with conditions" do
154
+ @user.lists.first(:position => 2).should == @list2
155
+ end
156
+ end
157
+
158
+ context "last" do
159
+ it "should work" do
160
+ @user.lists.last(:order => 'position').should == @list2
161
+ end
162
+
163
+ it "should work with conditions" do
164
+ @user.lists.last(:position => 2, :order => 'position').should == @list2
165
+ end
166
+ end
167
+
168
+ context "with one id" do
169
+ it "should work for id in association" do
170
+ @user.lists.find(@list1.id).should == @list1
171
+ end
172
+
173
+ it "should work with string ids" do
174
+ @user.lists.find(@list1.id.to_s).should == @list1
175
+ end
176
+
177
+ it "should not work for id not in association" do
178
+ @user.lists.find(@list3.id).should be_nil
179
+ end
180
+
181
+ it "should raise error when using ! and not found" do
182
+ lambda do
183
+ @user.lists.find!(@list3.id)
184
+ end.should raise_error(MongoMapper::DocumentNotFound)
185
+ end
186
+ end
187
+
188
+ context "with multiple ids" do
189
+ it "should work for ids in association" do
190
+ @user.lists.find(@list1.id, @list2.id).should == [@list1, @list2]
191
+ end
192
+
193
+ it "should not work for ids not in association" do
194
+ @user.lists.find(@list1.id, @list2.id, @list3.id).should == [@list1, @list2]
195
+ end
196
+ end
197
+
198
+ context "with #paginate" do
199
+ before do
200
+ @lists = @user.lists.paginate(:per_page => 1, :page => 1, :order => 'position')
201
+ end
202
+
203
+ it "should return total pages" do
204
+ @lists.total_pages.should == 2
205
+ end
206
+
207
+ it "should return total entries" do
208
+ @lists.total_entries.should == 2
209
+ end
210
+
211
+ it "should return the subject" do
212
+ @lists.collect(&:name).should == ['Foo 1']
213
+ end
214
+ end
215
+
216
+ context "dynamic finders" do
217
+ it "should work with single key" do
218
+ @user.lists.find_by_name('Foo 1').should == @list1
219
+ @user.lists.find_by_name!('Foo 1').should == @list1
220
+ @user.lists.find_by_name('Foo 3').should be_nil
221
+ end
222
+
223
+ it "should work with multiple keys" do
224
+ @user.lists.find_by_name_and_position('Foo 1', 1).should == @list1
225
+ @user.lists.find_by_name_and_position!('Foo 1', 1).should == @list1
226
+ @user.lists.find_by_name_and_position('Foo 3', 1).should be_nil
227
+ end
228
+
229
+ it "should raise error when using ! and not found" do
230
+ lambda do
231
+ @user.lists.find_by_name!('Foo 3')
232
+ end.should raise_error(MongoMapper::DocumentNotFound)
233
+ end
234
+
235
+ context "find_or_create_by" do
236
+ it "should not create document if found" do
237
+ lambda {
238
+ list = @user.lists.find_or_create_by_name('Foo 1')
239
+ list.should == @list1
240
+ }.should_not change { List.count }
241
+ end
242
+
243
+ it "should create document if not found" do
244
+ lambda {
245
+ list = @user.lists.find_or_create_by_name('Home')
246
+ @user.lists.should include(list)
247
+ }.should change { List.count }
248
+ end
249
+ end
250
+ end
251
+ end
252
+
253
+ context "count" do
254
+ before do
255
+ @user = User.create(:name => 'John')
256
+ @user2 = User.create(:name => 'Brandon')
257
+ @list1 = @user.lists.create!(:name => 'Foo 1')
258
+ @list2 = @user.lists.create!(:name => 'Foo 2')
259
+ @list3 = @user2.lists.create!(:name => 'Foo 3')
260
+ end
261
+
262
+ it "should return number of ids" do
263
+ @user.lists.count.should == 2
264
+ @user2.lists.count.should == 1
265
+ end
266
+
267
+ it "should return correct count when given criteria" do
268
+ @user.lists.count(:name => 'Foo 1').should == 1
269
+ @user2.lists.count(:name => 'Foo 1').should == 0
270
+ end
271
+ end
272
+
273
+ context "Removing documents" do
274
+ before do
275
+ @user = User.create(:name => 'John')
276
+ @user2 = User.create(:name => 'Brandon')
277
+ @list1 = @user.lists.create!(:name => 'Foo 1', :position => 1)
278
+ @list2 = @user.lists.create!(:name => 'Foo 2', :position => 2)
279
+ @list3 = @user2.lists.create!(:name => 'Foo 3', :position => 1)
280
+ end
281
+
282
+ context "destroy_all" do
283
+ it "should work" do
284
+ @user.lists.count.should == 2
285
+ @user.lists.destroy_all
286
+ @user.lists.count.should == 0
287
+ end
288
+
289
+ it "should work with conditions" do
290
+ @user.lists.count.should == 2
291
+ @user.lists.destroy_all(:name => 'Foo 1')
292
+ @user.lists.count.should == 1
293
+ end
294
+ end
295
+
296
+ context "delete_all" do
297
+ it "should work" do
298
+ @user.lists.count.should == 2
299
+ @user.lists.delete_all
300
+ @user.lists.count.should == 0
301
+ end
302
+
303
+ it "should work with conditions" do
304
+ @user.lists.count.should == 2
305
+ @user.lists.delete_all(:name => 'Foo 1')
306
+ @user.lists.count.should == 1
307
+ end
308
+ end
309
+
310
+ it "should work with nullify" do
311
+ @user.lists.count.should == 2
312
+
313
+ lambda {
314
+ @user.lists.nullify
315
+ }.should_not change { List.count }
316
+
317
+ @user.lists.count.should == 0
318
+ end
319
+ end
320
+ end
321
+ 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