assignable_values 0.14.0 → 0.15.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -2
- data/README.md +12 -5
- data/gemfiles/Gemfile.2.3.lock +1 -1
- data/gemfiles/Gemfile.3.2.lock +1 -1
- data/gemfiles/Gemfile.4.2.lock +1 -1
- data/gemfiles/Gemfile.5.0.lock +1 -1
- data/gemfiles/Gemfile.5.1.lock +1 -1
- data/gemfiles/Gemfile.5.1.pg.lock +1 -1
- data/lib/assignable_values.rb +1 -0
- data/lib/assignable_values/active_record/restriction/scalar_attribute.rb +18 -6
- data/lib/assignable_values/humanizable_string.rb +2 -1
- data/lib/assignable_values/version.rb +1 -1
- data/spec/assignable_values/active_record_spec.rb +38 -13
- data/spec/support/i18n.yml +3 -0
- data/spec/support/models.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d0359f423877ba1c7d9b303053434f7d80077da0cf314a964b6577f31b97fd01
|
4
|
+
data.tar.gz: e3818b1b35b173d4db2a896092a7cf0ee7f8d93381631cd1cfbbf62f64fc6783
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 479c5751e8086031feadcc70a06b878d04020f5fc023b69afd790c2a684f219d56e6d44abfa31387aa342a5c08b4ecfdcea6da085bc9060685ac04750fef3fba
|
7
|
+
data.tar.gz: c38e04dc6003f5f3ab055eeb2b4a9183351c4a7101da42750bd381263e190cdbca0e9638c5e44a88e6510f6aa741eac1612285b1bbfb5347dbb599fdefa95388
|
data/CHANGELOG.md
CHANGED
@@ -6,11 +6,18 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html
|
|
6
6
|
|
7
7
|
### Breaking changes
|
8
8
|
|
9
|
-
|
9
|
+
### Compatible changes
|
10
|
+
|
11
|
+
|
12
|
+
## 0.15.0 - 2018-10-26
|
13
|
+
|
14
|
+
### Breaking changes
|
15
|
+
|
16
|
+
- `#humanized_values` is deprecated, in favour of `#humanized_assignablevalues`
|
10
17
|
|
11
18
|
### Compatible changes
|
12
19
|
|
13
|
-
-
|
20
|
+
- `#humanized_value(value)` and `#humanized_assignable_values` now also works for the `multiple: true` case
|
14
21
|
|
15
22
|
|
16
23
|
## 0.14.0 - 2018-09-17
|
data/README.md
CHANGED
@@ -65,13 +65,13 @@ Or you can retrieve the humanized version of any given value by passing it as an
|
|
65
65
|
|
66
66
|
You can obtain a list of all assignable values with their humanizations:
|
67
67
|
|
68
|
-
song.
|
69
|
-
song.
|
70
|
-
song.
|
68
|
+
song.humanized_assignable_genres.size # => 3
|
69
|
+
song.humanized_assignable_genres.first.value # => "pop"
|
70
|
+
song.humanized_assignable_genres.first.humanized # => "Pop music"
|
71
71
|
|
72
72
|
A good way to populate a `<select>` tag with pairs of internal values and human labels is to use the `collection_select` helper from Rails:
|
73
73
|
|
74
|
-
form.collection_select :genre, form.object.
|
74
|
+
form.collection_select :genre, form.object.humanized_assignable_genres, :value, :humanized
|
75
75
|
|
76
76
|
|
77
77
|
### Defining default values
|
@@ -166,7 +166,7 @@ This is to prevent records from becoming invalid as the list of assignable value
|
|
166
166
|
|
167
167
|
### Array values
|
168
168
|
|
169
|
-
Assignable values can also be used for array values. This works when you use Rails 5+ and PostgreSQL with an array column, or
|
169
|
+
Assignable values can also be used for array values. This works when you use Rails 5+ and PostgreSQL with an array column, or on older Rails versions and other databases using ActiveRecord's `serialize`.
|
170
170
|
|
171
171
|
To validate array values, pass `multiple: true`:
|
172
172
|
|
@@ -182,6 +182,13 @@ end
|
|
182
182
|
|
183
183
|
In this case, every *subset* of the given values is valid, for example `['pop', 'electronic']`.
|
184
184
|
|
185
|
+
For humanization, you can still use
|
186
|
+
|
187
|
+
```
|
188
|
+
song.humanizable_genre('pop') # => "Pop music"
|
189
|
+
song.assignable_humanizable_genres.last.humanized # => "Electronic music"
|
190
|
+
```
|
191
|
+
|
185
192
|
|
186
193
|
Restricting belongs_to associations
|
187
194
|
-----------------------------------
|
data/gemfiles/Gemfile.2.3.lock
CHANGED
data/gemfiles/Gemfile.3.2.lock
CHANGED
data/gemfiles/Gemfile.4.2.lock
CHANGED
data/gemfiles/Gemfile.5.0.lock
CHANGED
data/gemfiles/Gemfile.5.1.lock
CHANGED
data/lib/assignable_values.rb
CHANGED
@@ -7,7 +7,7 @@ module AssignableValues
|
|
7
7
|
super
|
8
8
|
define_humanized_value_instance_method
|
9
9
|
define_humanized_value_class_method
|
10
|
-
|
10
|
+
define_humanized_assignable_values_instance_method
|
11
11
|
end
|
12
12
|
|
13
13
|
def humanized_value(value)
|
@@ -17,7 +17,7 @@ module AssignableValues
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
def
|
20
|
+
def humanized_assignable_values(record)
|
21
21
|
values = assignable_values(record)
|
22
22
|
values.collect do |value|
|
23
23
|
HumanizedValue.new(value, humanized_value(value))
|
@@ -45,8 +45,12 @@ module AssignableValues
|
|
45
45
|
|
46
46
|
def define_humanized_value_instance_method
|
47
47
|
restriction = self
|
48
|
+
multiple = @options[:multiple]
|
48
49
|
enhance_model do
|
49
|
-
define_method :"humanized_#{restriction.property}" do |*args|
|
50
|
+
define_method :"humanized_#{restriction.property.to_s.singularize}" do |*args|
|
51
|
+
if args.size > 1 || (multiple && args.size == 0)
|
52
|
+
raise ArgumentError.new("wrong number of arguments (#{args.size} for #{multiple ? '1' : '0..1'})")
|
53
|
+
end
|
50
54
|
given_value = args[0]
|
51
55
|
value = given_value || send(restriction.property)
|
52
56
|
restriction.humanized_value(value)
|
@@ -54,11 +58,19 @@ module AssignableValues
|
|
54
58
|
end
|
55
59
|
end
|
56
60
|
|
57
|
-
def
|
61
|
+
def define_humanized_assignable_values_instance_method
|
58
62
|
restriction = self
|
63
|
+
multiple = @options[:multiple]
|
59
64
|
enhance_model do
|
60
|
-
define_method :"
|
61
|
-
restriction.
|
65
|
+
define_method :"humanized_assignable_#{restriction.property.to_s.pluralize}" do
|
66
|
+
restriction.humanized_assignable_values(self)
|
67
|
+
end
|
68
|
+
|
69
|
+
unless multiple
|
70
|
+
define_method :"humanized_#{restriction.property.to_s.pluralize}" do
|
71
|
+
ActiveSupport::Deprecation.warn("humanized_<value>s is deprecated, use humanized_assignable_<value>s instead", caller)
|
72
|
+
restriction.humanized_assignable_values(self)
|
73
|
+
end
|
62
74
|
end
|
63
75
|
end
|
64
76
|
end
|
@@ -20,6 +20,10 @@ describe AssignableValues::ActiveRecord do
|
|
20
20
|
assignable_values_for :sub_genre do
|
21
21
|
%w[pop rock]
|
22
22
|
end
|
23
|
+
|
24
|
+
assignable_values_for :multi_genres, :allow_blank => true do
|
25
|
+
%w[pop rock]
|
26
|
+
end
|
23
27
|
end
|
24
28
|
end
|
25
29
|
|
@@ -35,6 +39,8 @@ describe AssignableValues::ActiveRecord do
|
|
35
39
|
it 'should generate a method returning the humanized value' do
|
36
40
|
song = @klass.new(:sub_genre => 'pop')
|
37
41
|
song.humanized_sub_genre.should == 'Pop music'
|
42
|
+
song.humanized_sub_genre('rock').should == 'Rock music'
|
43
|
+
song.humanized_multi_genre('rock').should == 'Rock music'
|
38
44
|
end
|
39
45
|
|
40
46
|
end
|
@@ -45,21 +51,21 @@ describe AssignableValues::ActiveRecord do
|
|
45
51
|
|
46
52
|
before :each do
|
47
53
|
@klass = Song.disposable_copy do
|
48
|
-
assignable_values_for :
|
54
|
+
assignable_values_for :multi_genres, :multiple => true do
|
49
55
|
%w[pop rock]
|
50
56
|
end
|
51
57
|
end
|
52
58
|
end
|
53
59
|
|
54
60
|
it 'should validate that the attribute is a subset' do
|
55
|
-
@klass.new(:
|
56
|
-
@klass.new(:
|
57
|
-
@klass.new(:
|
61
|
+
@klass.new(:multi_genres => ['pop']).should be_valid
|
62
|
+
@klass.new(:multi_genres => ['pop', 'rock']).should be_valid
|
63
|
+
@klass.new(:multi_genres => ['pop', 'disallowed value']).should_not be_valid
|
58
64
|
end
|
59
65
|
|
60
66
|
it 'should not allow nil or [] for allow_blank: false' do
|
61
|
-
@klass.new(:
|
62
|
-
@klass.new(:
|
67
|
+
@klass.new(:multi_genres => nil).should_not be_valid
|
68
|
+
@klass.new(:multi_genres => []).should_not be_valid
|
63
69
|
end
|
64
70
|
|
65
71
|
end
|
@@ -68,15 +74,15 @@ describe AssignableValues::ActiveRecord do
|
|
68
74
|
|
69
75
|
before :each do
|
70
76
|
@klass = Song.disposable_copy do
|
71
|
-
assignable_values_for :
|
77
|
+
assignable_values_for :multi_genres, :multiple => true, :allow_blank => true do
|
72
78
|
%w[pop rock]
|
73
79
|
end
|
74
80
|
end
|
75
81
|
end
|
76
82
|
|
77
83
|
it 'should allow nil or [] for allow_blank: false' do
|
78
|
-
@klass.new(:
|
79
|
-
@klass.new(:
|
84
|
+
@klass.new(:multi_genres => nil).should be_valid
|
85
|
+
@klass.new(:multi_genres => []).should be_valid
|
80
86
|
end
|
81
87
|
|
82
88
|
end
|
@@ -791,11 +797,18 @@ describe AssignableValues::ActiveRecord do
|
|
791
797
|
assignable_values_for :genre do
|
792
798
|
%w[pop rock]
|
793
799
|
end
|
800
|
+
|
801
|
+
assignable_values_for :multi_genres, :multiple => true do
|
802
|
+
%w[pop rock]
|
803
|
+
end
|
794
804
|
end
|
795
|
-
genres = klass.new.
|
805
|
+
genres = klass.new.humanized_assignable_genres
|
796
806
|
genres.collect(&:value).should == ['pop', 'rock']
|
797
807
|
genres.collect(&:humanized).should == ['Pop music', 'Rock music']
|
798
808
|
genres.collect(&:to_s).should == ['Pop music', 'Rock music']
|
809
|
+
|
810
|
+
multi_genres = klass.new.humanized_assignable_multi_genres
|
811
|
+
multi_genres.collect(&:humanized).should == ['Pop music', 'Rock music']
|
799
812
|
end
|
800
813
|
|
801
814
|
it 'should use String#humanize as a default translation' do
|
@@ -804,7 +817,7 @@ describe AssignableValues::ActiveRecord do
|
|
804
817
|
%w[electronic]
|
805
818
|
end
|
806
819
|
end
|
807
|
-
klass.new.
|
820
|
+
klass.new.humanized_assignable_genres.collect(&:humanized).should == ['Electronic']
|
808
821
|
end
|
809
822
|
|
810
823
|
it 'should allow to define humanizations for values that are not strings' do
|
@@ -813,7 +826,7 @@ describe AssignableValues::ActiveRecord do
|
|
813
826
|
[1977, 1980, 1983]
|
814
827
|
end
|
815
828
|
end
|
816
|
-
years = klass.new.
|
829
|
+
years = klass.new.humanized_assignable_years
|
817
830
|
years.collect(&:value).should == [1977, 1980, 1983]
|
818
831
|
years.collect(&:humanized).should == ['The year a new hope was born', 'The year the Empire stroke back', 'The year the Jedi returned']
|
819
832
|
end
|
@@ -824,18 +837,30 @@ describe AssignableValues::ActiveRecord do
|
|
824
837
|
[1977, 1980, 1983]
|
825
838
|
end
|
826
839
|
end
|
827
|
-
years = klass.new.
|
840
|
+
years = klass.new.humanized_assignable_years
|
828
841
|
years.collect(&:humanized).should == ['The year a new hope was born', 'The year the Empire stroke back', 'The year the Jedi returned']
|
829
842
|
end
|
830
843
|
|
831
844
|
context 'legacy methods for API compatibility' do
|
832
845
|
|
846
|
+
it 'should define a method that return pairs of values and their humanization' do
|
847
|
+
klass = Song.disposable_copy do
|
848
|
+
assignable_values_for :genre do
|
849
|
+
%w[pop rock]
|
850
|
+
end
|
851
|
+
end
|
852
|
+
ActiveSupport::Deprecation.should_receive(:warn)
|
853
|
+
genres = klass.new.humanized_genres
|
854
|
+
genres.collect(&:humanized).should == ['Pop music', 'Rock music']
|
855
|
+
end
|
856
|
+
|
833
857
|
it "should define a method #humanized on assignable string values, which return up the value's' translation" do
|
834
858
|
klass = Song.disposable_copy do
|
835
859
|
assignable_values_for :genre do
|
836
860
|
%w[pop rock]
|
837
861
|
end
|
838
862
|
end
|
863
|
+
ActiveSupport::Deprecation.should_receive(:warn).at_least(:once)
|
839
864
|
klass.new.assignable_genres.collect(&:humanized).should == ['Pop music', 'Rock music']
|
840
865
|
end
|
841
866
|
|
data/spec/support/i18n.yml
CHANGED
data/spec/support/models.rb
CHANGED
@@ -8,7 +8,7 @@ class Song < ActiveRecord::Base
|
|
8
8
|
|
9
9
|
belongs_to :artist
|
10
10
|
|
11
|
-
attr_accessor :sub_genre, :sub_genres
|
11
|
+
attr_accessor :sub_genre, :sub_genres, :multi_genres
|
12
12
|
|
13
13
|
if ActiveRecord::VERSION::MAJOR < 4 || !Song.new(:genres => ['test']).genres.is_a?(Array)
|
14
14
|
# Rails 4 or not postgres
|
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: 0.
|
4
|
+
version: 0.15.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: 2018-
|
11
|
+
date: 2018-10-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -90,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
90
|
version: '0'
|
91
91
|
requirements: []
|
92
92
|
rubyforge_project:
|
93
|
-
rubygems_version: 2.7.
|
93
|
+
rubygems_version: 2.7.7
|
94
94
|
signing_key:
|
95
95
|
specification_version: 4
|
96
96
|
summary: Restrict the values assignable to ActiveRecord attributes or associations
|