mongo_mapper-unstable 2010.3.8 → 2010.06.23

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. data/README.rdoc +4 -8
  2. data/bin/mmconsole +1 -1
  3. data/examples/keys.rb +37 -0
  4. data/examples/plugins.rb +41 -0
  5. data/examples/querying.rb +35 -0
  6. data/examples/scopes.rb +52 -0
  7. data/lib/mongo_mapper/connection.rb +83 -0
  8. data/lib/mongo_mapper/document.rb +11 -329
  9. data/lib/mongo_mapper/embedded_document.rb +9 -38
  10. data/lib/mongo_mapper/exceptions.rb +30 -0
  11. data/lib/mongo_mapper/extensions/array.rb +19 -0
  12. data/lib/mongo_mapper/extensions/binary.rb +22 -0
  13. data/lib/mongo_mapper/extensions/boolean.rb +44 -0
  14. data/lib/mongo_mapper/extensions/date.rb +25 -0
  15. data/lib/mongo_mapper/extensions/float.rb +14 -0
  16. data/lib/mongo_mapper/extensions/hash.rb +14 -0
  17. data/lib/mongo_mapper/extensions/integer.rb +19 -0
  18. data/lib/mongo_mapper/extensions/kernel.rb +9 -0
  19. data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
  20. data/lib/mongo_mapper/extensions/object.rb +27 -0
  21. data/lib/mongo_mapper/extensions/object_id.rb +30 -0
  22. data/lib/mongo_mapper/extensions/set.rb +20 -0
  23. data/lib/mongo_mapper/extensions/string.rb +18 -0
  24. data/lib/mongo_mapper/extensions/time.rb +29 -0
  25. data/lib/mongo_mapper/plugins/accessible.rb +44 -0
  26. data/lib/mongo_mapper/plugins/associations/base.rb +7 -6
  27. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +5 -6
  28. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +5 -6
  29. data/lib/mongo_mapper/plugins/associations/collection.rb +1 -0
  30. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +2 -1
  31. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +25 -39
  32. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +4 -4
  33. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +36 -46
  34. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +1 -0
  35. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +5 -4
  36. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +1 -0
  37. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +40 -0
  38. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +7 -7
  39. data/lib/mongo_mapper/plugins/associations/proxy.rb +16 -8
  40. data/lib/mongo_mapper/plugins/associations.rb +14 -22
  41. data/lib/mongo_mapper/plugins/caching.rb +21 -0
  42. data/lib/mongo_mapper/plugins/callbacks.rb +17 -5
  43. data/lib/mongo_mapper/plugins/clone.rb +10 -4
  44. data/lib/mongo_mapper/plugins/descendants.rb +3 -2
  45. data/lib/mongo_mapper/plugins/dirty.rb +1 -0
  46. data/lib/mongo_mapper/plugins/document.rb +41 -0
  47. data/lib/mongo_mapper/{support/find.rb → plugins/dynamic_querying/dynamic_finder.rb} +3 -36
  48. data/lib/mongo_mapper/plugins/dynamic_querying.rb +43 -0
  49. data/lib/mongo_mapper/plugins/embedded_document.rb +49 -0
  50. data/lib/mongo_mapper/plugins/equality.rb +4 -10
  51. data/lib/mongo_mapper/plugins/identity_map.rb +29 -23
  52. data/lib/mongo_mapper/plugins/indexes.rb +12 -0
  53. data/lib/mongo_mapper/plugins/inspect.rb +1 -0
  54. data/lib/mongo_mapper/plugins/keys/key.rb +55 -0
  55. data/lib/mongo_mapper/plugins/keys.rb +85 -110
  56. data/lib/mongo_mapper/plugins/logger.rb +1 -0
  57. data/lib/mongo_mapper/plugins/modifiers.rb +41 -16
  58. data/lib/mongo_mapper/plugins/pagination.rb +5 -15
  59. data/lib/mongo_mapper/plugins/persistence.rb +69 -0
  60. data/lib/mongo_mapper/plugins/protected.rb +9 -1
  61. data/lib/mongo_mapper/plugins/querying/decorator.rb +46 -0
  62. data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +15 -0
  63. data/lib/mongo_mapper/plugins/querying.rb +176 -0
  64. data/lib/mongo_mapper/plugins/rails.rb +6 -1
  65. data/lib/mongo_mapper/plugins/safe.rb +28 -0
  66. data/lib/mongo_mapper/plugins/sci.rb +32 -0
  67. data/lib/mongo_mapper/plugins/scopes.rb +21 -0
  68. data/lib/mongo_mapper/plugins/serialization.rb +5 -4
  69. data/lib/mongo_mapper/plugins/timestamps.rb +2 -1
  70. data/lib/mongo_mapper/plugins/userstamps.rb +1 -0
  71. data/lib/mongo_mapper/plugins/validations.rb +9 -5
  72. data/lib/mongo_mapper/plugins.rb +1 -20
  73. data/lib/mongo_mapper/support/descendant_appends.rb +5 -6
  74. data/lib/mongo_mapper/version.rb +4 -0
  75. data/lib/mongo_mapper.rb +71 -128
  76. data/test/{NOTE_ON_TESTING → _NOTE_ON_TESTING} +0 -0
  77. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +5 -5
  78. data/test/functional/associations/test_belongs_to_proxy.rb +13 -21
  79. data/test/functional/associations/test_in_array_proxy.rb +7 -9
  80. data/test/functional/associations/test_many_documents_as_proxy.rb +5 -5
  81. data/test/functional/associations/test_many_documents_proxy.rb +186 -64
  82. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +22 -22
  83. data/test/functional/associations/test_many_embedded_proxy.rb +32 -32
  84. data/test/functional/associations/test_many_polymorphic_proxy.rb +47 -47
  85. data/test/functional/associations/test_one_embedded_proxy.rb +67 -0
  86. data/test/functional/associations/test_one_proxy.rb +70 -49
  87. data/test/functional/test_accessible.rb +168 -0
  88. data/test/functional/test_associations.rb +11 -11
  89. data/test/functional/test_binary.rb +5 -5
  90. data/test/functional/test_caching.rb +76 -0
  91. data/test/functional/test_callbacks.rb +104 -34
  92. data/test/functional/test_dirty.rb +16 -16
  93. data/test/functional/test_document.rb +12 -924
  94. data/test/functional/test_dynamic_querying.rb +75 -0
  95. data/test/functional/test_embedded_document.rb +88 -8
  96. data/test/functional/test_identity_map.rb +41 -43
  97. data/test/functional/{test_indexing.rb → test_indexes.rb} +3 -5
  98. data/test/functional/test_logger.rb +1 -1
  99. data/test/functional/test_modifiers.rb +275 -181
  100. data/test/functional/test_pagination.rb +13 -15
  101. data/test/functional/test_protected.rb +25 -11
  102. data/test/functional/test_querying.rb +873 -0
  103. data/test/functional/test_safe.rb +76 -0
  104. data/test/functional/test_sci.rb +230 -0
  105. data/test/functional/test_scopes.rb +171 -0
  106. data/test/functional/test_string_id_compatibility.rb +11 -11
  107. data/test/functional/test_timestamps.rb +0 -2
  108. data/test/functional/test_userstamps.rb +0 -1
  109. data/test/functional/test_validations.rb +44 -31
  110. data/test/models.rb +18 -17
  111. data/test/{active_model_lint_test.rb → test_active_model_lint.rb} +3 -1
  112. data/test/test_helper.rb +59 -16
  113. data/test/unit/associations/test_base.rb +47 -42
  114. data/test/unit/associations/test_proxy.rb +15 -15
  115. data/test/unit/serializers/test_json_serializer.rb +29 -29
  116. data/test/unit/test_clone.rb +69 -0
  117. data/test/unit/test_descendant_appends.rb +3 -3
  118. data/test/unit/test_document.rb +49 -67
  119. data/test/unit/test_dynamic_finder.rb +53 -51
  120. data/test/unit/test_embedded_document.rb +19 -38
  121. data/test/unit/{test_support.rb → test_extensions.rb} +136 -122
  122. data/test/unit/test_key.rb +185 -0
  123. data/test/unit/test_keys.rb +29 -147
  124. data/test/unit/test_mongo_mapper.rb +3 -48
  125. data/test/unit/test_pagination.rb +1 -150
  126. data/test/unit/test_rails.rb +77 -19
  127. data/test/unit/test_rails_compatibility.rb +12 -12
  128. data/test/unit/test_serialization.rb +5 -5
  129. data/test/unit/test_time_zones.rb +9 -9
  130. data/test/unit/test_validations.rb +46 -46
  131. metadata +157 -155
  132. data/.gitignore +0 -10
  133. data/Rakefile +0 -55
  134. data/VERSION +0 -1
  135. data/lib/mongo_mapper/plugins/pagination/proxy.rb +0 -72
  136. data/lib/mongo_mapper/query.rb +0 -130
  137. data/lib/mongo_mapper/support.rb +0 -215
  138. data/mongo_mapper.gemspec +0 -196
  139. data/performance/read_write.rb +0 -52
  140. data/specs.watchr +0 -51
  141. data/test/support/custom_matchers.rb +0 -55
  142. data/test/support/timing.rb +0 -16
  143. data/test/unit/test_query.rb +0 -340
