assignable_values 0.16.6 → 0.17.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 +7 -0
- data/Gemfile.5.0 +1 -1
- data/Gemfile.5.0.lock +3 -3
- data/Gemfile.5.1 +1 -1
- data/Gemfile.5.1.lock +3 -3
- data/Gemfile.5.1.pg +1 -1
- data/Gemfile.5.1.pg.lock +3 -3
- data/Gemfile.6.1.pg +1 -1
- data/Gemfile.6.1.pg.lock +3 -3
- data/Gemfile.7.0.pg +1 -1
- data/Gemfile.7.0.pg.lock +3 -3
- data/README.md +18 -0
- data/lib/assignable_values/active_record/restriction/base.rb +3 -3
- data/lib/assignable_values/active_record/restriction/scalar_attribute.rb +16 -11
- data/lib/assignable_values/version.rb +1 -1
- data/spec/assignable_values/active_record_spec.rb +32 -2
- data/spec/support/i18n.yml +7 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8059afed1809ca7277b0db60d77f076f92746d3558bf06ed1713feb6961b053e
|
4
|
+
data.tar.gz: 11c933835b82953cc8576c99f23eebf6d686406be75a8566e27a067bb8dd0686
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6aa60676ebd5d1d21d6b64b0f1df342d3e1cafc540671d0321ed3f0d599b57a5b982d3a736d6b2922c263af5bb362da0e4ffecdb438ec5c547e755ecd36e6c4b
|
7
|
+
data.tar.gz: b67a535cb382ffe4f1996b919fc91440a97e758a839d87be98b6df50bffdb6bbf258b733fbceae5c888f05eecfe2327adc41690e101dc6e8a02c9220b4779386
|
data/CHANGELOG.md
CHANGED
@@ -12,11 +12,18 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html
|
|
12
12
|
|
13
13
|
-
|
14
14
|
|
15
|
+
## 0.17.0 - 2022-07-27
|
16
|
+
|
17
|
+
### Compatible changes
|
18
|
+
|
19
|
+
- Allow to override humanization for inherited assignable_values
|
20
|
+
|
15
21
|
## 0.16.6 - 2022-03-16
|
16
22
|
|
17
23
|
### Breaking changes
|
18
24
|
|
19
25
|
- Remove no longer supported ruby versions (2.3.8, 2.4.2)
|
26
|
+
- Activate rubygems MFA
|
20
27
|
|
21
28
|
### Compatible changes
|
22
29
|
|
data/Gemfile.5.0
CHANGED
data/Gemfile.5.0.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
assignable_values (0.
|
4
|
+
assignable_values (0.17.0)
|
5
5
|
activerecord (>= 2.3)
|
6
6
|
|
7
7
|
GEM
|
@@ -22,7 +22,7 @@ GEM
|
|
22
22
|
concurrent-ruby (1.1.7)
|
23
23
|
database_cleaner (1.8.5)
|
24
24
|
diff-lcs (1.4.4)
|
25
|
-
gemika (0.
|
25
|
+
gemika (0.7.1)
|
26
26
|
i18n (1.8.5)
|
27
27
|
concurrent-ruby (~> 1.0)
|
28
28
|
minitest (5.14.2)
|
@@ -57,7 +57,7 @@ DEPENDENCIES
|
|
57
57
|
activerecord (~> 5.0.0)
|
58
58
|
assignable_values!
|
59
59
|
database_cleaner
|
60
|
-
gemika
|
60
|
+
gemika
|
61
61
|
i18n
|
62
62
|
mysql2
|
63
63
|
rake
|
data/Gemfile.5.1
CHANGED
data/Gemfile.5.1.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
assignable_values (0.
|
4
|
+
assignable_values (0.17.0)
|
5
5
|
activerecord (>= 2.3)
|
6
6
|
|
7
7
|
GEM
|
@@ -22,7 +22,7 @@ GEM
|
|
22
22
|
concurrent-ruby (1.1.7)
|
23
23
|
database_cleaner (1.8.5)
|
24
24
|
diff-lcs (1.4.4)
|
25
|
-
gemika (0.
|
25
|
+
gemika (0.7.1)
|
26
26
|
i18n (1.8.5)
|
27
27
|
concurrent-ruby (~> 1.0)
|
28
28
|
minitest (5.14.2)
|
@@ -57,7 +57,7 @@ DEPENDENCIES
|
|
57
57
|
activerecord (~> 5.1.0)
|
58
58
|
assignable_values!
|
59
59
|
database_cleaner
|
60
|
-
gemika
|
60
|
+
gemika
|
61
61
|
i18n
|
62
62
|
mysql2
|
63
63
|
rake
|
data/Gemfile.5.1.pg
CHANGED
data/Gemfile.5.1.pg.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
assignable_values (0.
|
4
|
+
assignable_values (0.17.0)
|
5
5
|
activerecord (>= 2.3)
|
6
6
|
|
7
7
|
GEM
|
@@ -22,7 +22,7 @@ GEM
|
|
22
22
|
concurrent-ruby (1.0.5)
|
23
23
|
database_cleaner (1.6.1)
|
24
24
|
diff-lcs (1.3)
|
25
|
-
gemika (0.
|
25
|
+
gemika (0.7.1)
|
26
26
|
i18n (0.9.0)
|
27
27
|
concurrent-ruby (~> 1.0)
|
28
28
|
minitest (5.10.3)
|
@@ -57,7 +57,7 @@ DEPENDENCIES
|
|
57
57
|
activerecord (~> 5.1.0)
|
58
58
|
assignable_values!
|
59
59
|
database_cleaner
|
60
|
-
gemika
|
60
|
+
gemika
|
61
61
|
i18n
|
62
62
|
pg (< 1)
|
63
63
|
rake
|
data/Gemfile.6.1.pg
CHANGED
data/Gemfile.6.1.pg.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
assignable_values (0.
|
4
|
+
assignable_values (0.17.0)
|
5
5
|
activerecord (>= 2.3)
|
6
6
|
|
7
7
|
GEM
|
@@ -21,7 +21,7 @@ GEM
|
|
21
21
|
concurrent-ruby (1.1.5)
|
22
22
|
database_cleaner (1.7.0)
|
23
23
|
diff-lcs (1.3)
|
24
|
-
gemika (0.
|
24
|
+
gemika (0.7.1)
|
25
25
|
i18n (1.6.0)
|
26
26
|
concurrent-ruby (~> 1.0)
|
27
27
|
minitest (5.14.4)
|
@@ -56,7 +56,7 @@ DEPENDENCIES
|
|
56
56
|
activerecord (~> 6.1.3)
|
57
57
|
assignable_values!
|
58
58
|
database_cleaner
|
59
|
-
gemika
|
59
|
+
gemika
|
60
60
|
i18n
|
61
61
|
pg (= 1.1)
|
62
62
|
rake
|
data/Gemfile.7.0.pg
CHANGED
data/Gemfile.7.0.pg.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
assignable_values (0.
|
4
|
+
assignable_values (0.17.0)
|
5
5
|
activerecord (>= 2.3)
|
6
6
|
|
7
7
|
GEM
|
@@ -20,7 +20,7 @@ GEM
|
|
20
20
|
concurrent-ruby (1.1.5)
|
21
21
|
database_cleaner (1.7.0)
|
22
22
|
diff-lcs (1.3)
|
23
|
-
gemika (0.
|
23
|
+
gemika (0.7.1)
|
24
24
|
i18n (1.6.0)
|
25
25
|
concurrent-ruby (~> 1.0)
|
26
26
|
minitest (5.15.0)
|
@@ -54,7 +54,7 @@ DEPENDENCIES
|
|
54
54
|
activerecord (~> 7.0.0)
|
55
55
|
assignable_values!
|
56
56
|
database_cleaner
|
57
|
-
gemika
|
57
|
+
gemika
|
58
58
|
i18n
|
59
59
|
pg (= 1.1)
|
60
60
|
rake
|
data/README.md
CHANGED
@@ -73,6 +73,24 @@ A good way to populate a `<select>` tag with pairs of internal values and human
|
|
73
73
|
|
74
74
|
form.collection_select :genre, form.object.humanized_assignable_genres, :value, :humanized
|
75
75
|
|
76
|
+
#### Humanized labels and inheritance
|
77
|
+
|
78
|
+
For models that inherit assignable values you can override the humanized labels:
|
79
|
+
|
80
|
+
class FunnySong < Song
|
81
|
+
...
|
82
|
+
end
|
83
|
+
|
84
|
+
en:
|
85
|
+
assignable_values:
|
86
|
+
funny_song:
|
87
|
+
genre:
|
88
|
+
pop: 'The stuff you hear on mainstream radio all day long'
|
89
|
+
rock: 'A lot of electric guitars and drums'
|
90
|
+
electronic: 'Whatever David Guetta does'
|
91
|
+
|
92
|
+
If no humanization is provided for the child model (i.e. the `funny_song.genre` key) humanization will fall back to the
|
93
|
+
parent model (`song`).
|
76
94
|
|
77
95
|
### Defining default values
|
78
96
|
|
@@ -63,8 +63,8 @@ module AssignableValues
|
|
63
63
|
end
|
64
64
|
|
65
65
|
if options[:decorate]
|
66
|
-
current_values = decorate_values(current_values)
|
67
|
-
additional_assignable_values = decorate_values(additional_assignable_values)
|
66
|
+
current_values = decorate_values(current_values, record.class)
|
67
|
+
additional_assignable_values = decorate_values(additional_assignable_values, record.class)
|
68
68
|
end
|
69
69
|
|
70
70
|
if additional_assignable_values.present?
|
@@ -146,7 +146,7 @@ module AssignableValues
|
|
146
146
|
raise NotImplementedError
|
147
147
|
end
|
148
148
|
|
149
|
-
def decorate_values(values)
|
149
|
+
def decorate_values(values, _klass)
|
150
150
|
values
|
151
151
|
end
|
152
152
|
|
@@ -10,22 +10,29 @@ module AssignableValues
|
|
10
10
|
define_humanized_assignable_values_instance_method
|
11
11
|
end
|
12
12
|
|
13
|
-
def humanized_value(value)
|
13
|
+
def humanized_value(klass, value)
|
14
14
|
if value.present?
|
15
|
-
|
16
|
-
I18n.t(value, :scope => dictionary_scope, :default => default_humanization_for_value(value))
|
15
|
+
humanization_from_i18n(klass, value) || default_humanization_for_value(value)
|
17
16
|
end
|
18
17
|
end
|
19
18
|
|
20
19
|
def humanized_assignable_values(record, options = {})
|
21
20
|
values = assignable_values(record, options)
|
22
21
|
values.collect do |value|
|
23
|
-
HumanizedValue.new(value, humanized_value(value))
|
22
|
+
HumanizedValue.new(value, humanized_value(record.class, value))
|
24
23
|
end
|
25
24
|
end
|
26
25
|
|
27
26
|
private
|
28
27
|
|
28
|
+
def humanization_from_i18n(klass, value)
|
29
|
+
klass.lookup_ancestors.select(&:name).find do |klass|
|
30
|
+
dictionary_scope = :"assignable_values.#{klass.model_name.i18n_key}.#{property}"
|
31
|
+
translation = I18n.translate(value, scope: dictionary_scope, default: nil)
|
32
|
+
break translation unless translation.nil?
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
29
36
|
def default_humanization_for_value(value)
|
30
37
|
if value.is_a?(String)
|
31
38
|
value.humanize
|
@@ -38,7 +45,7 @@ module AssignableValues
|
|
38
45
|
restriction = self
|
39
46
|
enhance_model_singleton do
|
40
47
|
define_method :"humanized_#{restriction.property.to_s.singularize}" do |given_value|
|
41
|
-
restriction.humanized_value(given_value)
|
48
|
+
restriction.humanized_value(self, given_value)
|
42
49
|
end
|
43
50
|
end
|
44
51
|
end
|
@@ -53,7 +60,7 @@ module AssignableValues
|
|
53
60
|
end
|
54
61
|
given_value = args[0]
|
55
62
|
value = given_value || send(restriction.property)
|
56
|
-
restriction.humanized_value(value)
|
63
|
+
restriction.humanized_value(self.class, value)
|
57
64
|
end
|
58
65
|
|
59
66
|
if multiple
|
@@ -61,7 +68,7 @@ module AssignableValues
|
|
61
68
|
values = send(restriction.property)
|
62
69
|
if values.respond_to?(:map)
|
63
70
|
values.map do |value|
|
64
|
-
restriction.humanized_value(value)
|
71
|
+
restriction.humanized_value(self.class, value)
|
65
72
|
end
|
66
73
|
else
|
67
74
|
values
|
@@ -88,11 +95,11 @@ module AssignableValues
|
|
88
95
|
end
|
89
96
|
end
|
90
97
|
|
91
|
-
def decorate_values(values)
|
98
|
+
def decorate_values(values, klass)
|
92
99
|
restriction = self
|
93
100
|
values.collect do |value|
|
94
101
|
if value.is_a?(String)
|
95
|
-
humanization = restriction.humanized_value(value)
|
102
|
+
humanization = restriction.humanized_value(klass, value)
|
96
103
|
value = HumanizableString.new(value, humanization)
|
97
104
|
end
|
98
105
|
value
|
@@ -111,8 +118,6 @@ module AssignableValues
|
|
111
118
|
value_was(record)
|
112
119
|
end
|
113
120
|
|
114
|
-
private
|
115
|
-
|
116
121
|
def value_was(record)
|
117
122
|
if record.respond_to?(:attribute_in_database)
|
118
123
|
record.attribute_in_database(:"#{property}") # Rails >= 5.1
|
@@ -1011,7 +1011,7 @@ describe AssignableValues::ActiveRecord do
|
|
1011
1011
|
end
|
1012
1012
|
years = klass.new.humanized_assignable_years
|
1013
1013
|
years.collect(&:value).should == [1977, 1980, 1983]
|
1014
|
-
years.collect(&:humanized).should == ['The year a new hope was born', 'The year the Empire
|
1014
|
+
years.collect(&:humanized).should == ['The year a new hope was born', 'The year the Empire struck back', 'The year the Jedi returned']
|
1015
1015
|
end
|
1016
1016
|
|
1017
1017
|
it 'should properly look up humanizations for namespaced models' do
|
@@ -1021,7 +1021,37 @@ describe AssignableValues::ActiveRecord do
|
|
1021
1021
|
end
|
1022
1022
|
end
|
1023
1023
|
years = klass.new.humanized_assignable_years
|
1024
|
-
years.collect(&:humanized).should == ['The year a new hope was born', 'The year the Empire
|
1024
|
+
years.collect(&:humanized).should == ['The year a new hope was born', 'The year the Empire struck back', 'The year the Jedi returned']
|
1025
|
+
end
|
1026
|
+
|
1027
|
+
context 'when inheriting from a model with defined assignable_values' do
|
1028
|
+
|
1029
|
+
let(:base_class) do
|
1030
|
+
Class.new(Song) do
|
1031
|
+
assignable_values_for :year do
|
1032
|
+
[1977, 1980, 1983]
|
1033
|
+
end
|
1034
|
+
end
|
1035
|
+
end
|
1036
|
+
|
1037
|
+
it 'allows to override humanization for inherited values' do
|
1038
|
+
Song::SeriousRepresentation = Class.new(base_class)
|
1039
|
+
|
1040
|
+
years = Song::SeriousRepresentation.new.humanized_assignable_years
|
1041
|
+
years.collect(&:humanized).should == ['year 1977', 'year 1980', 'year 1983']
|
1042
|
+
end
|
1043
|
+
|
1044
|
+
it 'looks up humanization in parent classes if the translation is not overridden' do
|
1045
|
+
Song::Extension = Class.new(base_class)
|
1046
|
+
Song::ExtendedExtension = Class.new(Song::Extension)
|
1047
|
+
|
1048
|
+
years_1 = Song::Extension.new.humanized_assignable_years
|
1049
|
+
years_1.collect(&:humanized).should == ['The year a new hope was born', 'The year the Empire struck back', 'The year the Jedi returned']
|
1050
|
+
|
1051
|
+
years_2 = Song::ExtendedExtension.new.humanized_assignable_years
|
1052
|
+
years_2.collect(&:humanized).should == ['The year a new hope was born', 'The year the Empire struck back', 'The year the Jedi returned']
|
1053
|
+
end
|
1054
|
+
|
1025
1055
|
end
|
1026
1056
|
|
1027
1057
|
context 'legacy methods for API compatibility' do
|
data/spec/support/i18n.yml
CHANGED
@@ -23,10 +23,15 @@ en:
|
|
23
23
|
rock: 'Rock music'
|
24
24
|
year:
|
25
25
|
'1977': 'The year a new hope was born'
|
26
|
-
'1980': 'The year the Empire
|
26
|
+
'1980': 'The year the Empire struck back'
|
27
27
|
'1983': 'The year the Jedi returned'
|
28
|
+
song/serious_representation:
|
29
|
+
year:
|
30
|
+
'1977': 'year 1977'
|
31
|
+
'1980': 'year 1980'
|
32
|
+
'1983': 'year 1983'
|
28
33
|
recording/vinyl:
|
29
34
|
year:
|
30
35
|
'1977': 'The year a new hope was born'
|
31
|
-
'1980': 'The year the Empire
|
36
|
+
'1980': 'The year the Empire struck back'
|
32
37
|
'1983': 'The year the Jedi returned'
|
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.17.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: 2022-
|
11
|
+
date: 2022-07-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|