@@ -146,7 +146,6 @@ def save_without_validation(record)
146
146
errors = record . errors [ :genre ]
147
147
error = errors . respond_to? ( :first ) ? errors . first : errors # the return value sometimes was a string, sometimes an Array in Rails
148
148
error . should == I18n . t ( 'errors.messages.inclusion' )
149
- error . should == 'is not included in the list'
150
149
end
151
150
152
151
it 'should not allow nil for the attribute value' do
@@ -261,7 +260,7 @@ def save_without_validation(record)
261
260
262
261
end
263
262
264
- context 'if the :allow_blank option is set to a lambda ' do
263
+ context 'if the :allow_blank option is set to a lambda' do
265
264
266
265
before :each do
267
266
@klass = Song . disposable_copy do
@@ -370,6 +369,214 @@ def save_without_validation(record)
370
369
371
370
end
372
371
372
+ context 'when validating scalar attributes from a store_accessor' do
373
+
374
+ context 'without options' do
375
+
376
+ before :each do
377
+ @klass = Song . disposable_copy do
378
+ store :metadata , accessors : [ :format ] , coder : JSON
379
+
380
+ assignable_values_for :format do
381
+ %w[ mp3 wav ]
382
+ end
383
+ end
384
+ end
385
+
386
+ it 'should validate that the attribute is allowed' do
387
+ @klass . new ( :format => 'mp3' ) . should be_valid
388
+ @klass . new ( :format => 'disallowed value' ) . should_not be_valid
389
+ end
390
+
391
+ it 'should use the same error message as validates_inclusion_of' do
392
+ record = @klass . new ( :format => 'disallowed value' )
393
+ record . valid?
394
+ errors = record . errors [ :format ]
395
+ error = errors . respond_to? ( :first ) ? errors . first : errors # the return value sometimes was a string, sometimes an Array in Rails
396
+ error . should == I18n . t ( 'errors.messages.inclusion' )
397
+ end
398
+
399
+ it 'should not allow nil for the attribute value' do
400
+ @klass . new ( :format => nil ) . should_not be_valid
401
+ end
402
+
403
+ it 'should allow a previously saved value even if that value is no longer allowed' do
404
+ record = @klass . create! ( :format => 'mp3' )
405
+
406
+ record . update_column ( :metadata , { 'format' => 'pretend previously valid value' } ) # update without validations for the sake of this test
407
+ record . reload . should be_valid
408
+ end
409
+
410
+ it 'should allow a previously saved, blank format value even if that value is no longer allowed' do
411
+ record = @klass . create! ( :format => 'mp3' )
412
+
413
+ record . update_column ( :metadata , { 'format' => nil } ) # update without validations for the sake of this test
414
+ record . reload . should be_valid
415
+ end
416
+
417
+ it 'should not allow nil (the "previous value") if the record was never saved' do
418
+ record = @klass . new ( :format => nil )
419
+ record . should_not be_valid
420
+ end
421
+
422
+ it 'should generate a method returning the humanized value' do
423
+ song = @klass . new ( :format => 'mp3' )
424
+ song . humanized_format . should == 'MP3-Codec'
425
+ end
426
+
427
+ it 'should generate a method returning the humanized value, which is nil when the value is blank' do
428
+ song = @klass . new
429
+ song . format = nil
430
+ song . humanized_format . should be_nil
431
+ song . format = ''
432
+ song . humanized_format . should be_nil
433
+ end
434
+
435
+ it 'should generate an instance method to retrieve the humanization of any given value' do
436
+ song = @klass . new ( :format => 'mp3' )
437
+ song . humanized_format ( 'wav' ) . should == 'WAV-Codec'
438
+ end
439
+
440
+ it 'should generate a class method to retrieve the humanization of any given value' do
441
+ @klass . humanized_format ( 'wav' ) . should == 'WAV-Codec'
442
+ end
443
+
444
+ context 'for multiple: true' do
445
+ before :each do
446
+ @klass = Song . disposable_copy do
447
+ store :metadata , accessors : [ :instruments ] , coder : JSON
448
+
449
+ assignable_values_for :instruments , multiple : true do
450
+ %w[ piano drums guitar ]
451
+ end
452
+ end
453
+ end
454
+
455
+ it 'allows multiple assignments' do
456
+ song = @klass . new ( :instruments => %w[ guitar drums ] )
457
+ song . should be_valid
458
+ end
459
+
460
+ it 'should raise when trying to humanize a value without an argument' do
461
+ song = @klass . new
462
+ proc { song . humanized_instrument } . should raise_error ( ArgumentError )
463
+ end
464
+
465
+ it 'should generate an instance method to retrieve the humanization of any given value' do
466
+ song = @klass . new ( :instruments => 'drums' )
467
+ song . humanized_instrument ( 'piano' ) . should == 'Piano'
468
+ end
469
+
470
+ it 'should generate a class method to retrieve the humanization of any given value' do
471
+ @klass . humanized_instrument ( 'piano' ) . should == 'Piano'
472
+ end
473
+
474
+ it 'should generate an instance method to retrieve the humanizations of all current values' do
475
+ song = @klass . new
476
+ song . instruments = nil
477
+ song . humanized_instruments . should == nil
478
+ song . instruments = [ ]
479
+ song . humanized_instruments . should == [ ]
480
+ song . instruments = [ 'piano' , 'drums' ]
481
+ song . humanized_instruments . should == [ 'Piano' , 'Drums' ]
482
+ end
483
+ end
484
+ end
485
+
486
+ context 'if the :allow_blank option is set to true' do
487
+
488
+ before :each do
489
+ @klass = Song . disposable_copy do
490
+
491
+ store :metadata , accessors : [ :format ] , coder : JSON
492
+
493
+ assignable_values_for :format , :allow_blank => true do
494
+ %w[ mp3 wav ]
495
+ end
496
+ end
497
+ end
498
+
499
+ it 'should allow nil for the attribute value' do
500
+ @klass . new ( :format => nil ) . should be_valid
501
+ end
502
+
503
+ it 'should allow an empty string as value' do
504
+ @klass . new ( :format => '' ) . should be_valid
505
+ end
506
+
507
+ end
508
+
509
+ context 'if the :allow_blank option is set to a symbol that refers to an instance method' do
510
+
511
+ before :each do
512
+ @klass = Song . disposable_copy do
513
+
514
+ store :metadata , accessors : [ :format ] , coder : JSON
515
+
516
+ attr_accessor :format_may_be_blank
517
+
518
+ assignable_values_for :format , :allow_blank => :format_may_be_blank do
519
+ %w[ mp3 wav ]
520
+ end
521
+
522
+ end
523
+ end
524
+
525
+ it 'should call that method to determine if a blank value is allowed' do
526
+ @klass . new ( :format => '' , :format_may_be_blank => true ) . should be_valid
527
+ @klass . new ( :format => '' , :format_may_be_blank => false ) . should_not be_valid
528
+ end
529
+
530
+ end
531
+
532
+ context 'if the :allow_blank option is set to a lambda' do
533
+
534
+ before :each do
535
+ @klass = Song . disposable_copy do
536
+
537
+ store :metadata , accessors : [ :format ] , coder : JSON
538
+
539
+ attr_accessor :format_may_be_blank
540
+
541
+ assignable_values_for :format , :allow_blank => lambda { format_may_be_blank } do
542
+ %w[ mp3 wav ]
543
+ end
544
+
545
+ end
546
+ end
547
+
548
+ it 'should evaluate that lambda in the record context to determine if a blank value is allowed' do
549
+ @klass . new ( :format => '' , :format_may_be_blank => true ) . should be_valid
550
+ @klass . new ( :format => '' , :format_may_be_blank => false ) . should_not be_valid
551
+ end
552
+
553
+ end
554
+
555
+ context 'if the :message option is set to a string' do
556
+
557
+ before :each do
558
+ @klass = Song . disposable_copy do
559
+
560
+ store :metadata , accessors : [ :format ] , coder : JSON
561
+
562
+ assignable_values_for :format , :message => 'should be something different' do
563
+ %w[ mp3 wav ]
564
+ end
565
+ end
566
+ end
567
+
568
+ it 'should use this string as a custom error message' do
569
+ record = @klass . new ( :format => 'disallowed value' )
570
+ record . valid?
571
+ errors = record . errors [ :format ]
572
+ error = errors . respond_to? ( :first ) ? errors . first : errors # the return value sometimes was a string, sometimes an Array in Rails
573
+ error . should == 'should be something different'
574
+ end
575
+
576
+ end
577
+
578
+ end
579
+
373
580
context 'when validating belongs_to associations' do
374
581
375
582
it 'should validate that the association is allowed' do
0 commit comments