assignable_values 0.16.6 → 0.18.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +2 -2
- data/.ruby-version +1 -1
- data/CHANGELOG.md +16 -0
- data/Gemfile +1 -1
- 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 +2 -2
- data/Gemfile.6.1.pg.lock +5 -5
- data/Gemfile.7.0.pg +3 -3
- data/Gemfile.7.0.pg.lock +16 -16
- data/Gemfile.lock +1 -1
- 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 +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 35aa591ab2d6741bce223c55269b850a758f5f4f5cb63af013573bd9b54ebc26
|
4
|
+
data.tar.gz: adf1444e4391df0fd44367403b76fa79afb00d3035130a94a7b613e008e0a6e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f8bc245b41f780b768b8e3a5a3420278ac7123cddc90952785f8999ed12d4724c84fdef0876be7fa9cb756818662504b78499049745cc92bf71effd581d4eab0
|
7
|
+
data.tar.gz: c5078834cb387afe0c2b320aef6b70286e1959c873b6b8ec31cd8e5c8bfe308c72b36de5279729ec87a2ab0f9839a7c24d1417d9b13cde174f513b0971fd67eb
|
data/.github/workflows/test.yml
CHANGED
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
3.0
|
1
|
+
3.2.0
|
data/CHANGELOG.md
CHANGED
@@ -12,11 +12,27 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html
|
|
12
12
|
|
13
13
|
-
|
14
14
|
|
15
|
+
|
16
|
+
|
17
|
+
|
18
|
+
## 0.18.0 - 2023-01-24
|
19
|
+
|
20
|
+
### Compatible changes
|
21
|
+
|
22
|
+
- Add support for Ruby 3.2
|
23
|
+
|
24
|
+
## 0.17.0 - 2022-07-27
|
25
|
+
|
26
|
+
### Compatible changes
|
27
|
+
|
28
|
+
- Allow to override humanization for inherited assignable_values
|
29
|
+
|
15
30
|
## 0.16.6 - 2022-03-16
|
16
31
|
|
17
32
|
### Breaking changes
|
18
33
|
|
19
34
|
- Remove no longer supported ruby versions (2.3.8, 2.4.2)
|
35
|
+
- Activate rubygems MFA
|
20
36
|
|
21
37
|
### Compatible changes
|
22
38
|
|
data/Gemfile
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
Gemfile.7.0.pg
|
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.18.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.18.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.18.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
@@ -3,14 +3,14 @@ source 'https://rubygems.org'
|
|
3
3
|
# Runtime dependencies
|
4
4
|
gem 'activerecord', '~>6.1.3'
|
5
5
|
gem 'i18n'
|
6
|
-
gem 'pg', '1.
|
6
|
+
gem 'pg', '~> 1.3.5'
|
7
7
|
|
8
8
|
# Development dependencies
|
9
9
|
gem 'rake'
|
10
10
|
gem 'database_cleaner'
|
11
11
|
gem 'rspec'
|
12
12
|
gem 'rspec_candy'
|
13
|
-
gem 'gemika', '
|
13
|
+
gem 'gemika', '~> 0.8.0'
|
14
14
|
|
15
15
|
# Gem under test
|
16
16
|
gem 'assignable_values', :path => '.'
|
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.18.0)
|
5
5
|
activerecord (>= 2.3)
|
6
6
|
|
7
7
|
GEM
|
@@ -21,11 +21,11 @@ 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.8.0)
|
25
25
|
i18n (1.6.0)
|
26
26
|
concurrent-ruby (~> 1.0)
|
27
27
|
minitest (5.14.4)
|
28
|
-
pg (1.
|
28
|
+
pg (1.3.5)
|
29
29
|
rake (12.3.2)
|
30
30
|
rspec (3.8.0)
|
31
31
|
rspec-core (~> 3.8.0)
|
@@ -56,9 +56,9 @@ DEPENDENCIES
|
|
56
56
|
activerecord (~> 6.1.3)
|
57
57
|
assignable_values!
|
58
58
|
database_cleaner
|
59
|
-
gemika (
|
59
|
+
gemika (~> 0.8.0)
|
60
60
|
i18n
|
61
|
-
pg (
|
61
|
+
pg (~> 1.3.5)
|
62
62
|
rake
|
63
63
|
rspec
|
64
64
|
rspec_candy
|
data/Gemfile.7.0.pg
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
3
|
# Runtime dependencies
|
4
|
-
gem 'activerecord', '~>7.0.
|
4
|
+
gem 'activerecord', '~>7.0.1'
|
5
5
|
gem 'i18n'
|
6
|
-
gem 'pg', '1.
|
6
|
+
gem 'pg', '~> 1.3.5'
|
7
7
|
|
8
8
|
# Development dependencies
|
9
9
|
gem 'rake'
|
10
10
|
gem 'database_cleaner'
|
11
11
|
gem 'rspec'
|
12
12
|
gem 'rspec_candy'
|
13
|
-
gem 'gemika', '
|
13
|
+
gem 'gemika', '~> 0.8.0'
|
14
14
|
|
15
15
|
# Gem under test
|
16
16
|
gem 'assignable_values', :path => '.'
|
data/Gemfile.7.0.pg.lock
CHANGED
@@ -1,30 +1,30 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
assignable_values (0.
|
4
|
+
assignable_values (0.18.0)
|
5
5
|
activerecord (>= 2.3)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activemodel (7.0.
|
11
|
-
activesupport (= 7.0.
|
12
|
-
activerecord (7.0.
|
13
|
-
activemodel (= 7.0.
|
14
|
-
activesupport (= 7.0.
|
15
|
-
activesupport (7.0.
|
10
|
+
activemodel (7.0.4.1)
|
11
|
+
activesupport (= 7.0.4.1)
|
12
|
+
activerecord (7.0.4.1)
|
13
|
+
activemodel (= 7.0.4.1)
|
14
|
+
activesupport (= 7.0.4.1)
|
15
|
+
activesupport (7.0.4.1)
|
16
16
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
17
17
|
i18n (>= 1.6, < 2)
|
18
18
|
minitest (>= 5.1)
|
19
19
|
tzinfo (~> 2.0)
|
20
|
-
concurrent-ruby (1.
|
20
|
+
concurrent-ruby (1.2.0)
|
21
21
|
database_cleaner (1.7.0)
|
22
22
|
diff-lcs (1.3)
|
23
|
-
gemika (0.
|
24
|
-
i18n (1.
|
23
|
+
gemika (0.8.0)
|
24
|
+
i18n (1.12.0)
|
25
25
|
concurrent-ruby (~> 1.0)
|
26
|
-
minitest (5.
|
27
|
-
pg (1.
|
26
|
+
minitest (5.17.0)
|
27
|
+
pg (1.3.5)
|
28
28
|
rake (12.3.2)
|
29
29
|
rspec (3.8.0)
|
30
30
|
rspec-core (~> 3.8.0)
|
@@ -44,19 +44,19 @@ GEM
|
|
44
44
|
sneaky-save
|
45
45
|
sneaky-save (0.1.3)
|
46
46
|
activerecord (>= 3.2.0)
|
47
|
-
tzinfo (2.0.
|
47
|
+
tzinfo (2.0.5)
|
48
48
|
concurrent-ruby (~> 1.0)
|
49
49
|
|
50
50
|
PLATFORMS
|
51
51
|
ruby
|
52
52
|
|
53
53
|
DEPENDENCIES
|
54
|
-
activerecord (~> 7.0.
|
54
|
+
activerecord (~> 7.0.1)
|
55
55
|
assignable_values!
|
56
56
|
database_cleaner
|
57
|
-
gemika (
|
57
|
+
gemika (~> 0.8.0)
|
58
58
|
i18n
|
59
|
-
pg (
|
59
|
+
pg (~> 1.3.5)
|
60
60
|
rake
|
61
61
|
rspec
|
62
62
|
rspec_candy
|
data/Gemfile.lock
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
Gemfile.7.0.pg.lock
|
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.18.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:
|
11
|
+
date: 2023-01-24 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
|
- !ruby/object:Gem::Version
|
91
91
|
version: '0'
|
92
92
|
requirements: []
|
93
|
-
rubygems_version: 3.
|
93
|
+
rubygems_version: 3.4.1
|
94
94
|
signing_key:
|
95
95
|
specification_version: 4
|
96
96
|
summary: Restrict the values assignable to ActiveRecord attributes or associations
|