mongo_mapper-rails3 0.7.0.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 (94) hide show
  1. data/.gitignore +10 -0
  2. data/Gemfile +15 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +60 -0
  5. data/Rakefile +58 -0
  6. data/VERSION +1 -0
  7. data/bin/mmconsole +60 -0
  8. data/lib/mongo_mapper.rb +131 -0
  9. data/lib/mongo_mapper/document.rb +439 -0
  10. data/lib/mongo_mapper/embedded_document.rb +68 -0
  11. data/lib/mongo_mapper/plugins.rb +30 -0
  12. data/lib/mongo_mapper/plugins/associations.rb +106 -0
  13. data/lib/mongo_mapper/plugins/associations/base.rb +123 -0
  14. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +30 -0
  15. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +25 -0
  16. data/lib/mongo_mapper/plugins/associations/collection.rb +21 -0
  17. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +50 -0
  18. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +141 -0
  19. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  20. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +120 -0
  21. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +31 -0
  22. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +23 -0
  23. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +13 -0
  24. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +68 -0
  25. data/lib/mongo_mapper/plugins/associations/proxy.rb +119 -0
  26. data/lib/mongo_mapper/plugins/callbacks.rb +87 -0
  27. data/lib/mongo_mapper/plugins/clone.rb +14 -0
  28. data/lib/mongo_mapper/plugins/descendants.rb +17 -0
  29. data/lib/mongo_mapper/plugins/dirty.rb +120 -0
  30. data/lib/mongo_mapper/plugins/equality.rb +24 -0
  31. data/lib/mongo_mapper/plugins/identity_map.rb +124 -0
  32. data/lib/mongo_mapper/plugins/inspect.rb +15 -0
  33. data/lib/mongo_mapper/plugins/keys.rb +310 -0
  34. data/lib/mongo_mapper/plugins/logger.rb +19 -0
  35. data/lib/mongo_mapper/plugins/pagination.rb +26 -0
  36. data/lib/mongo_mapper/plugins/pagination/proxy.rb +72 -0
  37. data/lib/mongo_mapper/plugins/protected.rb +46 -0
  38. data/lib/mongo_mapper/plugins/rails.rb +46 -0
  39. data/lib/mongo_mapper/plugins/serialization.rb +50 -0
  40. data/lib/mongo_mapper/plugins/validations.rb +88 -0
  41. data/lib/mongo_mapper/query.rb +130 -0
  42. data/lib/mongo_mapper/support.rb +217 -0
  43. data/lib/mongo_mapper/support/descendant_appends.rb +46 -0
  44. data/lib/mongo_mapper/support/find.rb +77 -0
  45. data/mongo_mapper-rails3.gemspec +208 -0
  46. data/performance/read_write.rb +52 -0
  47. data/specs.watchr +51 -0
  48. data/test/NOTE_ON_TESTING +1 -0
  49. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
  50. data/test/functional/associations/test_belongs_to_proxy.rb +101 -0
  51. data/test/functional/associations/test_in_array_proxy.rb +321 -0
  52. data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
  53. data/test/functional/associations/test_many_documents_proxy.rb +453 -0
  54. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
  55. data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
  56. data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
  57. data/test/functional/associations/test_one_proxy.rb +161 -0
  58. data/test/functional/test_associations.rb +44 -0
  59. data/test/functional/test_binary.rb +27 -0
  60. data/test/functional/test_callbacks.rb +81 -0
  61. data/test/functional/test_dirty.rb +163 -0
  62. data/test/functional/test_document.rb +1244 -0
  63. data/test/functional/test_embedded_document.rb +125 -0
  64. data/test/functional/test_identity_map.rb +508 -0
  65. data/test/functional/test_logger.rb +20 -0
  66. data/test/functional/test_modifiers.rb +252 -0
  67. data/test/functional/test_pagination.rb +93 -0
  68. data/test/functional/test_protected.rb +161 -0
  69. data/test/functional/test_string_id_compatibility.rb +67 -0
  70. data/test/functional/test_validations.rb +329 -0
  71. data/test/models.rb +232 -0
  72. data/test/support/custom_matchers.rb +55 -0
  73. data/test/support/timing.rb +16 -0
  74. data/test/test_helper.rb +59 -0
  75. data/test/unit/associations/test_base.rb +207 -0
  76. data/test/unit/associations/test_proxy.rb +105 -0
  77. data/test/unit/serializers/test_json_serializer.rb +189 -0
  78. data/test/unit/test_descendant_appends.rb +71 -0
  79. data/test/unit/test_document.rb +231 -0
  80. data/test/unit/test_dynamic_finder.rb +123 -0
  81. data/test/unit/test_embedded_document.rb +663 -0
  82. data/test/unit/test_keys.rb +169 -0
  83. data/test/unit/test_lint.rb +8 -0
  84. data/test/unit/test_mongo_mapper.rb +125 -0
  85. data/test/unit/test_pagination.rb +160 -0
  86. data/test/unit/test_plugins.rb +51 -0
  87. data/test/unit/test_query.rb +334 -0
  88. data/test/unit/test_rails.rb +123 -0
  89. data/test/unit/test_rails_compatibility.rb +57 -0
  90. data/test/unit/test_serialization.rb +51 -0
  91. data/test/unit/test_support.rb +362 -0
  92. data/test/unit/test_time_zones.rb +39 -0
  93. data/test/unit/test_validations.rb +557 -0
  94. metadata +344 -0
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+
3
+ class TimeZonesTest < Test::Unit::TestCase
4
+ context "An instance of an embedded document" do
5
+ setup do
6
+ @document = EDoc do
7
+ key :name, String
8
+ key :created_at, Time
9
+ end
10
+ end
11
+
12
+ should "work without Time.zone" do
13
+ Time.zone = nil
14
+
15
+ doc = @document.new(:created_at => "2009-08-15 14:00:00")
16
+ doc.created_at.should == Time.local(2009, 8, 15, 14, 0, 0).utc
17
+ end
18
+
19
+ should "work with Time.zone set to the (default) UTC" do
20
+ Time.zone = 'UTC'
21
+
22
+ doc = @document.new(:created_at => "2009-08-15 14:00:00")
23
+ doc.created_at.is_a?(ActiveSupport::TimeWithZone).should be_true
24
+ doc.created_at.should == Time.utc(2009, 8, 15, 14)
25
+
26
+ Time.zone = nil
27
+ end
28
+
29
+ should "work with timezones that are not UTC" do
30
+ Time.zone = 'Hawaii'
31
+
32
+ doc = @document.new(:created_at => "2009-08-15 14:00:00")
33
+ doc.created_at.is_a?(ActiveSupport::TimeWithZone).should be_true
34
+ doc.created_at.should == Time.utc(2009, 8, 16)
35
+
36
+ Time.zone = nil
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,557 @@
1
+ require 'test_helper'
2
+
3
+ module MongoMapper::EmbeddedDocument
4
+ include MongoMapper::Document::InstanceMethods
5
+ end
6
+
7
+ class ValidationsTest < Test::Unit::TestCase
8
+ context "Validations" do
9
+ context "on a Document" do
10
+ setup do
11
+ @document = Doc()
12
+ end
13
+
14
+ context "Validating acceptance of" do
15
+ should "work with validates_acceptance_of macro" do
16
+ @document.key :terms, String
17
+ @document.validates_acceptance_of :terms
18
+ doc = @document.new(:terms => '')
19
+ doc.should have_error_on(:terms)
20
+ doc.terms = '1'
21
+ doc.should_not have_error_on(:terms)
22
+ end
23
+ end
24
+
25
+ context "validating confirmation of" do
26
+ should "work with validates_confirmation_of macro" do
27
+ @document.key :password, String
28
+ @document.validates_confirmation_of :password
29
+ doc = @document.new
30
+ doc.password = 'foobar'
31
+ doc.password_confirmation = 'gfg'
32
+ doc.should have_error_on(:password)
33
+ doc.password_confirmation = 'foobar'
34
+ doc.should_not have_error_on(:password)
35
+ end
36
+ end
37
+
38
+ context "validating format of" do
39
+ should "work with validates_format_of macro" do
40
+ @document.key :name, String
41
+ @document.validates_format_of :name, :with => /.+/
42
+ doc = @document.new
43
+ doc.should have_error_on(:name)
44
+ doc.name = 'John'
45
+ doc.should_not have_error_on(:name)
46
+ end
47
+
48
+ should "work with :format shorcut key" do
49
+ @document.key :name, String, :format => /.+/
50
+ doc = @document.new
51
+ doc.should have_error_on(:name)
52
+ doc.name = 'John'
53
+ doc.should_not have_error_on(:name)
54
+ end
55
+ end
56
+
57
+ context "validating length of" do
58
+ should "work with validates_length_of macro" do
59
+ @document.key :name, String
60
+ @document.validates_length_of :name, :minimum => 5
61
+ doc = @document.new
62
+ doc.should have_error_on(:name)
63
+ end
64
+
65
+ context "with :length => integer shortcut" do
66
+ should "set maximum of integer provided" do
67
+ @document.key :name, String, :length => 5
68
+ doc = @document.new
69
+ doc.name = '123456'
70
+ doc.should have_error_on(:name)
71
+ doc.name = '12345'
72
+ doc.should_not have_error_on(:name)
73
+ end
74
+ end
75
+
76
+ context "with :length => range shortcut" do
77
+ setup do
78
+ @document.key :name, String, :length => 5..7
79
+ end
80
+
81
+ should "set minimum of range min" do
82
+ doc = @document.new
83
+ doc.should have_error_on(:name)
84
+ doc.name = '123456'
85
+ doc.should_not have_error_on(:name)
86
+ end
87
+
88
+ should "set maximum of range max" do
89
+ doc = @document.new
90
+ doc.should have_error_on(:name)
91
+ doc.name = '12345678'
92
+ doc.should have_error_on(:name)
93
+ doc.name = '123456'
94
+ doc.should_not have_error_on(:name)
95
+ end
96
+ end
97
+
98
+ context "with :length => hash shortcut" do
99
+ should "pass options through" do
100
+ @document.key :name, String, :length => {:minimum => 2}
101
+ doc = @document.new
102
+ doc.should have_error_on(:name)
103
+ doc.name = '12'
104
+ doc.should_not have_error_on(:name)
105
+ end
106
+ end
107
+ end # validates_length_of
108
+
109
+ context "Validating numericality of" do
110
+ should "work with validates_numericality_of macro" do
111
+ @document.key :age, Integer
112
+ @document.validates_numericality_of :age
113
+ doc = @document.new
114
+ doc.age = 'String'
115
+ doc.should have_error_on(:age)
116
+ doc.age = 23
117
+ doc.should_not have_error_on(:age)
118
+ end
119
+
120
+ # doesn't work cause MM casts 'String' to 0.0
121
+ # context "with :numeric shortcut" do
122
+ # should "work with integer or float" do
123
+ # puts 'before'
124
+ # @document.key :weight, Float, :numeric => true
125
+ # @document.validates_numericality_of :weight
126
+ # puts 'after'
127
+ # doc = @document.new
128
+ # doc.weight = 'String'
129
+ # puts doc.valid?
130
+ # puts doc.save
131
+ # puts doc.weight
132
+ # doc.should have_error_on(:weight)
133
+ # doc.weight = 23.0
134
+ # doc.should_not have_error_on(:weight)
135
+ # doc.weight = 23
136
+ # doc.should_not have_error_on(:weight)
137
+ # end
138
+ # end
139
+
140
+ # context "with :numeric shortcut on Integer key" do
141
+ # should "only work with integers" do
142
+ # @document.key :age, Integer, :numeric => true
143
+ # doc = @document.new
144
+ # doc.age = 'String'
145
+ # doc.should have_error_on(:age)
146
+ # doc.age = 23.1
147
+ # doc.should have_error_on(:age)
148
+ # doc.age = 23
149
+ # doc.should_not have_error_on(:age)
150
+ # end
151
+ # end
152
+ end # numericality of
153
+
154
+ context "validating presence of" do
155
+ should "work with validates_presence_of macro" do
156
+ @document.key :name, String
157
+ @document.validates_presence_of :name
158
+ doc = @document.new
159
+ doc.should have_error_on(:name)
160
+ end
161
+
162
+ should "work with :required shortcut on key definition" do
163
+ @document.key :name, String, :required => true
164
+ doc = @document.new
165
+ doc.should have_error_on(:name)
166
+ end
167
+ end
168
+
169
+ context "validating exclusion of" do
170
+ should "throw error if enumerator not provided" do
171
+ @document.key :action, String
172
+ lambda {
173
+ @document.validates_exclusion_of :action
174
+ }.should raise_error(ArgumentError)
175
+ end
176
+
177
+ should "work with validates_exclusion_of macro" do
178
+ @document.key :action, String
179
+ @document.validates_exclusion_of :action, :within => %w(kick run)
180
+
181
+ doc = @document.new
182
+ doc.should_not have_error_on(:action)
183
+
184
+ doc.action = 'fart'
185
+ doc.should_not have_error_on(:action)
186
+
187
+ doc.action = 'kick'
188
+ doc.should have_error_on(:action, 'is reserved')
189
+ end
190
+
191
+ should "work with :not_in shortcut on key definition" do
192
+ @document.key :action, String, :not_in => %w(kick run)
193
+
194
+ doc = @document.new
195
+ doc.should_not have_error_on(:action)
196
+
197
+ doc.action = 'fart'
198
+ doc.should_not have_error_on(:action)
199
+
200
+ doc.action = 'kick'
201
+ doc.should have_error_on(:action, 'is reserved')
202
+ end
203
+
204
+ should "not have error if allow nil is true and value is nil" do
205
+ @document.key :action, String
206
+ @document.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
207
+
208
+ doc = @document.new
209
+ doc.should_not have_error_on(:action)
210
+ end
211
+
212
+ should "not have error if allow blank is true and value is blank" do
213
+ @document.key :action, String
214
+ @document.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
215
+
216
+ doc = @document.new(:action => '')
217
+ doc.should_not have_error_on(:action)
218
+ end
219
+ end
220
+
221
+ context "validating inclusion of" do
222
+ should "throw error if enumerator not provided" do
223
+ @document.key :action, String
224
+ lambda {
225
+ @document.validates_inclusion_of :action
226
+ }.should raise_error(ArgumentError)
227
+ end
228
+
229
+ should "work with validates_inclusion_of macro" do
230
+ @document.key :action, String
231
+ @document.validates_inclusion_of :action, :within => %w(kick run)
232
+
233
+ doc = @document.new
234
+ doc.should have_error_on(:action, 'is not included in the list')
235
+
236
+ doc.action = 'fart'
237
+ doc.should have_error_on(:action, 'is not included in the list')
238
+
239
+ doc.action = 'kick'
240
+ doc.should_not have_error_on(:action)
241
+ end
242
+
243
+ should "work with :in shortcut on key definition" do
244
+ @document.key :action, String, :in => %w(kick run)
245
+
246
+ doc = @document.new
247
+ doc.should have_error_on(:action, 'is not included in the list')
248
+
249
+ doc.action = 'fart'
250
+ doc.should have_error_on(:action, 'is not included in the list')
251
+
252
+ doc.action = 'kick'
253
+ doc.should_not have_error_on(:action)
254
+ end
255
+
256
+ should "not have error if allow nil is true and value is nil" do
257
+ @document.key :action, String
258
+ @document.validates_inclusion_of :action, :within => %w(kick run), :allow_nil => true
259
+
260
+ doc = @document.new
261
+ doc.should_not have_error_on(:action)
262
+ end
263
+
264
+ should "not have error if allow blank is true and value is blank" do
265
+ @document.key :action, String
266
+ @document.validates_inclusion_of :action, :within => %w(kick run), :allow_blank => true
267
+
268
+ doc = @document.new(:action => '')
269
+ doc.should_not have_error_on(:action)
270
+ end
271
+ end
272
+
273
+ end # End on a Document
274
+
275
+ context "On an EmbeddedDocument" do
276
+ setup do
277
+ @embedded_doc = EDoc()
278
+ end
279
+
280
+ context "Validating acceptance of" do
281
+ should "work with validates_acceptance_of macro" do
282
+ @embedded_doc.key :terms, String
283
+ @embedded_doc.validates_acceptance_of :terms
284
+ doc = @embedded_doc.new(:terms => '')
285
+ doc.should have_error_on(:terms)
286
+ doc.terms = '1'
287
+ doc.should_not have_error_on(:terms)
288
+ end
289
+ end
290
+
291
+ context "validating confirmation of" do
292
+ should "work with validates_confirmation_of macro" do
293
+ @embedded_doc.key :password, String
294
+ @embedded_doc.validates_confirmation_of :password
295
+ doc = @embedded_doc.new
296
+ doc.password = 'foobar'
297
+ doc.password_confirmation = 'fgfg'
298
+ doc.should have_error_on(:password)
299
+ doc.password_confirmation = 'foobar'
300
+ doc.should_not have_error_on(:password)
301
+ end
302
+ end
303
+
304
+ context "validating format of" do
305
+ should "work with validates_format_of macro" do
306
+ @embedded_doc.key :name, String
307
+ @embedded_doc.validates_format_of :name, :with => /.+/
308
+ doc = @embedded_doc.new
309
+ doc.should have_error_on(:name)
310
+ doc.name = 'John'
311
+ doc.should_not have_error_on(:name)
312
+ end
313
+
314
+ should "work with :format shorcut key" do
315
+ @embedded_doc.key :name, String, :format => /.+/
316
+ doc = @embedded_doc.new
317
+ doc.should have_error_on(:name)
318
+ doc.name = 'John'
319
+ doc.should_not have_error_on(:name)
320
+ end
321
+ end
322
+
323
+ context "validating length of" do
324
+ should "work with validates_length_of macro" do
325
+ @embedded_doc.key :name, String
326
+ @embedded_doc.validates_length_of :name, :minimum => 5
327
+ doc = @embedded_doc.new
328
+ doc.should have_error_on(:name)
329
+ end
330
+
331
+ context "with :length => integer shortcut" do
332
+ should "set maximum of integer provided" do
333
+ @embedded_doc.key :name, String, :length => 5
334
+ doc = @embedded_doc.new
335
+ doc.name = '123456'
336
+ doc.should have_error_on(:name)
337
+ doc.name = '12345'
338
+ doc.should_not have_error_on(:name)
339
+ end
340
+ end
341
+
342
+ context "with :length => range shortcut" do
343
+ setup do
344
+ @embedded_doc.key :name, String, :length => 5..7
345
+ end
346
+
347
+ should "set minimum of range min" do
348
+ doc = @embedded_doc.new
349
+ doc.should have_error_on(:name)
350
+ doc.name = '123456'
351
+ doc.should_not have_error_on(:name)
352
+ end
353
+
354
+ should "set maximum of range max" do
355
+ doc = @embedded_doc.new
356
+ doc.should have_error_on(:name)
357
+ doc.name = '12345678'
358
+ doc.should have_error_on(:name)
359
+ doc.name = '123456'
360
+ doc.should_not have_error_on(:name)
361
+ end
362
+ end
363
+
364
+ context "with :length => hash shortcut" do
365
+ should "pass options through" do
366
+ @embedded_doc.key :name, String, :length => {:minimum => 2}
367
+ doc = @embedded_doc.new
368
+ doc.should have_error_on(:name)
369
+ doc.name = '12'
370
+ doc.should_not have_error_on(:name)
371
+ end
372
+ end
373
+ end # validates_length_of
374
+
375
+ context "Validating numericality of" do
376
+ should "work with validates_numericality_of macro" do
377
+ @embedded_doc.key :age, Integer
378
+ @embedded_doc.validates_numericality_of :age
379
+ doc = @embedded_doc.new
380
+ doc.age = 'String'
381
+ doc.should have_error_on(:age)
382
+ doc.age = 23
383
+ doc.should_not have_error_on(:age)
384
+ end
385
+
386
+ # context "with :numeric shortcut" do
387
+ # should "work with integer or float" do
388
+ # @embedded_doc.key :weight, Float, :numeric => true
389
+ # doc = @embedded_doc.new
390
+ # doc.weight = 'String'
391
+ # doc.should have_error_on(:weight)
392
+ # doc.weight = 23.0
393
+ # doc.should_not have_error_on(:weight)
394
+ # doc.weight = 23
395
+ # doc.should_not have_error_on(:weight)
396
+ # end
397
+ # end
398
+
399
+ # context "with :numeric shortcut on Integer key" do
400
+ # should "only work with integers" do
401
+ # @embedded_doc.key :age, Integer, :numeric => true
402
+ # doc = @embedded_doc.new
403
+ # doc.age = 'String'
404
+ # doc.should have_error_on(:age)
405
+ # doc.age = 23.1
406
+ # doc.should have_error_on(:age)
407
+ # doc.age = 23
408
+ # doc.should_not have_error_on(:age)
409
+ # end
410
+ # end
411
+ end # numericality of
412
+
413
+ context "validating presence of" do
414
+ should "work with validates_presence_of macro" do
415
+ @embedded_doc.key :name, String
416
+ @embedded_doc.validates_presence_of :name
417
+ doc = @embedded_doc.new
418
+ doc.should have_error_on(:name)
419
+ end
420
+
421
+ should "work with :required shortcut on key definition" do
422
+ @embedded_doc.key :name, String, :required => true
423
+ doc = @embedded_doc.new
424
+ doc.should have_error_on(:name)
425
+ end
426
+ end
427
+
428
+ context "validating exclusion of" do
429
+ should "throw error if enumerator not provided" do
430
+ @embedded_doc.key :action, String
431
+ lambda {
432
+ @embedded_doc.validates_exclusion_of :action
433
+ }.should raise_error(ArgumentError)
434
+ end
435
+
436
+ should "work with validates_exclusion_of macro" do
437
+ @embedded_doc.key :action, String
438
+ @embedded_doc.validates_exclusion_of :action, :within => %w(kick run)
439
+
440
+ doc = @embedded_doc.new
441
+ doc.should_not have_error_on(:action)
442
+
443
+ doc.action = 'fart'
444
+ doc.should_not have_error_on(:action)
445
+
446
+ doc.action = 'kick'
447
+ doc.should have_error_on(:action, 'is reserved')
448
+ end
449
+
450
+ should "work with :not_in shortcut on key definition" do
451
+ @embedded_doc.key :action, String, :not_in => %w(kick run)
452
+
453
+ doc = @embedded_doc.new
454
+ doc.should_not have_error_on(:action)
455
+
456
+ doc.action = 'fart'
457
+ doc.should_not have_error_on(:action)
458
+
459
+ doc.action = 'kick'
460
+ doc.should have_error_on(:action, 'is reserved')
461
+ end
462
+
463
+ should "not have error if allow nil is true and value is nil" do
464
+ @embedded_doc.key :action, String
465
+ @embedded_doc.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
466
+
467
+ doc = @embedded_doc.new
468
+ doc.should_not have_error_on(:action)
469
+ end
470
+
471
+ should "not have error if allow blank is true and value is blank" do
472
+ @embedded_doc.key :action, String
473
+ @embedded_doc.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
474
+
475
+ doc = @embedded_doc.new(:action => '')
476
+ doc.should_not have_error_on(:action)
477
+ end
478
+ end
479
+
480
+ context "validating inclusion of" do
481
+ should "throw error if enumerator not provided" do
482
+ @embedded_doc.key :action, String
483
+ lambda {
484
+ @embedded_doc.validates_inclusion_of :action
485
+ }.should raise_error(ArgumentError)
486
+ end
487
+
488
+ should "work with validates_inclusion_of macro" do
489
+ @embedded_doc.key :action, String
490
+ @embedded_doc.validates_inclusion_of :action, :within => %w(kick run)
491
+
492
+ doc = @embedded_doc.new
493
+ doc.should have_error_on(:action, 'is not included in the list')
494
+
495
+ doc.action = 'fart'
496
+ doc.should have_error_on(:action, 'is not included in the list')
497
+
498
+ doc.action = 'kick'
499
+ doc.should_not have_error_on(:action)
500
+ end
501
+
502
+ should "work with :in shortcut on key definition" do
503
+ @embedded_doc.key :action, String, :in => %w(kick run)
504
+
505
+ doc = @embedded_doc.new
506
+ doc.should have_error_on(:action, 'is not included in the list')
507
+
508
+ doc.action = 'fart'
509
+ doc.should have_error_on(:action, 'is not included in the list')
510
+
511
+ doc.action = 'kick'
512
+ doc.should_not have_error_on(:action)
513
+ end
514
+
515
+ should "not have error if allow nil is true and value is nil" do
516
+ @embedded_doc.key :action, String
517
+ @embedded_doc.validates_inclusion_of :action, :within => %w(kick run), :allow_nil => true
518
+
519
+ doc = @embedded_doc.new
520
+ doc.should_not have_error_on(:action)
521
+ end
522
+
523
+ should "not have error if allow blank is true and value is blank" do
524
+ @embedded_doc.key :action, String
525
+ @embedded_doc.validates_inclusion_of :action, :within => %w(kick run), :allow_blank => true
526
+
527
+ doc = @embedded_doc.new(:action => '')
528
+ doc.should_not have_error_on(:action)
529
+ end
530
+ end
531
+
532
+ end # End on an EmbeddedDocument
533
+
534
+ end # Validations
535
+
536
+ context "Adding validation errors" do
537
+ setup do
538
+ @document = Doc do
539
+ key :action, String
540
+ def action_present
541
+ errors.add(:action, 'is invalid') if action.blank?
542
+ end
543
+ end
544
+ end
545
+
546
+ should "work with validate callback" do
547
+ @document.validate :action_present
548
+
549
+ doc = @document.new
550
+ doc.action = nil
551
+ doc.should have_error_on(:action)
552
+
553
+ doc.action = 'kick'
554
+ doc.should_not have_error_on(:action)
555
+ end
556
+ end
557
+ end