@@ -5,13 +5,49 @@ class ManyDocumentsProxyTest < Test::Unit::TestCase
5
5
  def setup
6
6
  Project.collection.remove
7
7
  Status.collection.remove
8
+
9
+ @pet_class = Doc do
10
+ key :name, String
11
+ key :owner_id, ObjectId
12
+ end
13
+
14
+ @owner_class = Doc do
15
+ key :name, String
16
+ end
17
+ @owner_class.many :pets, :class => @pet_class, :foreign_key => :owner_id, :order => 'name'
8
18
  end
9
-
19
+
10
20
  should "default reader to empty array" do
11
21
  project = Project.new
12
22
  project.statuses.should == []
13
23
  end
14
24
 
25
+ should "allow assignment of many associated documents using a hash" do
26
+ person_attributes = {
27
+ 'name' => 'Mr. Pet Lover',
28
+ 'pets' => [
29
+ {'name' => 'Jimmy', 'species' => 'Cocker Spainel'},
30
+ {'name' => 'Sasha', 'species' => 'Siberian Husky'},
31
+ ]
32
+ }
33
+
34
+ owner = @owner_class.new(person_attributes)
35
+ owner.name.should == 'Mr. Pet Lover'
36
+ owner.pets[0].name.should == 'Jimmy'
37
+ owner.pets[0].species.should == 'Cocker Spainel'
38
+ owner.pets[1].name.should == 'Sasha'
39
+ owner.pets[1].species.should == 'Siberian Husky'
40
+
41
+ owner.save.should be_true
42
+ owner.reload
43
+
44
+ owner.name.should == 'Mr. Pet Lover'
45
+ owner.pets[0].name.should == 'Jimmy'
46
+ owner.pets[0].species.should == 'Cocker Spainel'
47
+ owner.pets[1].name.should == 'Sasha'
48
+ owner.pets[1].species.should == 'Siberian Husky'
49
+ end
50
+
15
51
  should "allow adding to association like it was an array" do
