assignable_values 1.0.1 → 1.1.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.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +1 -1
- data/CHANGELOG.md +6 -0
- data/Gemfile.5.0.lock +1 -1
- data/Gemfile.5.1.lock +1 -1
- data/Gemfile.5.1.pg.lock +1 -1
- data/Gemfile.6.1.pg.lock +1 -1
- data/Gemfile.7.1.pg.lock +1 -1
- data/README.md +18 -2
- data/lib/assignable_values/active_record/restriction/scalar_attribute.rb +8 -8
- data/lib/assignable_values/active_record/restriction/store_accessor_attribute.rb +29 -0
- data/lib/assignable_values/active_record.rb +16 -1
- data/lib/assignable_values/version.rb +1 -1
- data/lib/assignable_values.rb +1 -0
- data/media/logo.dark.shapes.svg +169 -0
- data/media/logo.dark.text.svg +107 -0
- data/media/logo.light.shapes.svg +168 -0
- data/media/logo.light.text.svg +106 -0
- data/media/makandra-with-bottom-margin.dark.svg +180 -0
- data/media/makandra-with-bottom-margin.light.svg +180 -0
- data/spec/assignable_values/active_record_spec.rb +209 -2
- data/spec/support/database.rb +1 -0
- data/spec/support/i18n.yml +7 -0
- metadata +9 -2
@@ -146,7 +146,6 @@ describe AssignableValues::ActiveRecord do
|
|
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 @@ describe AssignableValues::ActiveRecord do
|
|
261
260
|
|
262
261
|
end
|
263
262
|
|
264
|
-
context 'if the :allow_blank option is set to a lambda
|
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 @@ describe AssignableValues::ActiveRecord do
|
|
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
|
data/spec/support/database.rb
CHANGED
data/spec/support/i18n.yml
CHANGED
@@ -9,9 +9,16 @@ en:
|
|
9
9
|
|
10
10
|
assignable_values:
|
11
11
|
song:
|
12
|
+
format:
|
13
|
+
mp3: 'MP3-Codec'
|
14
|
+
wav: 'WAV-Codec'
|
12
15
|
genre:
|
13
16
|
pop: 'Pop music'
|
14
17
|
rock: 'Rock music'
|
18
|
+
instruments:
|
19
|
+
drums: 'Drums'
|
20
|
+
guitar: 'Guitar'
|
21
|
+
piano: 'Piano'
|
15
22
|
multi_genres:
|
16
23
|
pop: 'Pop music'
|
17
24
|
rock: 'Rock music'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: assignable_values
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henning Koch
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-12-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -56,10 +56,17 @@ files:
|
|
56
56
|
- lib/assignable_values/active_record/restriction/base.rb
|
57
57
|
- lib/assignable_values/active_record/restriction/belongs_to_association.rb
|
58
58
|
- lib/assignable_values/active_record/restriction/scalar_attribute.rb
|
59
|
+
- lib/assignable_values/active_record/restriction/store_accessor_attribute.rb
|
59
60
|
- lib/assignable_values/errors.rb
|
60
61
|
- lib/assignable_values/humanizable_string.rb
|
61
62
|
- lib/assignable_values/humanized_value.rb
|
62
63
|
- lib/assignable_values/version.rb
|
64
|
+
- media/logo.dark.shapes.svg
|
65
|
+
- media/logo.dark.text.svg
|
66
|
+
- media/logo.light.shapes.svg
|
67
|
+
- media/logo.light.text.svg
|
68
|
+
- media/makandra-with-bottom-margin.dark.svg
|
69
|
+
- media/makandra-with-bottom-margin.light.svg
|
63
70
|
- spec/assignable_values/active_record_spec.rb
|
64
71
|
- spec/assignable_values/humanized_value_spec.rb
|
65
72
|
- spec/spec_helper.rb
|