sample_models 0.9.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 (43) hide show
  1. data/.gitignore +4 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.markdown +345 -0
  4. data/Rakefile +53 -0
  5. data/VERSION +1 -0
  6. data/init.rb +2 -0
  7. data/install.rb +1 -0
  8. data/lib/sample_models/creation.rb +116 -0
  9. data/lib/sample_models/finder.rb +81 -0
  10. data/lib/sample_models/model.rb +136 -0
  11. data/lib/sample_models/sampler.rb +110 -0
  12. data/lib/sample_models.rb +117 -0
  13. data/sample_models.gemspec +89 -0
  14. data/spec/sample_models_spec.rb +11 -0
  15. data/spec_or_test/database.yml +7 -0
  16. data/spec_or_test/setup.rb +249 -0
  17. data/spec_or_test/specs_or_test_cases.rb +569 -0
  18. data/spec_or_test/vendor/validates_email_format_of/CHANGELOG +11 -0
  19. data/spec_or_test/vendor/validates_email_format_of/MIT-LICENSE +20 -0
  20. data/spec_or_test/vendor/validates_email_format_of/README +28 -0
  21. data/spec_or_test/vendor/validates_email_format_of/TODO +1 -0
  22. data/spec_or_test/vendor/validates_email_format_of/init.rb +1 -0
  23. data/spec_or_test/vendor/validates_email_format_of/lib/validates_email_format_of.rb +41 -0
  24. data/spec_or_test/vendor/validates_email_format_of/rakefile +28 -0
  25. data/spec_or_test/vendor/validates_email_format_of/test/database.yml +3 -0
  26. data/spec_or_test/vendor/validates_email_format_of/test/fixtures/people.yml +3 -0
  27. data/spec_or_test/vendor/validates_email_format_of/test/fixtures/person.rb +3 -0
  28. data/spec_or_test/vendor/validates_email_format_of/test/schema.rb +5 -0
  29. data/spec_or_test/vendor/validates_email_format_of/test/test_helper.rb +35 -0
  30. data/spec_or_test/vendor/validates_email_format_of/test/validates_email_format_of_test.rb +82 -0
  31. data/tasks/sample_models_tasks.rake +4 -0
  32. data/test/test_sample_models.rb +28 -0
  33. data/uninstall.rb +1 -0
  34. data/vendor/ar_query/MIT-LICENSE +20 -0
  35. data/vendor/ar_query/README +0 -0
  36. data/vendor/ar_query/ar_query.gemspec +16 -0
  37. data/vendor/ar_query/init.rb +1 -0
  38. data/vendor/ar_query/install.rb +1 -0
  39. data/vendor/ar_query/lib/ar_query.rb +146 -0
  40. data/vendor/ar_query/spec/ar_query_spec.rb +318 -0
  41. data/vendor/ar_query/tasks/ar_query_tasks.rake +0 -0
  42. data/vendor/ar_query/uninstall.rb +1 -0
  43. metadata +117 -0