16
52
  project = Project.new
17
53
  project.statuses << Status.new(:name => 'Foo1!')
@@ -20,28 +56,62 @@ class ManyDocumentsProxyTest < Test::Unit::TestCase
20
56
  project.statuses.size.should == 3
21
57
  end
22
58
 
23
- should "be able to replace the association" do
24
- project = Project.new
25
- project.statuses = [Status.new(:name => "ready")]
26
- project.save.should be_true
59
+ context "replacing the association" do
60
+ context "with objects of the class" do
61
+ should "work" do
62
+ project = Project.new
63
+ project.statuses = [Status.new(:name => "ready")]
64
+ project.save.should be_true
27
65
 
28
- project.reload
29
- project.statuses.size.should == 1
30
- project.statuses[0].name.should == "ready"
66
+ project.reload
67
+ project.statuses.size.should == 1
68
+ project.statuses[0].name.should == "ready"
69
+ end
70
+ end
71
+
72
+ context "with Hashes" do
73
+ should "convert to objects of the class and work" do
74
+ project = Project.new
75
+ project.statuses = [{ 'name' => 'ready' }]
76
+ project.save.should be_true
77
+
78
+ project.reload
79
+ project.statuses.size.should == 1
80
+ project.statuses[0].name.should == "ready"
81
+ end
82
+ end
31
83
  end
