-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathactive_record_spec.rb
1400 lines (1149 loc) · 48.4 KB
/
active_record_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'spec_helper'
require 'ostruct'
def save_without_validation(record)
if ActiveRecord::VERSION::MAJOR < 3
record.save(false)
else
record.save(:validate => false)
end
end
describe AssignableValues::ActiveRecord do
describe '.assignable_values' do
it 'should raise an error when not called with a block or :through option' do
expect do
Song.disposable_copy do
assignable_values_for :genre
end
end.to raise_error(AssignableValues::NoValuesGiven)
end
it 'should raise an error when called with unsupported options' do
expect do
Song.disposable_copy do
assignable_values_for :genre, unsupported_option: 42
end
end.to raise_error(AssignableValues::UnsupportedOption, 'The following options are not supported: :unsupported_option')
end
context 'when validating virtual attributes' do
before :each do
@klass = Song.disposable_copy do
assignable_values_for :virtual_sub_genre do
%w[pop rock]
end
assignable_values_for :virtual_multi_genres, :multiple => true, :allow_blank => true do
%w[pop rock]
end
end
end
it 'should validate that the attribute is allowed' do
@klass.new(:virtual_sub_genre => 'pop').should be_valid
@klass.new(:virtual_sub_genre => 'disallowed value').should_not be_valid
end
it 'should not allow nil for the attribute value' do
@klass.new(:virtual_sub_genre => nil).should_not be_valid
end
it 'should generate a method returning the humanized value' do
song = @klass.new(:virtual_sub_genre => 'pop')
song.humanized_virtual_sub_genre.should == 'Pop music'
song.humanized_virtual_sub_genre('rock').should == 'Rock music'
song.humanized_virtual_multi_genre('rock').should == 'Rock music'
end
end
context 'when the options are passed as a hash' do
before :each do
@klass = Song.disposable_copy do
assignable_values_for(:virtual_multi_genres, { :multiple => true, :allow_blank => true }) do
%w[pop rock]
end
end
end
it 'should not raise an error' do
@klass.new(:virtual_multi_genres => ['rock']).should be_valid
end
end
context 'when validating virtual attributes with multiple: true' do
context 'with allow_blank: false' do
before :each do
@klass = Song.disposable_copy do
assignable_values_for :virtual_multi_genres, :multiple => true do
%w[pop rock]
end
end
end
it 'should validate that the attribute is a subset' do
@klass.new(:virtual_multi_genres => ['pop']).should be_valid
@klass.new(:virtual_multi_genres => ['pop', 'rock']).should be_valid
@klass.new(:virtual_multi_genres => ['pop', 'disallowed value']).should_not be_valid
end
it 'should not allow nil or [] for allow_blank: false' do
@klass.new(:virtual_multi_genres => nil).should_not be_valid
@klass.new(:virtual_multi_genres => []).should_not be_valid
end
end
context 'with allow_blank: true' do
before :each do
@klass = Song.disposable_copy do
assignable_values_for :virtual_multi_genres, :multiple => true, :allow_blank => true do
%w[pop rock]
end
end
end
it 'should allow nil or [] for allow_blank: false' do
@klass.new(:virtual_multi_genres => nil).should be_valid
@klass.new(:virtual_multi_genres => []).should be_valid
end
end
end
context 'when validating scalar attributes' do
context 'without options' do
before :each do
@klass = Song.disposable_copy do
assignable_values_for :genre do
%w[pop rock]
end
assignable_values_for :virtual_multi_genres, :multiple => true, :allow_blank => true do
%w[pop rock]
end
end
end
it 'should validate that the attribute is allowed' do
@klass.new(:genre => 'pop').should be_valid
@klass.new(:genre => 'disallowed value').should_not be_valid
end
it 'should use the same error message as validates_inclusion_of' do
record = @klass.new(:genre => 'disallowed value')
record.valid?
errors = record.errors[:genre]
error = errors.respond_to?(:first) ? errors.first : errors # the return value sometimes was a string, sometimes an Array in Rails
error.should == I18n.t('errors.messages.inclusion')
error.should == 'is not included in the list'
end
it 'should not allow nil for the attribute value' do
@klass.new(:genre => nil).should_not be_valid
end
it 'should allow a previously saved value even if that value is no longer allowed' do
record = @klass.create!(:genre => 'pop')
@klass.update_all(:genre => 'pretend previously valid value') # update without validations for the sake of this test
record.reload.should be_valid
end
it 'should allow a previously saved, blank value even if that value is no longer allowed' do
record = @klass.create!(:genre => 'pop')
@klass.update_all(:genre => '') # update without validations for the sake of this test
record.reload.should be_valid
end
it 'should not allow nil (the "previous value") if the record was never saved' do
record = @klass.new(:genre => nil)
# Show that nil is not assignable, even though `record.genre_was` is nil.
record.should_not be_valid
end
it 'should generate a method returning the humanized value' do
song = @klass.new(:genre => 'pop')
song.humanized_genre.should == 'Pop music'
end
it 'should generate a method returning the humanized value, which is nil when the value is blank' do
song = @klass.new
song.genre = nil
song.humanized_genre.should be_nil
song.genre = ''
song.humanized_genre.should be_nil
end
it 'should generate an instance method to retrieve the humanization of any given value' do
song = @klass.new(:genre => 'pop')
song.humanized_genre('rock').should == 'Rock music'
end
it 'should generate a class method to retrieve the humanization of any given value' do
@klass.humanized_genre('rock').should == 'Rock music'
end
context 'for multiple: true' do
it 'should raise when trying to humanize a value without an argument' do
song = @klass.new
proc { song.humanized_virtual_multi_genre }.should raise_error(ArgumentError)
end
it 'should generate an instance method to retrieve the humanization of any given value' do
song = @klass.new(:genre => 'pop')
song.humanized_virtual_multi_genre('rock').should == 'Rock music'
end
it 'should generate a class method to retrieve the humanization of any given value' do
@klass.humanized_virtual_multi_genre('rock').should == 'Rock music'
end
it 'should generate an instance method to retrieve the humanizations of all current values' do
song = @klass.new
song.virtual_multi_genres = nil
song.humanized_virtual_multi_genres.should == nil
song.virtual_multi_genres = []
song.humanized_virtual_multi_genres.should == []
song.virtual_multi_genres = ['pop', 'rock']
song.humanized_virtual_multi_genres.should == ['Pop music', 'Rock music']
end
end
end
context 'if the :allow_blank option is set to true' do
before :each do
@klass = Song.disposable_copy do
assignable_values_for :genre, :allow_blank => true do
%w[pop rock]
end
end
end
it 'should allow nil for the attribute value' do
@klass.new(:genre => nil).should be_valid
end
it 'should allow an empty string as value' do
@klass.new(:genre => '').should be_valid
end
end
context 'if the :allow_blank option is set to a symbol that refers to an instance method' do
before :each do
@klass = Song.disposable_copy do
attr_accessor :genre_may_be_blank
assignable_values_for :genre, :allow_blank => :genre_may_be_blank do
%w[pop rock]
end
end
end
it 'should call that method to determine if a blank value is allowed' do
@klass.new(:genre => '', :genre_may_be_blank => true).should be_valid
@klass.new(:genre => '', :genre_may_be_blank => false).should_not be_valid
end
end
context 'if the :allow_blank option is set to a lambda' do
before :each do
@klass = Song.disposable_copy do
attr_accessor :genre_may_be_blank
assignable_values_for :genre, :allow_blank => lambda { genre_may_be_blank } do
%w[pop rock]
end
end
end
it 'should evaluate that lambda in the record context to determine if a blank value is allowed' do
@klass.new(:genre => '', :genre_may_be_blank => true).should be_valid
@klass.new(:genre => '', :genre_may_be_blank => false).should_not be_valid
end
end
context 'if the :message option is set to a string' do
before :each do
@klass = Song.disposable_copy do
assignable_values_for :genre, :message => 'should be something different' do
%w[pop rock]
end
end
end
it 'should use this string as a custom error message' do
record = @klass.new(:genre => 'disallowed value')
record.valid?
errors = record.errors[:genre]
error = errors.respond_to?(:first) ? errors.first : errors # the return value sometimes was a string, sometimes an Array in Rails
error.should == 'should be something different'
end
end
end
context 'when validating scalar attributes with multiple: true' do
context 'without options' do
before :each do
@klass = Song.disposable_copy do
assignable_values_for :multi_genres, :multiple => true do
%w[pop rock]
end
end
end
it 'should validate that the attribute is allowed' do
@klass.new(:multi_genres => ['pop']).should be_valid
@klass.new(:multi_genres => ['pop', 'rock']).should be_valid
@klass.new(:multi_genres => ['pop', 'invalid value']).should_not be_valid
end
it 'should not allow a scalar attribute' do
@klass.new(:multi_genres => 'pop').should_not be_valid
end
it 'should not allow nil or [] for the attribute value' do
@klass.new(:multi_genres => nil).should_not be_valid
@klass.new(:multi_genres => []).should_not be_valid
end
it 'should allow a subset of previously saved values even if that value is no longer allowed' do
record = @klass.create!(:multi_genres => ['pop'])
record.multi_genres = ['pretend', 'previously', 'valid', 'value']
save_without_validation(record) # update without validation for the sake of this test
record.reload.should be_valid
record.multi_genres = ['valid', 'previously', 'pop']
record.should be_valid
end
it 'should allow a previously saved, blank value even if that value is no longer allowed' do
record = @klass.create!(:multi_genres => ['pop'])
@klass.update_all(:multi_genres => []) # update without validations for the sake of this test
record.reload.should be_valid
end
end
context 'if the :allow_blank option is set to true' do
before :each do
@klass = Song.disposable_copy do
assignable_values_for :multi_genres, :multiple => true, :allow_blank => true do
%w[pop rock]
end
end
end
it 'should allow nil for the attribute value' do
@klass.new(:multi_genres => nil).should be_valid
end
it 'should allow an empty array as value' do
@klass.new(:multi_genres => []).should be_valid
end
end
end
context 'when validating scalar attributes from a store_accessor' do
context 'without options' do
before :each do
@klass = Song.disposable_copy do
store_accessor :metadata, :format
assignable_values_for :format do
%w[mp3 wav]
end
end
end
it 'should validate that the attribute is allowed' do
@klass.new(:format => 'mp3').should be_valid
@klass.new(:format => 'disallowed value').should_not be_valid
end
it 'should use the same error message as validates_inclusion_of' do
record = @klass.new(:format => 'disallowed value')
record.valid?
errors = record.errors[:format]
error = errors.respond_to?(:first) ? errors.first : errors # the return value sometimes was a string, sometimes an Array in Rails
error.should == I18n.t('errors.messages.inclusion')
error.should == 'is not included in the list'
end
it 'should not allow nil for the attribute value' do
@klass.new(:format => nil).should_not be_valid
end
it 'should allow a previously saved value even if that value is no longer allowed' do
record = @klass.create!(:format => 'mp3')
@klass.update_all(metadata: { 'format' => 'pretend previously valid value' }) # update without validations for the sake of this test
record.reload.should be_valid
end
it 'should allow a previously saved, blank format value even if that value is no longer allowed' do
record = @klass.create!(:format => 'mp3')
@klass.update_all(metadata: { 'format' => nil }) # update without validations for the sake of this test
record.reload.should be_valid
end
it 'should not allow nil (the "previous value") if the record was never saved' do
record = @klass.new(:format => nil)
# Show that nil is not assignable, even though `record.genre_was` is nil.
record.should_not be_valid
end
it 'should generate a method returning the humanized value' do
song = @klass.new(:format => 'mp3')
song.humanized_format.should == 'MP3-Codec'
end
it 'should generate a method returning the humanized value, which is nil when the value is blank' do
song = @klass.new
song.format = nil
song.humanized_format.should be_nil
song.format = ''
song.humanized_format.should be_nil
end
it 'should generate an instance method to retrieve the humanization of any given value' do
song = @klass.new(:format => 'mp3')
song.humanized_format('wav').should == 'WAV-Codec'
end
it 'should generate a class method to retrieve the humanization of any given value' do
@klass.humanized_format('wav').should == 'WAV-Codec'
end
context 'for multiple: true' do
before :each do
@klass = Song.disposable_copy do
store_accessor :metadata, :instruments
assignable_values_for :instruments, multiple: true do
%w[piano drums guitar]
end
end
end
it 'should raise when trying to humanize a value without an argument' do
song = @klass.new
proc { song.humanized_instrument }.should raise_error(ArgumentError)
end
it 'should generate an instance method to retrieve the humanization of any given value' do
song = @klass.new(:instruments => 'drums')
song.humanized_instrument('piano').should == 'Piano'
end
it 'should generate a class method to retrieve the humanization of any given value' do
@klass.humanized_instrument('piano').should == 'Piano'
end
it 'should generate an instance method to retrieve the humanizations of all current values' do
song = @klass.new
song.instruments = nil
song.humanized_instruments.should == nil
song.instruments = []
song.humanized_instruments.should == []
song.instruments = ['piano', 'drums']
song.humanized_instruments.should == ['Piano', 'Drums']
end
end
end
context 'if the :allow_blank option is set to true' do
before :each do
@klass = Song.disposable_copy do
store_accessor :metadata, :format
assignable_values_for :format, :allow_blank => true do
%w[mp3 wav]
end
end
end
it 'should allow nil for the attribute value' do
@klass.new(:format => nil).should be_valid
end
it 'should allow an empty string as value' do
@klass.new(:format => '').should be_valid
end
end
context 'if the :allow_blank option is set to a symbol that refers to an instance method' do
before :each do
@klass = Song.disposable_copy do
store_accessor :metadata, :format
attr_accessor :format_may_be_blank
assignable_values_for :format, :allow_blank => :format_may_be_blank do
%w[mp3 wav]
end
end
end
it 'should call that method to determine if a blank value is allowed' do
@klass.new(:format => '', :format_may_be_blank => true).should be_valid
@klass.new(:format => '', :format_may_be_blank => false).should_not be_valid
end
end
context 'if the :allow_blank option is set to a lambda' do
before :each do
@klass = Song.disposable_copy do
store_accessor :metadata, :format
attr_accessor :format_may_be_blank
assignable_values_for :format, :allow_blank => lambda { format_may_be_blank } do
%w[mp3 wav]
end
end
end
it 'should evaluate that lambda in the record context to determine if a blank value is allowed' do
@klass.new(:format => '', :format_may_be_blank => true).should be_valid
@klass.new(:format => '', :format_may_be_blank => false).should_not be_valid
end
end
context 'if the :message option is set to a string' do
before :each do
@klass = Song.disposable_copy do
store_accessor :metadata, :format
assignable_values_for :format, :message => 'should be something different' do
%w[mp3 wav]
end
end
end
it 'should use this string as a custom error message' do
record = @klass.new(:format => 'disallowed value')
record.valid?
errors = record.errors[:format]
error = errors.respond_to?(:first) ? errors.first : errors # the return value sometimes was a string, sometimes an Array in Rails
error.should == 'should be something different'
end
end
end
context 'when validating belongs_to associations' do
it 'should validate that the association is allowed' do
allowed_association = Artist.create!
disallowed_association = Artist.create!
klass = Song.disposable_copy do
assignable_values_for :artist do
[allowed_association]
end
end
klass.new(:artist => allowed_association).should be_valid
klass.new(:artist => disallowed_association).should_not be_valid
end
it 'should attach errors to the foreign key of the association, not the association itself ' do
allowed_association = Artist.create!
disallowed_association = Artist.create!
klass = Song.disposable_copy do
assignable_values_for :artist do
[allowed_association]
end
end
record = klass.new(:artist => disallowed_association)
record.valid?
errors = record.errors[:artist_id]
error = errors.respond_to?(:first) ? errors.first : errors # the return value sometimes was a string, sometimes an Array in Rails
error.should == I18n.t('errors.messages.inclusion')
end
it 'uses the defined foreign key of the association' do
klass = Song.disposable_copy do
belongs_to :creator, :foreign_key => 'artist_id', :class_name => 'Artist'
assignable_values_for :creator do
[]
end
end
song = klass.new(:creator => Artist.new)
song.valid?
song.errors[:artist_id].should be_present
end
it 'should allow a nil association if the :allow_blank option is set' do
klass = Song.disposable_copy do
assignable_values_for :artist, :allow_blank => true do
[]
end
end
record = klass.new
record.artist.should be_nil
record.should be_valid
end
it 'should allow a previously saved association, even if that association is no longer allowed' do
allowed_association = Artist.create!
disallowed_association = Artist.create!
klass = Song.disposable_copy
record = klass.create!(:artist => disallowed_association)
klass.class_eval do
assignable_values_for :artist do
[allowed_association]
end
end
record.should be_valid
end
it 'should not request the list of assignable values during validation if the association has not changed' do
allowed_association = Artist.create!
klass = Song.disposable_copy
record = klass.create!(:artist => allowed_association)
request_count = 0
klass.class_eval do
assignable_values_for :artist do
request_count += 1
[allowed_association]
end
end
record.reload
request_count.should == 0
record.year = 1975 # change any other attribute to make the record dirty
record.valid?
request_count.should == 0
end
it 'should allow nil for an association if the record was saved before with a nil association' do
allowed_association = Artist.create!
klass = Song.disposable_copy
record = klass.create!(:artist => nil)
klass.class_eval do
assignable_values_for :artist do
[allowed_association]
end
end
record.should be_valid
end
it 'should not allow nil for an association (the "previously saved value") if the record is new' do
allowed_association = Artist.create!
klass = Song.disposable_copy
record = klass.new(:artist => nil)
klass.class_eval do
assignable_values_for :artist do
[allowed_association]
end
end
record.should_not be_valid
end
it "should not load a previously saved association if the association's foreign key hasn't changed" do
association = Artist.create!
klass = Song.disposable_copy do
assignable_values_for :artist do
[association] # This example doesn't care about what's assignable. We're only interested in behavior up to the validation.
end
end
record = klass.create!(:artist => association)
Artist.should_not_receive(:find_by_id)
record.valid?
end
it 'should not fail or allow nil if a previously saved association no longer exists in the database' do
allowed_association = Artist.create!
klass = Song.disposable_copy do
assignable_values_for :artist do
[allowed_association]
end
end
record = klass.new
record.stub :artist_id_was => -1
record.should_not be_valid
end
it 'should uncache a stale association before validating' do
klass = Song.disposable_copy do
assignable_values_for :artist do
[] # This example doesn't care about what's assignable. We're only interested in behavior up to the validation.
end
end
association = Artist.create!
record = klass.new
record.stub(:artist => association, :artist_id => -1) # This is a stale association: The associated object's id doesn't match the foreign key. This can happen in Rails 2, not Rails 3.
record.should_receive(:artist).ordered.and_return(association)
record.valid?
end
it 'should not uncache a fresh association before validating' do
klass = Song.disposable_copy do
assignable_values_for :artist do
[] # This example doesn't care about what's assignable. We're only interested in behavior up to the validation.
end
end
association = Artist.create!
record = klass.new
record.stub(:artist => association, :artist_id => association.id) # This is a fresh association: The associated object's id matches the foreign key.
record.should_receive(:artist).with(no_args).and_return(association)
record.valid?
end
context 'when assignable values are provided as an ActiveRecord scope' do
MyArtist = Artist.disposable_copy
let(:klass) { Song.disposable_copy }
before do
klass.class_eval do
assignable_values_for :artist do
if ::ActiveRecord::VERSION::MAJOR < 4
MyArtist.scoped({})
else
MyArtist.all
end
end
end
end
it 'allows assigning a record from the scope' do
artist = MyArtist.create!
song = klass.new(:artist => artist)
song.should be_valid
end
it 'will not crash during validation when the assigned value is nil' do
song = klass.new
expect { song.valid? }.to_not raise_error
song.errors[:artist_id].should be_present
end
it 'should not load all records into memory' do
unless ::ActiveRecord::VERSION::MAJOR < 3 # somehow rails 2 still initializes Objects during the scope.exists?-call
initialized_artists_count = 0
MyArtist.class_eval do
after_initialize :increase_initialized_count
define_method :increase_initialized_count do
initialized_artists_count += 1
end
end
artist = MyArtist.create!
initialized_artists_count.should == 1
song = klass.new(:artist => artist)
song.valid?
initialized_artists_count.should == 1
song.assignable_artists
initialized_artists_count.should == 1
song.assignable_artists.to_a
initialized_artists_count.should == 2
end
end
end
end
context 'when delegating using the :through option' do
it 'should obtain allowed values from a method with the given name' do
klass = Song.disposable_copy do
assignable_values_for :genre, :through => :delegate
def delegate
Class.new do
def assignable_song_genres
%w[pop rock]
end
end.new
end
end
klass.new(:genre => 'pop').should be_valid
klass.new(:genre => 'disallowed value').should_not be_valid
end
it 'should be able to delegate to a lambda, which is evaluated in the context of the record instance' do
klass = Song.disposable_copy do
assignable_values_for :genre, :through => lambda { delegate }
def delegate
Class.new do
def assignable_song_genres
%w[pop rock]
end
end.new
end
end
klass.new(:genre => 'pop').should be_valid
klass.new(:genre => 'disallowed value').should_not be_valid
end
it 'should generate a legal getter name for a namespaced model (bugfix)' do
klass = Recording::Vinyl.disposable_copy do
assignable_values_for :year, :through => :delegate
def delegate
Class.new do
def assignable_recording_vinyl_years
[1977, 1980, 1983]
end
end.new
end
end
klass.new.assignable_years.should == [1977, 1980, 1983]
end
it 'should not request the list of assignable values during validation if the association has not changed' do
allowed_association = Artist.create!
klass = Song.disposable_copy
request_count = 0
delegate = Class.new do
define_method :assignable_song_artists do
request_count += 1
[allowed_association]
end
end.new
record = klass.create!(:artist => allowed_association)
klass.class_eval do
assignable_values_for :artist, :through => lambda { delegate }
end
request_count.should == 0
record.reload
record.year = 1975 # change any other attribute to make the record dirty
record.valid?
request_count.should == 0
end
context 'when the delegation method returns nil' do
let(:klass) do
Song.disposable_copy do
assignable_values_for :genre, :through => proc { nil }
end
end
it 'should skip the validation' do
klass.new(:genre => 'pop').should be_valid
end
it 'should still be able to humanize values on the instance' do
klass.new(:genre => 'pop').humanized_genre.should == 'Pop music'
end
end
end
context 'with :default option' do
it 'should allow to set a default' do
klass = Song.disposable_copy do
assignable_values_for :genre, :default => 'pop' do
%w[pop rock]
end
end
klass.new.genre.should == 'pop'
end
it 'should allow to set a default through a lambda' do
klass = Song.disposable_copy do
assignable_values_for :genre, :default => lambda { 'pop' } do
%w[pop rock]
end
end
klass.new.genre.should == 'pop'
end
it 'should evaluate a lambda default in the context of the record instance' do
klass = Song.disposable_copy do
assignable_values_for :genre, :default => lambda { default_genre } do
%w[pop rock]
end
def default_genre
'pop'
end
end
klass.new.genre.should == 'pop'
end
end
context 'with :secondary_default option' do
it 'should set a secondary default value if the primary value is not assignable' do
klass = Song.disposable_copy do
assignable_values_for :genre, :default => 'techno', :secondary_default => 'rock' do
%w[pop rock]
end
end
klass.new.genre.should == 'rock'
end
it 'should not change the default value if the default value is assignable' do
klass = Song.disposable_copy do
assignable_values_for :genre, :default => 'pop', :secondary_default => 'rock' do
%w[pop rock]
end
end
klass.new.genre.should == 'pop'
end
it "should not change the primary default if the secondary default value isn't assignable either" do
klass = Song.disposable_copy do
assignable_values_for :genre, :default => 'techno', :secondary_default => 'jazz' do
%w[pop rock]
end
end
klass.new.genre.should == 'techno'
end
it 'should raise an error if used without a :default option' do
expect do
Song.disposable_copy do
assignable_values_for :genre, :secondary_default => 'pop' do
%w[pop rock]
end
end
end.to raise_error(AssignableValues::NoDefault)
end
it 'should allow to set a secondary default through a lambda' do
klass = Song.disposable_copy do
assignable_values_for :genre, :default => 'techno', :secondary_default => lambda { 'pop' } do
%w[pop rock]
end
end
klass.new.genre.should == 'pop'
end
it 'should evaluate a secondary lambda default in the context of the record instance' do
klass = Song.disposable_copy do
assignable_values_for :genre, :default => 'techno', :secondary_default => lambda { default_genre } do
%w[pop rock]
end
def default_genre
'pop'
end
end
klass.new.genre.should == 'pop'
end
it "should not raise an error or change the primary default if assignable values are retrieved through a delegate, and the delegate is nil" do
klass = Song.disposable_copy do
assignable_values_for :genre, :default => 'techno', :secondary_default => 'pop', :through => lambda { nil }
end
expect do
klass.new.genre.should == 'techno'
end.to_not raise_error
end
it 'should not cause the list of assignable values to be evaluated if the :secondary_default option is not used' do
klass = Song.disposable_copy do
assignable_values_for :genre, :default => 'techno' do
raise "block called!"
end
end
expect do
klass.new.genre.should == 'techno'