@@ -0,0 +1,569 @@
1
+ describe "Model.sample" do
2
+ it 'should fill non-validated, non-configured fields with a non-blank value' do
3
+ appt = Appointment.sample
4
+ assert appt.end_time.is_a?(Time)
5
+ blog_post = BlogPost.sample
6
+ assert blog_post.average_rating.is_a?(Float)
7
+ show = Show.sample
8
+ assert show.subscription_price.is_a?(Integer)
9
+ network = Network.sample
10
+ assert network.name.is_a?(String)
11
+ end
12
+
13
+ it 'should allow overrides of all fields in sample' do
14
+ user = User.sample(
15
+ :homepage => 'http://mysite.com/', :password => 'myownpassword'
16
+ )
17
+ assert_equal 'http://mysite.com/', user.homepage
18
+ assert_equal 'myownpassword', user.password
19
+ end
20
+
21
+ it 'should pick the first value given in a validates_inclusion_of' do
22
+ user = User.sample
23
+ assert_equal 'f', user.gender
24
+ end
25
+
26
+ it "should raise the standard validation error if you break the model's validates_inclusion_of validation" do
27
+ assert_raise(ActiveRecord::RecordInvalid) do
28
+ User.sample(:gender => 'x')
29
+ end
30
+ end
31
+
32
+ it 'should set emails based on a validation' do
33
+ user = User.sample
34
+ assert_match /^.*@.*\..*/, user.email
35
+ end
36
+
37
+ it "should raise the standard validation error if you break the model's validates_email_format_of validation" do
38
+ assert_raise(ActiveRecord::RecordInvalid) do
39
+ User.sample(:email => 'call.me')
40
+ end
41
+ end
42
+
43
+ it 'should set fields that are not validated to non-nil values' do
44
+ user = User.sample
45
+ assert_not_nil user.homepage
46
+ end
47
+
48
+ it 'should not override a boolean default' do
49
+ assert !Comment.sample.flagged_as_spam
50
+ end
51
+
52
+ it 'should return the same instance with two consecutive calls without arguments' do
53
+ user1 = User.sample
54
+ user2 = User.sample
55
+ assert_equal user1, user2
56
+ assert_equal user1.login, user2.login
57
+ end
58
+
59
+ it 'should return the same instance with two consecutive calls with the same arguments' do
60
+ user1 = User.sample :homepage => 'http://john.doe.com/'
61
+ user2 = User.sample :homepage => 'http://john.doe.com/'
62
+ assert_equal user1, user2
63
+ assert_equal user1.login, user2.login
64
+ end
65
+
66
+ it 'should return different instances with two consecutive calls with different arguments' do
67
+ user1 = User.sample :homepage => 'http://john.doe.com/'
68
+ user2 = User.sample :homepage => 'http://jane.doe.com/'
69
+ assert_not_equal user1, user2
70
+ assert_not_equal user1.login, user2.login
71
+ end
72
+
73
+ it 'should return a different instance to a later call with more specific attributes' do
74
+ user1 = User.sample
75
+ user2 = User.sample :homepage => 'http://john.doe.com/'
76
+ assert_not_equal user1, user2
77
+ assert_not_equal user1.login, user2.login
78
+ end
79
+
80
+ it 'should return the same instance to a later call with less specific attributes' do
81
+ User.destroy_all
82
+ user1 = User.sample(
83
+ :homepage => 'http://mysite.com/', :password => 'myownpassword'
84
+ )
85
+ user2 = User.sample :homepage => 'http://mysite.com/'
86
+ assert_equal user1, user2
87
+ user3 = User.sample
88
+ assert_equal user1, user3
89
+ end
90
+ end
91
+
92
+ describe 'Model with a belongs_to association' do
93
+ it 'should be associated with the belongs_to recipient by default' do
94
+ blog_post = BlogPost.sample
95
+ assert blog_post.user
96
+ assert blog_post.user.is_a?(User)
97
+ end
98
+
99
+ it 'should set a custom value by the association name' do
100
+ user = User.sample
101
+ blog_post = BlogPost.sample :user => user
102
+ assert_equal user, blog_post.user
103
+ end
104
+
105
+ it 'should return the same instance with two consecutive calls with the same associated value' do
106
+ user = User.sample
107
+ blog_post1 = BlogPost.sample :user => user
108
+ blog_post2 = BlogPost.sample :user => user
109
+ assert_equal blog_post1, blog_post2
110
+ end
111
+
112
+ it 'should set a custom value by the column name' do
113
+ user = User.sample
114
+ blog_post = BlogPost.sample :user_id => user.id
115
+ assert_equal user, blog_post.user
116
+ end
117
+
118
+ it 'should set a custom nil value by the association name' do
119
+ show = Show.sample :network => nil
120
+ assert_nil show.network
121
+ assert_nil show.network_id
122
+ end
123
+
124
+ it 'should set a custom nil value by the association name, even when there has been a previously created record with default attributes' do
125
+ show1 = Show.sample
126
+ show2 = Show.sample :network => nil
127
+ assert_nil show2.network
128
+ end
129
+
130
+ it 'should set a custom nil value by the association name, even when there has been a previously created record with that association assigned' do
131
+ show1 = Show.sample :network => Network.sample
132
+ show2 = Show.sample :network => nil
133
+ assert_nil show2.network
134
+ end
135
+
136
+ it 'should set a custom nil value by the association ID' do
137
+ show = Show.sample :network_id => nil
138
+ assert_nil show.network
139
+ assert_nil show.network_id
140
+ end
141
+
142
+ it 'should have no problem with circular associations' do
143
+ assert User.sample.favorite_blog_post.is_a?(BlogPost)
144
+ assert BlogPost.sample.user.is_a?(User)
145
+ end
146
+
147
+ it 'should allow creation of a custom associated instance with a hash' do
148
+ show = Show.sample(
149
+ :name => 'The Daily Show', :network => {:name => 'Comedy Central'}
150
+ )
151
+ assert_equal "The Daily Show", show.name
152
+ assert_equal 'Comedy Central', show.network.name
153
+ end
154
+
155
+ it 'should return the same instance with two consecutive calls with the same association hash' do
156
+ show1 = Show.sample(
157
+ :name => 'The Daily Show', :network => {:name => 'Comedy Central'}
158
+ )
159
+ show2 = Show.sample(
160
+ :name => 'The Daily Show', :network => {:name => 'Comedy Central'}
161
+ )
162
+ assert_equal show1, show2
163
+ end
164
+
165
+ it 'should return different instances when passed different association hashes' do
166
+ show1 = Show.sample :network => {:name => 'Comedy Central'}
167
+ show2 = Show.sample :network => {:name => 'MTV'}
168
+ assert_not_equal show1, show2
169
+ end
170
+
171
+ it 'should gracefully handle destruction of an associated value' do
172
+ blog_post1 = BlogPost.sample
173
+ assert blog_post1.user
174
+ User.destroy_all
175
+ blog_post2 = BlogPost.sample
176
+ assert blog_post2.user
177
+ blog_post2.reload
178
+ assert blog_post2.user
179
+ blog_post3 = BlogPost.sample :name => 'funny'
180
+ assert blog_post3.user
181
+ end
182
+
183
+ it "should just create a new instance after destruction even if the association is not validated to be present" do
184
+ show1 = Show.sample :name => "Oh no you didn't"
185
+ show1.network.destroy
186
+ show2 = Show.sample :name => "Don't go there"
187
+ assert_not_nil show2.network
188
+ end
189
+
190
+ it 'should assign that belongs-to association if a value is passed in as the first argument' do
191
+ user = User.create_sample
192
+ blog_post = BlogPost.sample(user, :title => 'some title')
193
+ assert_equal user, blog_post.user
194
+ assert_equal 'some title', blog_post.title
195
+ end
196
+
197
+ it 'should allow you to specify that associated records with shortcut associations' do
198
+ network = Network.sample
199
+ video = Video.sample(:show => [network, {:name => "Jersey Shore"}])
200
+ assert_equal network, video.show.network
201
+ assert_equal "Jersey Shore", video.show.name
202
+ end
203
+ end
204
+
205
+ describe 'Model with a belongs_to association of the same class' do
206
+ before :all do
207
+ @blog_post = BlogPost.sample
208
+ end
209
+
210
+ it 'should be nil by default' do
211
+ assert_nil @blog_post.merged_into
212
+ end
213
+
214
+ it 'should not be itself by default' do
215
+ assert_not_equal @blog_post, @blog_post.merged_into
216
+ end
217
+ end
218
+
219
+ describe 'Model with a configured attribute' do
220
+ it 'should use that default' do
221
+ assert_equal 0, Video.sample.view_count
222
+ end
223
+ end
224
+
225
+ describe 'Model with a configured default association' do
226
+ it 'should use that default' do
227
+ cat = Category.sample
228
+ assert_nil cat.parent
229
+ end
230
+
231
+ it 'should allow that default to be overridden by name' do
232
+ sports = Category.sample :name => 'Sports'
233
+ soccer = Category.sample :name => 'Soccer', :parent => sports
234
+ assert_equal sports, soccer.parent
235
+ end
236
+
237
+ it 'should allow that default to be overridden by ID' do
238
+ sports = Category.sample :name => 'Sports'
239
+ soccer = Category.sample :name => 'Soccer', :parent_id => sports.id
240
+ assert_equal sports, soccer.parent
241
+ end
242
+ end
243
+
244
+
245
+ describe 'Model configuration with a bad field name' do
246
+ it 'should raise a useful error message' do
247
+ assert_raises(NoMethodError) do
248
+ SampleModels.configure Category do |category|
249
+ category.default.foobar ''
250
+ end
251
+ end
252
+ end
253
+ end
254
+
255
+ describe 'Model with a triangular belongs-to association' do
256
+ it 'should set unspecified association values to the same default instance' do
257
+ video = Video.sample :show => {:name => 'House'}
258
+ assert_equal 'House', video.show.name
259
+ assert video.show.network
260
+ assert video.network
261
+ assert_equal video.network, video.show.network
262
+ end
263
+ end
264
+
265
+ describe 'Model configured with .force_unique' do
266
+ it 'should return the same instance when called twice with no custom attrs' do
267
+ bp1 = BlogPost.sample
268
+ bp2 = BlogPost.sample
269
+ assert_equal bp1, bp2
270
+ assert_equal bp1.published_at, bp2.published_at
271
+ end
272
+
273
+ it 'should generated a new value for sample calls with custom attrs' do
274
+ bp1 = BlogPost.sample
275
+ bp2 = BlogPost.sample :user => {:login => 'francis'}
276
+ assert_not_equal bp1, bp2
277
+ assert_not_equal bp1.published_at, bp2.published_at
278
+ end
279
+
280
+ it 'should allow nil uniqued attribute if the model allows it' do
281
+ bp = BlogPost.sample :published_at => nil
282
+ assert_nil bp.published_at
283
+ end
284
+ end
285
+
286
+ describe 'Model with a redundant but validated association' do
287
+ it 'should use before_save to reconcile instance issues' do
288
+ video1 = Video.sample :episode => {:name => 'The one about the parents'}
289
+ assert_equal video1.show, video1.episode.show
290
+ video2 = Video.sample :show => {:name => 'South Park'}
291
+ assert_equal video2.show, video2.episode.show
292
+ assert_equal 'South Park', video2.show.name
293
+ end
294
+
295
+ it 'should not try to prefill the 2nd-hand association with another record' do
296
+ show = Show.sample(
297
+ :name => 'The Daily Show', :network => {:name => 'Comedy Central'}
298
+ )
299
+ video = Video.sample :show => {:name => 'House'}
300
+ assert_equal 'House', video.show.name
301
+ end
302
+
303
+ it 'should handle destroys gracefully' do
304
+ v1 = Video.sample
305
+ v1.show.destroy
306
+ v2 = Video.create_sample
307
+ assert_not_nil v2.show
308
+ assert_not_nil v2.episode.show
309
+ assert_equal v2.show, v2.episode.show
310
+ end
311
+
312
+ it 'should be able to use a before_save with only the first argument, and afterwards when a non-required association forces a 2nd save' do
313
+ appt = Appointment.sample
314
+ end
315
+
316
+ it 'should be able to recover from the associated value being destroyed' do
317
+ Appointment.sample
318
+ User.destroy_all
319
+ Appointment.sample
320
+ end
321
+ end
322
+
323
+ describe 'Model with a unique associated attribute' do
324
+ it 'should ensure that the attribute is unique every time you call create_sample' do
325
+ video_ids = {}
326
+ 10.times do
327
+ created = VideoTakedownEvent.create_sample
328
+ assert_nil video_ids[created.video_id]
329
+ video_ids[created.video_id] = true
330
+ end
331
+ end
332
+ end
333
+
334
+
335
+ describe 'Model with a unique scoped associated attribute' do
336
+ it 'should create a new instance when you create_sample with the same scope variable as before' do
337
+ video_fav1 = VideoFavorite.sample
338
+ video_fav2 = VideoFavorite.create_sample :user => video_fav1.user
339
+ assert_not_equal video_fav1, video_fav2
340
+ assert_equal video_fav1.user, video_fav2.user
341
+ assert_not_equal video_fav1.video, video_fav2.video
342
+ end
343
+ end
344
+
345
+ describe 'Model with a unique string attribute' do
346
+ it 'should use sequences to ensure that the attribute is unique every time you call create_sample' do
347
+ ids = []
348
+ logins = []
349
+ 10.times do
350
+ custom = User.create_sample
351
+ assert !ids.include?(custom.id)
352
+ ids << custom.id
353
+ assert !logins.include?(custom.login)
354
+ logins << custom.login
355
+ end
356
+ end
357
+
358
+ it 'should return the same instance if you use the same unique attribute each time' do
359
+ user1 = User.sample :login => 'john_doe'
360
+ user2 = User.sample :login => 'john_doe'
361
+ assert_equal user1, user2
362
+ end
363
+
364
+ it 'should raise an error if you try to make two different instances with the same string value' do
365
+ User.sample :login => 'john_doe'
366
+ assert_raise(ActiveRecord::RecordInvalid) do
367
+ User.sample(:login => 'john_doe', :homepage => 'http://john.doe.com/')
368
+ end
369
+ end
370
+ end
371
+
372
+ describe 'Model with a unique time attribute' do
373
+ it 'should use sequences to ensure that the attribute is unique every time you call create_sample' do
374
+ times = {}
375
+ 10.times do
376
+ custom = Appointment.create_sample
377
+ assert times[custom.start_time].nil?
378
+ times[custom.start_time] = true
379
+ end
380
+ end
381
+ end
382
+
383
+ describe 'Model with email and uniqueness validations on the same field' do
384
+ it 'should be able to create a value that satisfies both validations' do
385
+ emails = {}
386
+ 10.times do
387
+ custom = User.create_sample
388
+ assert emails[custom.email].nil?
389
+ emails[custom.email] = true
390
+ end
391
+ end
392
+ end
393
+
394
+ describe "Model when its default associated record has been deleted" do
395
+ it 'should just create a new one' do
396
+ ep1 = Episode.sample :name => 'funny'
397
+ ep1.show.destroy
398
+ ep2 = Episode.sample :name => 'funnier'
399
+ assert_not_nil ep2.show
400
+ end
401
+ end
402
+
403
+ describe 'Model with a has-many through association' do
404
+ it 'should not interfere with standard instance assignation' do
405
+ funny = Tag.sample :tag => 'funny'
406
+ bp = BlogPost.sample :tags => [funny]
407
+ assert_equal 1, bp.tags.size
408
+ assert_equal 'funny', bp.tags.first.tag
409
+ end
410
+
411
+ it 'should use the has-many through association to know that it needs to create a new instance' do
412
+ BlogPost.destroy_all
413
+ bp1 = BlogPost.sample
414
+ assert bp1.tags.empty?
415
+ funny = Tag.sample :tag => 'funny'
416
+ bp2 = BlogPost.sample :tags => [funny]
417
+ assert_equal %w(funny), bp2.tags.map(&:tag)
418
+ assert_not_equal bp1, bp2
419
+ assert_not_equal bp1.id, bp2.id
420
+ sad = Tag.sample :tag => 'sad'
421
+ bp3 = BlogPost.sample :tags => [sad]
422
+ assert_equal %w(sad), bp3.tags.map(&:tag)
423
+ [bp1, bp2].each do |other_bp|
424
+ assert_not_equal(
425
+ other_bp, bp3, "matched blog post with tags #{other_bp.tags.inspect}"
426
+ )
427
+ assert_not_equal other_bp.id, bp3.id
428
+ end
429
+ bp4 = BlogPost.sample :tags => [funny, sad]
430
+ [bp1, bp2, bp3].each do |other_bp|
431
+ assert_not_equal(
432
+ other_bp, bp4, "matched blog post with tags #{other_bp.tags.inspect}"
433
+ )
434
+ assert_not_equal other_bp.id, bp4.id
435
+ end
436
+ assert_equal 2, bp4.tags.size
437
+ %w(funny sad).each do |t|
438
+ assert bp4.tags.map(&:tag).include?(t)
439
+ end
440
+ bp5 = BlogPost.sample :tags => []
441
+ assert bp5.tags.empty?
442
+ end
443
+
444
+ it 'should create a later instance based on an empty has-many through association' do
445
+ BlogPost.destroy_all
446
+ funny = Tag.sample :tag => 'funny'
447
+ bp1 = BlogPost.sample :tags => [funny]
448
+ bp2 = BlogPost.sample :tags => []
449
+ assert_not_equal bp1, bp2
450
+ end
451
+
452
+ it "should create a later instance based on another attribute" do
453
+ BlogPost.destroy_all
454
+ funny = Tag.sample :tag => 'funny'
455
+ bp1 = BlogPost.sample :tags => [funny]
456
+ bp2 = BlogPost.sample :tags => [funny], :title => "really funny"
457
+ assert_not_equal bp1, bp2
458
+ end
459
+
460
+ it "should not match an earlier instance based on a has-many through array that's a subset of the earlier array" do
461
+ BlogPost.destroy_all
462
+ funny = Tag.sample :tag => 'funny'
463
+ sad = Tag.sample :tag => 'sad'
464
+ bp1 = BlogPost.sample :tags => [funny, sad]
465
+ bp2 = BlogPost.sample :tags => [funny]
466
+ assert_not_equal bp1, bp2
467
+ end
468
+
469
+ it 'should use the has-many through array to determine it already has a matching record' do
470
+ funny = Tag.sample :tag => 'funny'
471
+ bp1 = BlogPost.sample :tags => [funny]
472
+ bp2 = BlogPost.sample :tags => [funny]
473
+ assert_equal bp1, bp2
474
+ sad = Tag.sample :tag => 'sad'
475
+ bp3 = BlogPost.sample :tags => [funny, sad]
476
+ bp4 = BlogPost.sample :tags => [sad, funny]
477
+ assert_equal bp3, bp4
478
+ end
479
+
480
+ it 'should make it possible to assign and find as hashes' do
481
+ bp1 = BlogPost.sample :tags => [{:tag => 'funny'}]
482
+ assert_equal 1, bp1.tags.size
483
+ assert_equal 'funny', bp1.tags.first.tag
484
+ bp2 = BlogPost.sample :tags => [{:tag => 'funny'}]
485
+ assert_equal bp1, bp2
486
+ end
487
+
488
+ it 'should handle a mix of instances and hashes in the array' do
489
+ funny = Tag.sample :tag => 'funny'
490
+ bp = BlogPost.sample :tags => [{:tag => 'sad'}, funny]
491
+ assert_equal 2, bp.tags.size
492
+ %w(sad funny).each do |t|
493
+ assert bp.tags.map(&:tag).include?(t)
494
+ end
495
+ end
496
+ end
497
+
498
+ describe 'Model with an invalid default field' do
499
+ it "should raise an error when a bad configuration is attempted" do
500
+ assert_raise(RuntimeError) do
501
+ SampleModels.configure BlogPost do |b|
502
+ b.title.default ''
503
+ end
504
+ end
505
+ end
506
+ end
507
+
508
+ describe 'Model with a polymorphic belongs-to association' do
509
+ it 'should fill in that association with any old sample of another domain class' do
510
+ bookmark = Bookmark.sample
511
+ assert_not_nil bookmark.bookmarkable
512
+ assert_equal ActiveRecord::Base, bookmark.bookmarkable.class.superclass
513
+ assert_not_equal Bookmark, bookmark.bookmarkable.class
514
+ end
515
+
516
+ it 'should let you specify that association' do
517
+ blog_post = BlogPost.sample
518
+ bookmark = Bookmark.sample :bookmarkable => blog_post
519
+ assert_equal blog_post, bookmark.bookmarkable
520
+ end
521
+
522
+ it 'should let you specify that association with other leading associations' do
523
+ user = User.sample
524
+ blog_post = BlogPost.sample
525
+ sub = Subscription.sample user, :subscribable => blog_post
526
+ assert_equal user, sub.user
527
+ assert_equal blog_post, sub.subscribable
528
+ end
529
+
530
+ it 'should allow you to constrain which classes will be used by default for the polymorphic type' do
531
+ sub = Subscription.create_sample
532
+ assert_equal 'BlogPost', sub.subscribable_type
533
+ end
534
+ end
535
+
536
+ describe 'Model with a named sample' do
537
+ it 'should fill the default fields' do
538
+ bp = BlogPost.sample :funny
539
+ assert_equal 'Funny haha', bp.title
540
+ assert_equal 3.0, bp.average_rating
541
+ end
542
+
543
+ it 'should allow you to override fields' do
544
+ bp = BlogPost.sample :funny, :average_rating => 4.0
545
+ assert_equal 'Funny haha', bp.title
546
+ assert_equal 4.0, bp.average_rating
547
+ end
548
+
549
+ it 'should let you create_sample too' do
550
+ bp1 = BlogPost.sample :funny
551
+ bp2 = BlogPost.create_sample :funny
552
+ assert_not_equal bp1, bp2
553
+ end
554
+ end
555
+
556
+ describe 'Model which validates the presence of a date field' do
557
+ it 'should assign a value to that field' do
558
+ episode = Episode.sample
559
+ assert episode.original_air_date.is_a?(Date)
560
+ end
561
+ end
562
+
563
+ describe 'Model with a required accessor' do
564
+ it 'should assign that accessor' do
565
+ user_with_password = UserWithPassword.sample
566
+ assert user_with_password.password.present?
567
+ end
568
+ end
569
+
@@ -0,0 +1,11 @@
1
+ = CHANGELOG
2
+
3
+ == Version 1.0
4
+ * initial version
5
+
6
+ == Version 1.1 (the Francis Hwang edition)
7
+ * moved Regexp out of class methods into the ValidatesEmailFormatOf module
8
+
9
+ == TODO
10
+ * Create additional test cases to represent more RFC 2822 possibilities
11
+ * Package as gem
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2006 Alex Dunae
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,28 @@
1
+ = Validates email format
2
+
3
+ Validate various formats of email address against RFC 2822.
4
+
5
+ == Usage
6
+
7
+ class Person < ActiveRecord::Base
8
+ validates_email_format_of :email
9
+ end
10
+
11
+ == Testing
12
+
13
+ To execute the unit tests run <tt>rake test</tt>.
14
+
15
+ The unit tests for this plugin use an in-memory sqlite3 database.
16
+
17
+ == Resources
18
+
19
+ === Subversion
20
+
21
+ * http://code.dunae.ca/validates_email_format_of
22
+
23
+ == Credits
24
+
25
+ Written by Alex Dunae (dunae.ca), 2006-07.
26
+
27
+ Thanks to Francis Hwang (http://fhwang.net/) at Diversion Media for
28
+ creating the 1.1 update.
@@ -0,0 +1 @@
1
+ * Create additional test cases to represent more RFC 2822 possibilities
@@ -0,0 +1 @@
1
+ require 'validates_email_format_of'
@@ -0,0 +1,41 @@
1
+ module ValidatesEmailFormatOf
2
+ Regex = /
3
+ ^(
4
+ ([A-Za-z0-9]+_+)|
5
+ ([A-Za-z0-9]+\-+)|
6
+ ([A-Za-z0-9]+\.+)|
7
+ ([A-Za-z0-9]+\++)
8
+ )*[A-Za-z0-9]+@
9
+ ((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$
10
+ /ix
11
+ end
12
+
13
+ module ActiveRecord
14
+ module Validations
15
+ module ClassMethods
16
+ # Validates whether the value of the specified attribute is a valid email address
17
+ #
18
+ # class User < ActiveRecord::Base
19
+ # validates_email_format_of :email, :on => :create
20
+ # end
21
+ #
22
+ # Configuration options:
23
+ # * <tt>message</tt> - A custom error message (default is: " does not appear to be a valid e-mail address")
24
+ # * <tt>on</tt> Specifies when this validation is active (default is :save, other options :create, :update)
25
+ # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should
26
+ # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The
27
+ # method, proc or string should return or evaluate to a true or false value.
28
+ def validates_email_format_of(*attr_names)
29
+ configuration = { :message => ' does not appear to be a valid e-mail address',
30
+ :on => :save,
31
+ :with => ValidatesEmailFormatOf::Regex }
32
+
33
+ configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
34
+
35
+ validates_each(attr_names, configuration) do |record, attr_name, value|
36
+ record.errors.add(attr_name, configuration[:message]) unless value.to_s =~ configuration[:with]
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end