32
-
33
- should "correctly assign foreign key when using <<, push and concat" do
34
- project = Project.new
35
- project.statuses << Status.new(:name => '<<')
36
- project.statuses.push Status.new(:name => 'push')
37
- project.statuses.concat Status.new(:name => 'concat')
38
-
39
- project.reload
40
- project.statuses[0].project_id.should == project.id
41
- project.statuses[1].project_id.should == project.id
42
- project.statuses[2].project_id.should == project.id
84
+
85
+ context "using <<, push and concat" do
86
+ context "with objects of the class" do
87
+ should "correctly assign foreign key" do
88
+ project = Project.new
89
+ project.statuses << Status.new(:name => '<<')
90
+ project.statuses.push Status.new(:name => 'push')
91
+ project.statuses.concat Status.new(:name => 'concat')
92
+
93
+ project.reload
94
+ project.statuses[0].project_id.should == project.id
95
+ project.statuses[1].project_id.should == project.id
96
+ project.statuses[2].project_id.should == project.id
97
+ end
98
+ end
99
+
100
+ context "with Hashes" do
101
+ should "correctly convert to objects and assign foreign key" do
102
+ project = Project.new
103
+ project.statuses << { 'name' => '<<' }
104
+ project.statuses.push( { 'name' => 'push' })
105
+ project.statuses.concat({ 'name' => 'concat' })
106
+
107
+ project.reload
108
+ project.statuses[0].project_id.should == project.id
109
+ project.statuses[1].project_id.should == project.id
110
+ project.statuses[2].project_id.should == project.id
111
+ end
112
+ end
43
113
  end
44
-
114
+
45
115
  context "build" do
46
116
  should "assign foreign key" do
47
117
  project = Project.create
@@ -62,22 +132,35 @@ class ManyDocumentsProxyTest < Test::Unit::TestCase
62
132
  status.save!
63
133
  project.statuses.size.should == 1
64
134
  end
135
+
136
+ should "update collection without save" do
137
+ project = Project.create
138
+ project.statuses.build(:name => 'Foo')
139
+ project.statuses.size.should == 1
140
+ end
141
+
142
+ should "save built document when saving parent" do
143
+ project = Project.create
144
+ status = project.statuses.build(:name => 'Foo')
145
+ project.save!
146
+ status.should_not be_new
147
+ end
65
148
  end
66
-
149
+
67
150
  context "create" do
68
151
  should "assign foreign key" do
69
152
  project = Project.create
70
153
  status = project.statuses.create(:name => 'Foo!')
71
154
  status.project_id.should == project.id
72
155
  end
73
-
156
+
74
157
  should "save record" do
75
158
  project = Project.create
76
159
  lambda {
77
160
  project.statuses.create(:name => 'Foo!')
78
161
  }.should change { Status.count }
79
162
  end
80
-
163
+
81
164
  should "allow passing attributes" do
82
165
  project = Project.create
83
166
  status = project.statuses.create(:name => 'Foo!')
@@ -91,27 +174,27 @@ class ManyDocumentsProxyTest < Test::Unit::TestCase
91
174
  project.statuses.size.should == 1
92
175
  end
93
176
  end
94
-
177
+
95
178
  context "create!" do
96
179
  should "assign foreign key" do
97
180
  project = Project.create
98
181
  status = project.statuses.create!(:name => 'Foo!')
99
182
  status.project_id.should == project.id
100
183
  end
101
-
184
+
102
185
  should "save record" do
103
186
  project = Project.create
104
187
  lambda {
105
188
  project.statuses.create!(:name => 'Foo!')
106
189
  }.should change { Status.count }
107
190
  end
108
-
191
+
109
192
  should "allow passing attributes" do
110
193
  project = Project.create
111
194
  status = project.statuses.create!(:name => 'Foo!')
112
195
  status.name.should == 'Foo!'
