enumerize 2.6.1 → 2.7.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/ruby.yml +2 -0
- data/CHANGELOG.md +16 -0
- data/Gemfile.global +4 -1
- data/README.md +1 -0
- data/Rakefile +7 -3
- data/lib/enumerize/activerecord.rb +2 -2
- data/lib/enumerize/attribute.rb +1 -5
- data/lib/enumerize/predicates.rb +14 -0
- data/lib/enumerize/value.rb +5 -5
- data/lib/enumerize/version.rb +1 -1
- data/lib/enumerize.rb +1 -1
- data/test/activerecord_test.rb +9 -0
- data/test/attribute_test.rb +0 -4
- data/test/predicates_test.rb +20 -0
- data/test/value_test.rb +11 -4
- 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: a2c0c71e8e7b38b6ec3eb0e0f53429a21465bd1dcea61ea87761a497fa9f55ab
|
4
|
+
data.tar.gz: b2803acb5138d2a471ddf84cbcc8b5b3fde56507e8f6db7135ea7ea6f50f5f98
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3cb1506ba0eed847bf0e8f09df719ad06f88034b3f58988046bad0f36ecbcabf8e43f8b31f2e38160f15cf0d7886667fbc36a59b91b73f85a6b09efd6d925877
|
7
|
+
data.tar.gz: 4e927b95541a1cd89584d65c7abc5bd1ab7b0fdcda8abaa7e9c53c7b0b4c0e928c5a2055e0c160ffd0e1b295f276d534909f0411c0c6ea35342ee10f2dca9380
|
data/.github/workflows/ruby.yml
CHANGED
@@ -33,6 +33,7 @@ jobs:
|
|
33
33
|
fail-fast: false
|
34
34
|
matrix:
|
35
35
|
ruby-version: ['2.7', '3.0', '3.1']
|
36
|
+
test: ['minitest', 'rspec']
|
36
37
|
gemfile:
|
37
38
|
- Gemfile
|
38
39
|
- Gemfile.rails60
|
@@ -58,6 +59,7 @@ jobs:
|
|
58
59
|
env:
|
59
60
|
BUNDLE_GEMFILE: "${{ matrix.gemfile }}"
|
60
61
|
DB: "${{ matrix.db }}"
|
62
|
+
TEST_FRAMEWORK: "${{ matrix.test }}"
|
61
63
|
steps:
|
62
64
|
- uses: actions/checkout@v2
|
63
65
|
- name: Set up Ruby
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,19 @@
|
|
1
|
+
## 2.7.0 (July 7, 2023)
|
2
|
+
|
3
|
+
### bug fix
|
4
|
+
|
5
|
+
* Warn about already defined predicate methods only when we generate predicate methods with `predicate: true` (by [@nashby](https://github.com/nashby))
|
6
|
+
* Define `Type#cast` instead of deserialize to fix serialization issue. (by [@nashby](https://github.com/nashby))
|
7
|
+
* Fix `Undefined method 'enumerize' for RSpec::ExampleGroups` (by [@softwaregravy](https://github.com/softwaregravy))
|
8
|
+
|
9
|
+
### enchancements
|
10
|
+
|
11
|
+
* Add support for procs as `i18n_scope` option value. (by [@nashby](https://github.com/nashby))
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
enumerize :color, in: %w[green blue], i18n_scope: proc { |value| "color" }
|
15
|
+
```
|
16
|
+
|
1
17
|
## 2.6.1 (March 17, 2023)
|
2
18
|
|
3
19
|
### bug fix
|
data/Gemfile.global
CHANGED
data/README.md
CHANGED
@@ -161,6 +161,7 @@ class Person
|
|
161
161
|
|
162
162
|
enumerize :status, in: %w[student employed retired], i18n_scope: "status"
|
163
163
|
enumerize :roles, in: %w[user admin], i18n_scope: ["user.roles", "roles"]
|
164
|
+
enumerize :color, in: %w[green blue], i18n_scope: proc { |value| "color" }
|
164
165
|
end
|
165
166
|
|
166
167
|
# localization file
|
data/Rakefile
CHANGED
@@ -4,7 +4,6 @@
|
|
4
4
|
require "bundler/gem_tasks"
|
5
5
|
|
6
6
|
require 'rake/testtask'
|
7
|
-
require 'rspec/core/rake_task'
|
8
7
|
|
9
8
|
Rake::TestTask.new do |t|
|
10
9
|
t.libs << 'test'
|
@@ -12,6 +11,11 @@ Rake::TestTask.new do |t|
|
|
12
11
|
t.verbose = true
|
13
12
|
end
|
14
13
|
|
15
|
-
|
14
|
+
if ENV['TEST_FRAMEWORK'] == 'rspec'
|
15
|
+
require 'rspec/core/rake_task'
|
16
|
+
RSpec::Core::RakeTask.new
|
16
17
|
|
17
|
-
task :default => [:
|
18
|
+
task :default => [:spec]
|
19
|
+
else
|
20
|
+
task :default => [:test]
|
21
|
+
end
|
@@ -115,11 +115,11 @@ module Enumerize
|
|
115
115
|
|
116
116
|
alias type_cast_for_database serialize
|
117
117
|
|
118
|
-
def
|
118
|
+
def cast(value)
|
119
119
|
@attr.find_value(value)
|
120
120
|
end
|
121
121
|
|
122
|
-
alias type_cast_from_database
|
122
|
+
alias type_cast_from_database cast
|
123
123
|
|
124
124
|
def as_json(options = nil)
|
125
125
|
{attr: @attr.name}.as_json(options)
|
data/lib/enumerize/attribute.rb
CHANGED
@@ -12,11 +12,7 @@ module Enumerize
|
|
12
12
|
|
13
13
|
@klass = klass
|
14
14
|
@name = name.to_sym
|
15
|
-
|
16
|
-
if options[:i18n_scope]
|
17
|
-
raise ArgumentError, ':i18n_scope option accepts only String or Array of strings' unless Array(options[:i18n_scope]).all? { |s| s.is_a?(String) }
|
18
|
-
@i18n_scope = options[:i18n_scope]
|
19
|
-
end
|
15
|
+
@i18n_scope = options[:i18n_scope]
|
20
16
|
|
21
17
|
value_class = options.fetch(:value_class, Value)
|
22
18
|
@values = Array(options[:in]).map { |v| value_class.new(self, *v).freeze }
|
data/lib/enumerize/predicates.rb
CHANGED
@@ -69,8 +69,22 @@ module Enumerize
|
|
69
69
|
end
|
70
70
|
|
71
71
|
def build(klass)
|
72
|
+
warn_on_already_defined_methods
|
73
|
+
|
72
74
|
klass.delegate(*names, to: @attr.name, prefix: @options[:prefix], allow_nil: true)
|
73
75
|
end
|
76
|
+
|
77
|
+
def warn_on_already_defined_methods
|
78
|
+
names.each do |name|
|
79
|
+
method_name = [@options[:prefix], name].compact.join('_')
|
80
|
+
|
81
|
+
if @attr.klass.respond_to?(method_name)
|
82
|
+
warn(
|
83
|
+
"Predicate method `#{name}` is already defined as #{@attr.klass.name}##{name}. Use enumerize's :prefix option to avoid it"
|
84
|
+
)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
74
88
|
end
|
75
89
|
end
|
76
90
|
end
|
data/lib/enumerize/value.rb
CHANGED
@@ -10,16 +10,16 @@ module Enumerize
|
|
10
10
|
attr_reader :value
|
11
11
|
|
12
12
|
def initialize(attr, name, value=nil)
|
13
|
-
if self.class.method_defined?("#{name}?")
|
14
|
-
warn("It's not recommended to use `#{name}` as a field value since `#{name}?` is defined. (#{attr.klass.name}##{attr.name})")
|
15
|
-
end
|
16
|
-
|
17
13
|
@attr = attr
|
18
14
|
@value = value.nil? ? name.to_s : value
|
19
15
|
|
20
16
|
super(name.to_s)
|
21
17
|
|
22
|
-
@i18n_keys = @attr.i18n_scopes.map
|
18
|
+
@i18n_keys = @attr.i18n_scopes.map do |s|
|
19
|
+
scope = Utils.call_if_callable(s, @value)
|
20
|
+
|
21
|
+
:"#{scope}.#{self}"
|
22
|
+
end
|
23
23
|
@i18n_keys << :"enumerize.defaults.#{@attr.name}.#{self}"
|
24
24
|
@i18n_keys << :"enumerize.#{@attr.name}.#{self}"
|
25
25
|
@i18n_keys << ActiveSupport::Inflector.humanize(ActiveSupport::Inflector.underscore(self)) # humanize value if there are no translations
|
data/lib/enumerize/version.rb
CHANGED
data/lib/enumerize.rb
CHANGED
data/test/activerecord_test.rb
CHANGED
@@ -249,6 +249,15 @@ class ActiveRecordTest < MiniTest::Spec
|
|
249
249
|
expect(user.interests).must_equal %w[music dancing]
|
250
250
|
end
|
251
251
|
|
252
|
+
it 'has enumerized values in active record attributes after reload' do
|
253
|
+
User.delete_all
|
254
|
+
user = User.new
|
255
|
+
user.status = :blocked
|
256
|
+
user.save!
|
257
|
+
user.reload
|
258
|
+
expect(user.attributes["status"]).must_equal 'blocked'
|
259
|
+
end
|
260
|
+
|
252
261
|
it 'validates inclusion when using write_attribute with string attribute' do
|
253
262
|
user = User.new
|
254
263
|
user.send(:write_attribute, 'role', 'wrong')
|
data/test/attribute_test.rb
CHANGED
@@ -37,10 +37,6 @@ class AttributeTest < MiniTest::Spec
|
|
37
37
|
build_attr nil, 'foo', :in => %w[a b], :i18n_scope => %w[bar buzz]
|
38
38
|
expect(attr.i18n_scopes).must_equal %w[bar buzz]
|
39
39
|
end
|
40
|
-
|
41
|
-
it 'accepts only string scopes' do
|
42
|
-
expect(proc { build_attr nil, 'foo', :in => %w[a b], :i18n_scope => [%w[bar buzz], "bar.buzz"] }).must_raise ArgumentError
|
43
|
-
end
|
44
40
|
end
|
45
41
|
|
46
42
|
describe 'options for select' do
|
data/test/predicates_test.rb
CHANGED
@@ -6,6 +6,8 @@ class PredicatesTest < MiniTest::Spec
|
|
6
6
|
let(:kklass) do
|
7
7
|
Class.new do
|
8
8
|
extend Enumerize
|
9
|
+
|
10
|
+
def self.c?; end
|
9
11
|
end
|
10
12
|
end
|
11
13
|
|
@@ -62,4 +64,22 @@ class PredicatesTest < MiniTest::Spec
|
|
62
64
|
expect(object).wont_respond_to :a?
|
63
65
|
expect(object).must_respond_to :b?
|
64
66
|
end
|
67
|
+
|
68
|
+
it 'warns if predicate method is already defined' do
|
69
|
+
assert_output(nil, /`c\?` is already defined/) do
|
70
|
+
kklass.enumerize(:bar, in: %w(a b c), predicates: true)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'does not warn if predicate has prefix and does not collide with defined method' do
|
75
|
+
assert_output(nil, '') do
|
76
|
+
kklass.enumerize(:bar, in: %w(a b c), predicates: { prefix: 'bar' })
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'does not warn if predicate method is already defined but enumerize does not generate predicates' do
|
81
|
+
assert_output(nil, '') do
|
82
|
+
kklass.enumerize(:bar, in: %w(a b c))
|
83
|
+
end
|
84
|
+
end
|
65
85
|
end
|
data/test/value_test.rb
CHANGED
@@ -8,6 +8,9 @@ class ValueTest < MiniTest::Spec
|
|
8
8
|
end
|
9
9
|
|
10
10
|
class Attr < Struct.new(:values, :name, :i18n_scopes, :klass)
|
11
|
+
def value?(value)
|
12
|
+
values.include?(value)
|
13
|
+
end
|
11
14
|
end
|
12
15
|
|
13
16
|
let(:attr) { Attr.new([], "attribute_name", [], Model) }
|
@@ -84,6 +87,14 @@ class ValueTest < MiniTest::Spec
|
|
84
87
|
expect(val.text).must_be :==, "Scope specific translation"
|
85
88
|
end
|
86
89
|
end
|
90
|
+
|
91
|
+
it 'allows to pass a proc as i18n_scopes param' do
|
92
|
+
attr.i18n_scopes = [proc { |v| "other.scope.#{v}" }]
|
93
|
+
|
94
|
+
store_translations(:en, :other => {:scope => {:"1" => {:test_value => "Scope specific translation"}}}) do
|
95
|
+
expect(val.text).must_be :==, "Scope specific translation"
|
96
|
+
end
|
97
|
+
end
|
87
98
|
end
|
88
99
|
|
89
100
|
describe 'boolean methods comparison' do
|
@@ -150,9 +161,5 @@ class ValueTest < MiniTest::Spec
|
|
150
161
|
it 'no output if undefined boolean method' do
|
151
162
|
assert_silent() { Enumerize::Value.new(attr, 'test_value') }
|
152
163
|
end
|
153
|
-
|
154
|
-
it 'error output if defined boolean method' do
|
155
|
-
assert_output(nil, /`empty\?` is defined/) { Enumerize::Value.new(attr, 'empty') }
|
156
|
-
end
|
157
164
|
end
|
158
165
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumerize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergey Nartimov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-07-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|