113
196
  end
114
-
197
+
115
198
  should "raise exception if not valid" do
116
199
  project = Project.create
117
200
  lambda {
@@ -126,29 +209,29 @@ class ManyDocumentsProxyTest < Test::Unit::TestCase
126
209
  project.statuses.size.should == 1
127
210
  end
128
211
  end
129
-
212
+
130
213
  context "count" do
131
214
  should "work scoped to association" do
132
215
  project = Project.create
133
216
  3.times { project.statuses.create(:name => 'Foo!') }
134
-
217
+
135
218
  other_project = Project.create
136
219
  2.times { other_project.statuses.create(:name => 'Foo!') }
137
-
220
+
138
221
  project.statuses.count.should == 3
139
222
  other_project.statuses.count.should == 2
140
223
  end
141
-
224
+
142
225
  should "work with conditions" do
143
226
  project = Project.create
144
227
  project.statuses.create(:name => 'Foo')
145
228
  project.statuses.create(:name => 'Other 1')
146
229
  project.statuses.create(:name => 'Other 2')
147
-
230
+
148
231
  project.statuses.count(:name => 'Foo').should == 1
149
232
  end
150
233
  end
151
-
234
+
152
235
  context "Unassociating documents" do
153
236
  setup do
154
237
  @project = Project.create
@@ -168,7 +251,7 @@ class ManyDocumentsProxyTest < Test::Unit::TestCase
168
251
  @project2.statuses.count.should == 2
169
252
  Status.count.should == 2
170
253
  end
171
-
254
+
172
255
  should "work with destroy all and conditions" do
173
256
  @project.statuses.count.should == 2
174
257
  @project.statuses.destroy_all(:name => '1')
@@ -177,37 +260,37 @@ class ManyDocumentsProxyTest < Test::Unit::TestCase
177
260
  @project2.statuses.count.should == 2
178
261
  Status.count.should == 3
179
262
  end
180
-
263
+
181
264
  should "work with delete all" do
182
265
  @project.statuses.count.should == 2
183
266
  @project.statuses.delete_all
184
267
  @project.statuses.count.should == 0
185
-
268
+
186
269
  @project2.statuses.count.should == 2
187
270
  Status.count.should == 2
188
271
  end
189
-
272
+
190
273
  should "work with delete all and conditions" do
191
274
  @project.statuses.count.should == 2
192
275
  @project.statuses.delete_all(:name => '1')
193
276
  @project.statuses.count.should == 1
194
-
277
+
195
278
  @project2.statuses.count.should == 2
196
279
  Status.count.should == 3
197
280
  end
198
-
281
+
199
282
  should "work with nullify" do
200
283
  @project.statuses.count.should == 2
201
284
  @project.statuses.nullify
202
285
  @project.statuses.count.should == 0
203
-
286
+
204
287
  @project2.statuses.count.should == 2
205
288
  Status.count.should == 4
206
289
  Status.count(:name => '1').should == 2
207
290
  Status.count(:name => '2').should == 2
208
291
  end
209
- end
210
-
292
+ end
293
+
211
294
  context "Finding scoped to association" do
212
295
  setup do
213
296
  @project1 = Project.new(:name => 'Project 1')
@@ -215,7 +298,7 @@ class ManyDocumentsProxyTest < Test::Unit::TestCase
215
298
  @complete = Status.create(:name => 'Complete', :position => 2)
216
299
  @project1.statuses = [@brand_new, @complete]
217
300
  @project1.save
218
-
301
+
219
302
  @project2 = Project.create(:name => 'Project 2')
220
303
  @in_progress = Status.create(:name => 'In Progress')
221
304
  @archived = Status.create(:name => 'Archived')
@@ -223,7 +306,7 @@ class ManyDocumentsProxyTest < Test::Unit::TestCase
223
306
  @project2.statuses = [@in_progress, @archived, @another_complete]
224
307
  @project2.save
225
308
  end
226
-
309
+
227
310
  context "dynamic finders" do
228
311
  should "work with single key" do
229
312
  @project1.statuses.find_by_name('New').should == @brand_new
@@ -231,19 +314,19 @@ class ManyDocumentsProxyTest < Test::Unit::TestCase
231
314
  @project2.statuses.find_by_name('In Progress').should == @in_progress
232
315
  @project2.statuses.find_by_name!('In Progress').should == @in_progress
233
316
  end
234
-
317
+
235
318
  should "work with multiple keys" do
236
319
  @project1.statuses.find_by_name_and_position('New', 1).should == @brand_new
237
320
  @project1.statuses.find_by_name_and_position!('New', 1).should == @brand_new
238
321
  @project1.statuses.find_by_name_and_position('New', 2).should be_nil
239
322
  end
240
-
323
+
241
324
  should "raise error when using !" do
242
325
  lambda {
243
326
  @project1.statuses.find_by_name!('Fake')
244
327
  }.should raise_error(MongoMapper::DocumentNotFound)
245
328
  end
246
-
329
+
247
330
  context "find_or_create_by" do
248
331
  should "not create document if found" do
249
332
  lambda {
@@ -261,75 +344,114 @@ class ManyDocumentsProxyTest < Test::Unit::TestCase
261
344
  end
262
345
  end
263
346
  end
264
-
347
+
348
+ context "sexy querying" do
349
+ should "work with where" do
350
+ @project1.statuses.where(:name => 'New').all.should == [@brand_new]
351
+ end
352
+
353
+ should "work with sort" do
354
+ @project1.statuses.sort(:name).all.should == [@complete, @brand_new]
355
+ end
356
+
357
+ should "work with limit" do
358
+ @project1.statuses.sort(:name).limit(1).all.should == [@complete]
359
+ end
360
+
361
+ should "work with skip" do
362
+ @project1.statuses.sort(:name).skip(1).all.should == [@brand_new]
363
+ end
364
+
365
+ should "work with fields" do
366
+ @project1.statuses.fields(:position).all.each do |status|
367
+ status.position.should_not be_nil
368
+ status.name.should be_nil
369
+ end
370
+ end
371
+
372
+ should "work with scopes" do
373
+ @project1.statuses.complete.all.should == [@complete]
374
+ end
375
+
376
+ should "work with methods on class that return query" do
377
+ @project1.statuses.by_position(1).first.should == @brand_new
378
+ end
379
+
380
+ should "not work with methods on class that do not return query" do
381
+ Status.class_eval { def self.foo; 'foo' end }
382
+ lambda { @project1.statuses.foo }.
383
+ should raise_error(NoMethodError)
384
+ end
385
+ end
386
+
265
387
  context "all" do
266
388
  should "work" do
267
389
  @project1.statuses.all(:order => "position asc").should == [@brand_new, @complete]
268
390
  end
269
-
391
+
270
392
  should "work with conditions" do
271
393
  @project1.statuses.all(:name => 'Complete').should == [@complete]
272
394
  end
273
395
  end
274
-
396
+
275
397
  context "first" do
276
398
  should "work" do
277
399
  @project1.statuses.first(:order => 'name').should == @complete
278
400
  end
279
-
401
+
280
402
  should "work with conditions" do
281
403
  @project1.statuses.first(:name => 'Complete').should == @complete
282
404
  end
283
405
  end
284
-
406
+
285
407
  context "last" do
286
408
  should "work" do
287
409
  @project1.statuses.last(:order => "position asc").should == @complete
288
410
  end
289
-
411
+
290
412
  should "work with conditions" do
291
413
  @project1.statuses.last(:order => 'position', :name => 'New').should == @brand_new
292
414
  end
293
415
  end
294
-
416
+
295
417
  context "with one id" do
296
418
  should "work for id in association" do
297
419
  @project1.statuses.find(@complete.id).should == @complete
298
420
  end
299
-
421
+
300
422
  should "not work for id not in association" do
301
423
  lambda {
302
424
  @project1.statuses.find!(@archived.id)
303
425
  }.should raise_error(MongoMapper::DocumentNotFound)
304
426
  end
305
427
  end
306
-
428
+
307
429
  context "with multiple ids" do
308
430
  should "work for ids in association" do
309
431
  statuses = @project1.statuses.find(@brand_new.id, @complete.id)
310
432
  statuses.should == [@brand_new, @complete]
311
433
  end
312
-
434
+
313
435
  should "not work for ids not in association" do
314
436
  assert_raises(MongoMapper::DocumentNotFound) do
315
437
  @project1.statuses.find!(@brand_new.id, @complete.id, @archived.id)
316
438
  end
317
439
  end
318
440
  end
319
-
441
+
320
442
  context "with #paginate" do
321
443
  setup do
322
444
  @statuses = @project2.statuses.paginate(:per_page => 2, :page => 1, :order => 'name asc')
323
445
  end
324
-
446
+
325
447
  should "return total pages" do
326
448
  @statuses.total_pages.should == 2
327
449
  end
328
-
450
+
329
451
  should "return total entries" do
330
452
  @statuses.total_entries.should == 3
331
453
  end
332
-
454
+
333
455
  should "return the subject" do
334
456
  @statuses.collect(&:name).should == %w(Archived Complete)
335
457
  end
@@ -344,13 +466,13 @@ class ManyDocumentsProxyTest < Test::Unit::TestCase
344
466
  status3 = Status.new(:name => "Closed")
345
467
  project.statuses = [status1, status2, status3]
346
468
  project.save
347
-
469
+
348
470
  open_statuses = project.statuses.open
349
471
  open_statuses.should include(status1)
350
472
  open_statuses.should include(status2)
351
473
  open_statuses.should_not include(status3)
352
474
  end
353
-
475
+
354
476
  should "work using many's :extend option" do
355
477
  project = Project.new(:name => "Some Project")
356
478
  collaborator1 = Collaborator.new(:name => "zing")
@@ -360,7 +482,7 @@ class ManyDocumentsProxyTest < Test::Unit::TestCase
360
482
  project.collaborators.top.should == collaborator1
361
483
  end
362
484
  end
363
-
485
+
364
486
  context ":dependent" do
365
487
  setup do
366
488
  # FIXME: make use of already defined models
@@ -6,7 +6,7 @@ class ManyEmbeddedPolymorphicProxyTest < Test::Unit::TestCase
6
6
  Catalog.collection.remove
7
7
  TrModels::Fleet.collection.remove
8
8
  end
9
-
9
+
10
10
  should "default reader to empty array" do
11
11
  catalog = Catalog.new
12
12
  catalog.medias.should == []
@@ -29,12 +29,12 @@ class ManyEmbeddedPolymorphicProxyTest < Test::Unit::TestCase
29
29
  catalog.medias[0].file.should == 'video.mpg'
30
30
  catalog.medias[0].new?.should == false
31
31
  end
32
-
32
+
33
33
  context "count" do
34
34
  should "default to 0" do
35
35
  Catalog.new.medias.count.should == 0
36
36
  end
37
-
37
+
38
38
  should 'return correct count if any are embedded' do
39
39
  catalog = Catalog.new
40
40
  catalog.medias = [
@@ -48,8 +48,8 @@ class ManyEmbeddedPolymorphicProxyTest < Test::Unit::TestCase
48
48
  catalog.medias.count.should == 3
49
49
  end
50
50
  end
51
-
52
- should "store different associations" do
51
+
52
+ should "store different associations" do
53
53
  catalog = Catalog.new
54
54
  catalog.medias = [
55
55
  Video.new('file' => 'video.mpg', 'length' => 3600),
@@ -57,7 +57,7 @@ class ManyEmbeddedPolymorphicProxyTest < Test::Unit::TestCase
57
57
  Image.new('file' => 'image.png', 'width' => 800, 'height' => 600)
58
58
  ]
59
59
  catalog.save.should be_true
60
-
60
+
61
61
  catalog = catalog.reload
62
62
  catalog.medias.size.should == 3
63
63
  catalog.medias[0].file.should == 'video.mpg'
@@ -68,18 +68,18 @@ class ManyEmbeddedPolymorphicProxyTest < Test::Unit::TestCase
68
68
  catalog.medias[2].width.should == 800
69
69
  catalog.medias[2].height.should == 600
70
70
  end
71
-
71
+
72
72
  context "With modularized models" do
73
73
  should "set associations correctly" do
74
- fleet_attributes = {
75
- 'name' => 'My Fleet',
74
+ fleet_attributes = {
75
+ 'name' => 'My Fleet',
76
76
  'transports' => [
77
77
  {'_type' => 'TrModels::Ambulance', 'license_plate' => 'GGG123', 'icu' => true},
78
- {'_type' => 'TrModels::Car', 'license_plate' => 'ABC123', 'model' => 'VW Golf', 'year' => 2001},
78
+ {'_type' => 'TrModels::Car', 'license_plate' => 'ABC123', 'model' => 'VW Golf', 'year' => 2001},
79
79
  {'_type' => 'TrModels::Car', 'license_plate' => 'DEF123', 'model' => 'Honda Accord', 'year' => 2008},
80
- ]
80
+ ]
81
81
  }
82
-
82
+
83
83
  fleet = TrModels::Fleet.new(fleet_attributes)
84
84
  fleet.transports.size.should == 3
85
85
  fleet.transports[0].class.should == TrModels::Ambulance
@@ -92,9 +92,9 @@ class ManyEmbeddedPolymorphicProxyTest < Test::Unit::TestCase
92
92
  fleet.transports[2].class.should == TrModels::Car
93
93
  fleet.transports[2].license_plate.should == 'DEF123'
94
94
  fleet.transports[2].model.should == 'Honda Accord'
95
- fleet.transports[2].year.should == 2008
95
+ fleet.transports[2].year.should == 2008
96
96
  fleet.save.should be_true
97
-
97
+
98
98
  fleet = fleet.reload
99
99
  fleet.transports.size.should == 3
100
100
  fleet.transports[0].license_plate.should == 'GGG123'
@@ -104,31 +104,31 @@ class ManyEmbeddedPolymorphicProxyTest < Test::Unit::TestCase
104
104
  fleet.transports[1].year.should == 2001
105
105
  fleet.transports[2].license_plate.should == 'DEF123'
106
106
  fleet.transports[2].model.should == 'Honda Accord'
107
- fleet.transports[2].year.should == 2008
107
+ fleet.transports[2].year.should == 2008
108
108
  end
109
-
109
+
110
110
  should "default reader to empty array" do
111
111
  fleet = TrModels::Fleet.new
112
112
  fleet.transports.should == []
113
113
  end
114
-
114
+
115
115
  should "allow adding to association like it was an array" do
116
116
  fleet = TrModels::Fleet.new
117
117
  fleet.transports << TrModels::Car.new
118
118
  fleet.transports.push TrModels::Bus.new
119
119
  fleet.transports.size.should == 2
120
120
  end
121
-
121
+
122
122
  should "be able to replace the association" do
123
123
  fleet = TrModels::Fleet.new
124
124
  fleet.transports = [TrModels::Car.new('license_plate' => 'DCU2013', 'model' => 'Honda Civic')]
125
125
  fleet.save.should be_true
126
-
126
+
127
127
  fleet = fleet.reload
128
128
  fleet.transports.size.should == 1
129
129
  fleet.transports[0].license_plate.should == 'DCU2013'
130
130
  end
131
-
131
+
132
132
  should "store different associations" do
133
133
  fleet = TrModels::Fleet.new
134
134
  fleet.transports = [
@@ -137,7 +137,7 @@ class ManyEmbeddedPolymorphicProxyTest < Test::Unit::TestCase
137
137
  TrModels::Ambulance.new('license_plate' => 'HDD3030', 'icu' => true)
138
138
  ]
139
139
  fleet.save.should be_true
140
-
140
+
141
141
  fleet = fleet.reload
142
142
  fleet.transports.size.should == 3
143
143
  fleet.transports[0].license_plate.should == 'ABC1223'
@@ -161,7 +161,7 @@ class ManyEmbeddedPolymorphicProxyTest < Test::Unit::TestCase
161
161
  catalog.save
162
162
  catalog.medias.visible.should == [medias[0], medias[1]]
163
163
  end
164
-
164
+
165
165
  should "work using many's :extend option" do
166
166
  fleet = TrModels::Fleet.new
167
167
  transports = fleet.transports = [