enumerate_it 3.2.4 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/dependabot.yml +6 -0
- data/.github/workflows/ci.yml +5 -29
- data/.rubocop.yml +4 -1
- data/Appraisals +4 -4
- data/Gemfile.lock +75 -42
- data/README.md +105 -77
- data/enumerate_it.gemspec +4 -3
- data/gemfiles/rails_6.0.gemfile +3 -3
- data/gemfiles/rails_6.1.gemfile +3 -3
- data/gemfiles/rails_7.0.gemfile +3 -3
- data/gemfiles/{rails_5.1.gemfile → rails_7.1.gemfile} +3 -3
- data/lib/enumerate_it/class_methods.rb +11 -0
- data/lib/enumerate_it/version.rb +1 -1
- data/spec/enumerate_it_spec.rb +10 -1
- data/spec/spec_helper.rb +2 -2
- metadata +27 -14
- data/gemfiles/rails_5.0.gemfile +0 -9
- data/gemfiles/rails_5.2.gemfile +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 57b418cb6e9cf24f8b935c898004ddc62ba373f9c03b4c3df600339a26e7d16e
|
4
|
+
data.tar.gz: 4625233483bf656e0e75d9d116dff98fb1801f31e348c54e643bd501081da1fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af19adb823775b00e138df51023b4a724219e9c12958218fb63cbea843fecb0ce8a97657a3ec6aceca2d2db4818b9415c6a94f5105bb31dbca415b542acf3158
|
7
|
+
data.tar.gz: bbaab168f1ba3c5199eced5b46244dae3499b459a8941d9719a3f264cc5a08d46c2d5356aae9381a686c46bce0cc43df6a4230ae2e4fe8266f4bb5a8335ae708
|
data/.github/workflows/ci.yml
CHANGED
@@ -9,39 +9,15 @@ jobs:
|
|
9
9
|
|
10
10
|
matrix:
|
11
11
|
ruby:
|
12
|
-
- 2.5
|
13
|
-
- 2.6
|
14
|
-
- 2.7
|
15
12
|
- 3.0
|
16
13
|
- 3.1
|
14
|
+
- 3.2
|
15
|
+
- 3.3
|
17
16
|
gemfile:
|
18
|
-
- gemfiles/rails_5.0.gemfile
|
19
|
-
- gemfiles/rails_5.1.gemfile
|
20
|
-
- gemfiles/rails_5.2.gemfile
|
21
17
|
- gemfiles/rails_6.0.gemfile
|
22
18
|
- gemfiles/rails_6.1.gemfile
|
23
19
|
- gemfiles/rails_7.0.gemfile
|
24
|
-
|
25
|
-
# Rails < 6 does not support Ruby 3+:
|
26
|
-
# https://github.com/rails/rails/issues/40938#issuecomment-751357907
|
27
|
-
- ruby: 3.0
|
28
|
-
gemfile: gemfiles/rails_5.0.gemfile
|
29
|
-
- ruby: 3.0
|
30
|
-
gemfile: gemfiles/rails_5.1.gemfile
|
31
|
-
- ruby: 3.0
|
32
|
-
gemfile: gemfiles/rails_5.2.gemfile
|
33
|
-
- ruby: 3.1
|
34
|
-
gemfile: gemfiles/rails_5.0.gemfile
|
35
|
-
- ruby: 3.1
|
36
|
-
gemfile: gemfiles/rails_5.1.gemfile
|
37
|
-
- ruby: 3.1
|
38
|
-
gemfile: gemfiles/rails_5.2.gemfile
|
39
|
-
|
40
|
-
# Rails 7.0 requires Ruby 2.7+
|
41
|
-
- ruby: 2.5
|
42
|
-
gemfile: gemfiles/rails_7.0.gemfile
|
43
|
-
- ruby: 2.6
|
44
|
-
gemfile: gemfiles/rails_7.0.gemfile
|
20
|
+
- gemfiles/rails_7.1.gemfile
|
45
21
|
|
46
22
|
env:
|
47
23
|
BUNDLE_GEMFILE: "${{ matrix.gemfile }}"
|
@@ -50,7 +26,7 @@ jobs:
|
|
50
26
|
|
51
27
|
steps:
|
52
28
|
- name: Checkout
|
53
|
-
uses: actions/checkout@
|
29
|
+
uses: actions/checkout@v4
|
54
30
|
- name: Set up Ruby
|
55
31
|
uses: ruby/setup-ruby@v1
|
56
32
|
with:
|
@@ -58,7 +34,7 @@ jobs:
|
|
58
34
|
bundler-cache: true
|
59
35
|
|
60
36
|
- name: Rubocop
|
61
|
-
if: ${{ matrix.ruby == '3.
|
37
|
+
if: ${{ matrix.ruby == '3.3' }}
|
62
38
|
run: "bundle exec rubocop"
|
63
39
|
|
64
40
|
- name: Tests
|
data/.rubocop.yml
CHANGED
@@ -8,12 +8,15 @@ inherit_mode:
|
|
8
8
|
|
9
9
|
AllCops:
|
10
10
|
NewCops: enable
|
11
|
-
TargetRubyVersion:
|
11
|
+
TargetRubyVersion: 3.0
|
12
12
|
|
13
13
|
Exclude:
|
14
14
|
- 'lib/generators/enumerate_it/enum/templates/**/*'
|
15
15
|
- 'gemfiles/vendor/**/*'
|
16
16
|
|
17
|
+
Gemspec/DevelopmentDependencies:
|
18
|
+
Enabled: false
|
19
|
+
|
17
20
|
Gemspec/RequiredRubyVersion:
|
18
21
|
Enabled: false
|
19
22
|
|
data/Appraisals
CHANGED
@@ -4,15 +4,15 @@ require 'json'
|
|
4
4
|
rails_versions = JSON.parse(Net::HTTP.get(URI('https://rubygems.org/api/v1/versions/rails.json')))
|
5
5
|
.group_by { |version| version['number'] }.keys.grep_v(/rc|racecar|beta|pre/)
|
6
6
|
|
7
|
-
%w[
|
8
|
-
appraise "rails_#{
|
7
|
+
%w[6.0 6.1 7.0 7.1].each do |rails_version|
|
8
|
+
appraise "rails_#{rails_version}" do
|
9
9
|
current_version = rails_versions
|
10
|
-
.select { |key| key.match(/\A#{
|
10
|
+
.select { |key| key.match(/\A#{rails_version}/) }
|
11
11
|
.max { |a, b| Gem::Version.new(a) <=> Gem::Version.new(b) }
|
12
12
|
|
13
13
|
gem 'activesupport', "~> #{current_version}"
|
14
14
|
gem 'activerecord', "~> #{current_version}"
|
15
15
|
|
16
|
-
gem 'sqlite3',
|
16
|
+
gem 'sqlite3', '< 2' # v2.x isn't yet working. See: https://github.com/sparklemotion/sqlite3-ruby/issues/529
|
17
17
|
end
|
18
18
|
end
|
data/Gemfile.lock
CHANGED
@@ -1,80 +1,113 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
enumerate_it (
|
5
|
-
activesupport (>=
|
4
|
+
enumerate_it (4.0.0)
|
5
|
+
activesupport (>= 6.0.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
|
10
|
+
activemodel (7.1.3.2)
|
11
|
+
activesupport (= 7.1.3.2)
|
12
|
+
activerecord (7.1.3.2)
|
13
|
+
activemodel (= 7.1.3.2)
|
14
|
+
activesupport (= 7.1.3.2)
|
15
|
+
timeout (>= 0.4.0)
|
16
|
+
activesupport (7.1.3.2)
|
17
|
+
base64
|
18
|
+
bigdecimal
|
11
19
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
20
|
+
connection_pool (>= 2.2.5)
|
21
|
+
drb
|
12
22
|
i18n (>= 1.6, < 2)
|
13
23
|
minitest (>= 5.1)
|
24
|
+
mutex_m
|
14
25
|
tzinfo (~> 2.0)
|
15
|
-
appraisal (2.
|
26
|
+
appraisal (2.5.0)
|
16
27
|
bundler
|
17
28
|
rake
|
18
29
|
thor (>= 0.14.0)
|
19
30
|
ast (2.4.2)
|
31
|
+
base64 (0.2.0)
|
32
|
+
bigdecimal (3.1.7)
|
20
33
|
coderay (1.1.3)
|
21
|
-
concurrent-ruby (1.
|
22
|
-
|
23
|
-
|
34
|
+
concurrent-ruby (1.2.3)
|
35
|
+
connection_pool (2.4.1)
|
36
|
+
diff-lcs (1.5.1)
|
37
|
+
drb (2.2.1)
|
38
|
+
i18n (1.14.4)
|
24
39
|
concurrent-ruby (~> 1.0)
|
25
|
-
json (2.
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
40
|
+
json (2.7.2)
|
41
|
+
language_server-protocol (3.17.0.3)
|
42
|
+
method_source (1.1.0)
|
43
|
+
mini_portile2 (2.8.6)
|
44
|
+
minitest (5.22.3)
|
45
|
+
mutex_m (0.2.0)
|
46
|
+
parallel (1.24.0)
|
47
|
+
parser (3.3.1.0)
|
30
48
|
ast (~> 2.4.1)
|
31
|
-
|
49
|
+
racc
|
50
|
+
pry (0.14.2)
|
32
51
|
coderay (~> 1.1)
|
33
52
|
method_source (~> 1.0)
|
53
|
+
racc (1.7.3)
|
34
54
|
rainbow (3.1.1)
|
35
|
-
rake (13.
|
36
|
-
regexp_parser (2.
|
37
|
-
rexml (3.2.
|
38
|
-
rspec (3.
|
39
|
-
rspec-core (~> 3.
|
40
|
-
rspec-expectations (~> 3.
|
41
|
-
rspec-mocks (~> 3.
|
42
|
-
rspec-core (3.
|
43
|
-
rspec-support (~> 3.
|
44
|
-
rspec-expectations (3.
|
55
|
+
rake (13.2.1)
|
56
|
+
regexp_parser (2.9.0)
|
57
|
+
rexml (3.2.6)
|
58
|
+
rspec (3.13.0)
|
59
|
+
rspec-core (~> 3.13.0)
|
60
|
+
rspec-expectations (~> 3.13.0)
|
61
|
+
rspec-mocks (~> 3.13.0)
|
62
|
+
rspec-core (3.13.0)
|
63
|
+
rspec-support (~> 3.13.0)
|
64
|
+
rspec-expectations (3.13.0)
|
45
65
|
diff-lcs (>= 1.2.0, < 2.0)
|
46
|
-
rspec-support (~> 3.
|
47
|
-
rspec-mocks (3.
|
66
|
+
rspec-support (~> 3.13.0)
|
67
|
+
rspec-mocks (3.13.0)
|
48
68
|
diff-lcs (>= 1.2.0, < 2.0)
|
49
|
-
rspec-support (~> 3.
|
50
|
-
rspec-support (3.
|
51
|
-
rubocop (1.
|
69
|
+
rspec-support (~> 3.13.0)
|
70
|
+
rspec-support (3.13.1)
|
71
|
+
rubocop (1.63.3)
|
52
72
|
json (~> 2.3)
|
73
|
+
language_server-protocol (>= 3.17.0)
|
53
74
|
parallel (~> 1.10)
|
54
|
-
parser (>= 3.
|
75
|
+
parser (>= 3.3.0.2)
|
55
76
|
rainbow (>= 2.2.2, < 4.0)
|
56
77
|
regexp_parser (>= 1.8, < 3.0)
|
57
78
|
rexml (>= 3.2.5, < 4.0)
|
58
|
-
rubocop-ast (>= 1.
|
79
|
+
rubocop-ast (>= 1.31.1, < 2.0)
|
59
80
|
ruby-progressbar (~> 1.7)
|
60
|
-
unicode-display_width (>=
|
61
|
-
rubocop-ast (1.
|
62
|
-
parser (>= 3.
|
81
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
82
|
+
rubocop-ast (1.31.2)
|
83
|
+
parser (>= 3.3.0.4)
|
84
|
+
rubocop-capybara (2.20.0)
|
85
|
+
rubocop (~> 1.41)
|
86
|
+
rubocop-factory_bot (2.25.1)
|
87
|
+
rubocop (~> 1.41)
|
63
88
|
rubocop-rake (0.6.0)
|
64
89
|
rubocop (~> 1.0)
|
65
|
-
rubocop-rspec (2.
|
66
|
-
rubocop (~> 1.
|
67
|
-
|
68
|
-
|
69
|
-
|
90
|
+
rubocop-rspec (2.29.1)
|
91
|
+
rubocop (~> 1.40)
|
92
|
+
rubocop-capybara (~> 2.17)
|
93
|
+
rubocop-factory_bot (~> 2.22)
|
94
|
+
rubocop-rspec_rails (~> 2.28)
|
95
|
+
rubocop-rspec_rails (2.28.3)
|
96
|
+
rubocop (~> 1.40)
|
97
|
+
ruby-progressbar (1.13.0)
|
98
|
+
sqlite3 (1.7.3)
|
99
|
+
mini_portile2 (~> 2.8.0)
|
100
|
+
thor (1.3.1)
|
101
|
+
timeout (0.4.1)
|
102
|
+
tzinfo (2.0.6)
|
70
103
|
concurrent-ruby (~> 1.0)
|
71
|
-
unicode-display_width (2.
|
72
|
-
wwtd (1.4.1)
|
104
|
+
unicode-display_width (2.5.0)
|
73
105
|
|
74
106
|
PLATFORMS
|
75
107
|
ruby
|
76
108
|
|
77
109
|
DEPENDENCIES
|
110
|
+
activerecord
|
78
111
|
appraisal
|
79
112
|
bundler
|
80
113
|
enumerate_it!
|
@@ -84,7 +117,7 @@ DEPENDENCIES
|
|
84
117
|
rubocop
|
85
118
|
rubocop-rake
|
86
119
|
rubocop-rspec
|
87
|
-
|
120
|
+
sqlite3 (< 2)
|
88
121
|
|
89
122
|
BUNDLED WITH
|
90
|
-
2.
|
123
|
+
2.5.9
|
data/README.md
CHANGED
@@ -8,7 +8,8 @@ Enumerations for Ruby with some magic powers! 🎩
|
|
8
8
|
[![Downloads](https://img.shields.io/gem/dt/enumerate_it.svg)](https://rubygems.org/gems/enumerate_it)
|
9
9
|
[![Changelog](https://img.shields.io/badge/changelog--brightgreen.svg?style=flat)](https://github.com/lucascaton/enumerate_it/releases)
|
10
10
|
|
11
|
-
**EnumerateIt** helps you to declare and use enumerations in a very simple and
|
11
|
+
**EnumerateIt** helps you to declare and use enumerations in a very simple and
|
12
|
+
flexible way.
|
12
13
|
|
13
14
|
### Why would I want a gem if Rails already has native enumerations support?
|
14
15
|
|
@@ -21,16 +22,27 @@ so you can **add behaviour** and also **reuse** them! 😀
|
|
21
22
|
|
22
23
|
<!-- Tocer[start]: Auto-generated, don't remove. -->
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
- [
|
27
|
-
- [
|
28
|
-
- [
|
29
|
-
- [
|
30
|
-
- [
|
31
|
-
- [
|
32
|
-
- [
|
33
|
-
- [
|
25
|
+
## Table of Contents
|
26
|
+
|
27
|
+
- [EnumerateIt](#enumerateit)
|
28
|
+
- [Why would I want a gem if Rails already has native enumerations support?](#why-would-i-want-a-gem-if-rails-already-has-native-enumerations-support)
|
29
|
+
- [Table of Contents](#table-of-contents)
|
30
|
+
- [Installation](#installation)
|
31
|
+
- [Using with Rails](#using-with-rails)
|
32
|
+
- [Creating enumerations](#creating-enumerations)
|
33
|
+
- [Sorting enumerations](#sorting-enumerations)
|
34
|
+
- [Using enumerations](#using-enumerations)
|
35
|
+
- [FAQ](#faq)
|
36
|
+
- [Why define enumerations outside the class that uses them?](#why-define-enumerations-outside-the-class-that-uses-them)
|
37
|
+
- [Can I use `enumerate_it` gem without Rails?](#can-i-use-enumerate_it-gem-without-rails)
|
38
|
+
- [What versions of Ruby and Rails are supported?](#what-versions-of-ruby-and-rails-are-supported)
|
39
|
+
- [Can I set a value to always be at the end of a sorted list?](#can-i-set-a-value-to-always-be-at-the-end-of-a-sorted-list)
|
40
|
+
- [I18n](#i18n)
|
41
|
+
- [Translate a name-spaced enumeration](#translate-a-name-spaced-enumeration)
|
42
|
+
- [Handling a legacy database](#handling-a-legacy-database)
|
43
|
+
- [Changelog](#changelog)
|
44
|
+
- [Note on Patches/Pull Requests](#note-on-patchespull-requests)
|
45
|
+
- [Copyright](#copyright)
|
34
46
|
|
35
47
|
<!-- Tocer[finish]: Auto-generated, don't remove. -->
|
36
48
|
|
@@ -56,10 +68,11 @@ rails generate enumerate_it:enum --help
|
|
56
68
|
|
57
69
|
## Creating enumerations
|
58
70
|
|
59
|
-
Enumerations are created as classes and you should put them inside
|
71
|
+
Enumerations are created as classes and you should put them inside
|
72
|
+
`app/enumerations` folder.
|
60
73
|
|
61
|
-
You can pass an array of symbols, so that the respective value for each symbol
|
62
|
-
stringified version of the symbol itself:
|
74
|
+
You can pass an array of symbols, so that the respective value for each symbol
|
75
|
+
will be the stringified version of the symbol itself:
|
63
76
|
|
64
77
|
```ruby
|
65
78
|
class RelationshipStatus < EnumerateIt::Base
|
@@ -73,7 +86,7 @@ end
|
|
73
86
|
|
74
87
|
This will create some nice stuff:
|
75
88
|
|
76
|
-
|
89
|
+
- Each enumeration's value will turn into a constant:
|
77
90
|
|
78
91
|
```ruby
|
79
92
|
RelationshipStatus::SINGLE
|
@@ -83,68 +96,68 @@ This will create some nice stuff:
|
|
83
96
|
#=> 'married'
|
84
97
|
```
|
85
98
|
|
86
|
-
|
99
|
+
- You can retrieve a list with all the enumeration codes:
|
87
100
|
|
88
101
|
```ruby
|
89
102
|
RelationshipStatus.list
|
90
103
|
#=> ['divorced', 'married', 'single']
|
91
104
|
```
|
92
105
|
|
93
|
-
|
106
|
+
- You can retrieve a JSON with all the enumeration codes:
|
94
107
|
|
95
108
|
```ruby
|
96
109
|
RelationshipStatus.to_json
|
97
110
|
#=> "[{\"value\":\"divorced\",\"label\":\"Divorced\"},{\"value\":\"married\", ...
|
98
111
|
```
|
99
112
|
|
100
|
-
|
101
|
-
Rails helpers.
|
113
|
+
- You can get an array of options, ready to use with the `select`, `select_tag`,
|
114
|
+
etc. family of Rails helpers.
|
102
115
|
|
103
116
|
```ruby
|
104
117
|
RelationshipStatus.to_a
|
105
118
|
#=> [['Divorced', 'divorced'], ['Married', 'married'], ['Single', 'single']]
|
106
119
|
```
|
107
120
|
|
108
|
-
|
121
|
+
- You can retrieve a list with values for a group of enumeration constants.
|
109
122
|
|
110
123
|
```ruby
|
111
124
|
RelationshipStatus.values_for %w(MARRIED SINGLE)
|
112
125
|
#=> ['married', 'single']
|
113
126
|
```
|
114
127
|
|
115
|
-
|
128
|
+
- You can retrieve the value for a specific enumeration constant:
|
116
129
|
|
117
130
|
```ruby
|
118
131
|
RelationshipStatus.value_for('MARRIED')
|
119
132
|
#=> 'married'
|
120
133
|
```
|
121
134
|
|
122
|
-
|
135
|
+
- You can retrieve the symbol used to declare a specific enumeration value:
|
123
136
|
|
124
137
|
```ruby
|
125
138
|
RelationshipStatus.key_for(RelationshipStatus::MARRIED)
|
126
139
|
#=> :married
|
127
140
|
```
|
128
141
|
|
129
|
-
|
142
|
+
- You can iterate over the list of the enumeration's values:
|
130
143
|
|
131
144
|
```ruby
|
132
145
|
RelationshipStatus.each_value { |value| ... }
|
133
146
|
```
|
134
147
|
|
135
|
-
|
148
|
+
- You can iterate over the list of the enumeration's translations:
|
136
149
|
|
137
150
|
```ruby
|
138
151
|
RelationshipStatus.each_translation { |translation| ... }
|
139
152
|
```
|
140
153
|
|
141
|
-
|
154
|
+
- You can also retrieve all the translations of the enumeration:
|
142
155
|
|
143
156
|
```ruby
|
144
157
|
RelationshipStatus.translations
|
145
158
|
```
|
146
159
|
|
147
|
-
|
160
|
+
- You can ask for the enumeration's length:
|
148
161
|
|
149
162
|
```ruby
|
150
163
|
RelationshipStatus.length
|
@@ -153,11 +166,11 @@ This will create some nice stuff:
|
|
153
166
|
|
154
167
|
### Sorting enumerations
|
155
168
|
|
156
|
-
When calling methods like `to_a`, `to_json` and `list`, the returned values will
|
157
|
-
default in the same order passed to `associate_values` call.
|
158
|
-
|
159
|
-
However, if you want to overwrite the default sort mode, you can use the `sort_by` class method:
|
169
|
+
When calling methods like `to_a`, `to_json` and `list`, the returned values will
|
170
|
+
be sorted by default in the same order passed to `associate_values` call.
|
160
171
|
|
172
|
+
However, if you want to overwrite the default sort mode, you can use the
|
173
|
+
`sort_by` class method:
|
161
174
|
|
162
175
|
```ruby
|
163
176
|
class RelationshipStatus < EnumerateIt::Base
|
@@ -197,14 +210,15 @@ class Person
|
|
197
210
|
end
|
198
211
|
```
|
199
212
|
|
200
|
-
> **Note:** **EnumerateIt** will try to load an enumeration class based on the
|
201
|
-
> name. If you have a different name, you can specify it by
|
213
|
+
> **Note:** **EnumerateIt** will try to load an enumeration class based on the
|
214
|
+
> camelized attribute name. If you have a different name, you can specify it by
|
215
|
+
> using the `with` option:
|
202
216
|
>
|
203
217
|
> `has_enumeration_for :relationship_status, with: RelationshipStatus`
|
204
218
|
|
205
219
|
This will create:
|
206
220
|
|
207
|
-
|
221
|
+
- A "humanized" version of the hash's key to humanize the attribute's value:
|
208
222
|
|
209
223
|
```ruby
|
210
224
|
p = Person.new
|
@@ -213,8 +227,8 @@ This will create:
|
|
213
227
|
#=> 'Divorced'
|
214
228
|
```
|
215
229
|
|
216
|
-
|
217
|
-
|
230
|
+
- A translation for your options, if you include a locale to represent it (see
|
231
|
+
more in the [I18n section](#i18n)).
|
218
232
|
|
219
233
|
```ruby
|
220
234
|
p = Person.new
|
@@ -223,14 +237,16 @@ This will create:
|
|
223
237
|
#=> 'Divorciado'
|
224
238
|
```
|
225
239
|
|
226
|
-
|
240
|
+
- The associated enumerations, which can be retrieved with the `enumerations`
|
241
|
+
class method:
|
227
242
|
|
228
243
|
```ruby
|
229
244
|
Person.enumerations
|
230
245
|
#=> { relationship_status: RelationshipStatus }
|
231
246
|
```
|
232
247
|
|
233
|
-
|
248
|
+
- A helper method for each enumeration option, if you pass the `create_helpers`
|
249
|
+
option as `true`:
|
234
250
|
|
235
251
|
```ruby
|
236
252
|
class Person < ApplicationRecord
|
@@ -247,8 +263,9 @@ This will create:
|
|
247
263
|
#=> false
|
248
264
|
```
|
249
265
|
|
250
|
-
It's also possible to "namespace" the created helper methods, passing a hash
|
251
|
-
option. This can be useful when two or more of the
|
266
|
+
It's also possible to "namespace" the created helper methods, passing a hash
|
267
|
+
to the `create_helpers` option. This can be useful when two or more of the
|
268
|
+
enumerations used share the same constants:
|
252
269
|
|
253
270
|
```ruby
|
254
271
|
class Person < ApplicationRecord
|
@@ -266,8 +283,8 @@ This will create:
|
|
266
283
|
#=> false
|
267
284
|
```
|
268
285
|
|
269
|
-
You can define polymorphic behavior for the enumeration values, so you can
|
270
|
-
of them:
|
286
|
+
You can define polymorphic behavior for the enumeration values, so you can
|
287
|
+
define a class for each of them:
|
271
288
|
|
272
289
|
```ruby
|
273
290
|
class RelationshipStatus < EnumerateIt::Base
|
@@ -312,8 +329,8 @@ This will create:
|
|
312
329
|
p.relationship_status_mode.saturday_night
|
313
330
|
```
|
314
331
|
|
315
|
-
The `create_helpers` also creates some mutator helper methods, that can be
|
316
|
-
attribute's value.
|
332
|
+
The `create_helpers` also creates some mutator helper methods, that can be
|
333
|
+
used to change the attribute's value.
|
317
334
|
|
318
335
|
```ruby
|
319
336
|
p = Person.new
|
@@ -323,7 +340,8 @@ This will create:
|
|
323
340
|
#=> true
|
324
341
|
```
|
325
342
|
|
326
|
-
|
343
|
+
- A scope method for each enumeration option if you pass the `create_scopes`
|
344
|
+
option as `true`:
|
327
345
|
|
328
346
|
```ruby
|
329
347
|
class Person < ApplicationRecord
|
@@ -345,7 +363,7 @@ This will create:
|
|
345
363
|
Person.relationship_status_married.to_sql
|
346
364
|
```
|
347
365
|
|
348
|
-
|
366
|
+
- An inclusion validation (if your class can manage validations and responds to
|
349
367
|
`validates_inclusion_of`):
|
350
368
|
|
351
369
|
```ruby
|
@@ -360,7 +378,7 @@ This will create:
|
|
360
378
|
#=> 'is not included in the list'
|
361
379
|
```
|
362
380
|
|
363
|
-
|
381
|
+
- A presence validation (if your class can manage validations and responds to
|
364
382
|
`validates_presence_of` and you pass the `required` options as `true`):
|
365
383
|
|
366
384
|
```ruby
|
@@ -375,7 +393,8 @@ This will create:
|
|
375
393
|
#=> "can't be blank"
|
376
394
|
```
|
377
395
|
|
378
|
-
If you pass the `skip_validation` option as `true`, it will not create any
|
396
|
+
If you pass the `skip_validation` option as `true`, it will not create any
|
397
|
+
validations:
|
379
398
|
|
380
399
|
```ruby
|
381
400
|
class Person < ApplicationRecord
|
@@ -387,15 +406,16 @@ This will create:
|
|
387
406
|
#=> true
|
388
407
|
```
|
389
408
|
|
390
|
-
Remember that you can add validations to any kind of class and not only
|
409
|
+
Remember that you can add validations to any kind of class and not only
|
410
|
+
`ActiveRecord` ones.
|
391
411
|
|
392
412
|
## FAQ
|
393
413
|
|
394
414
|
#### Why define enumerations outside the class that uses them?
|
395
415
|
|
396
|
-
|
397
|
-
|
398
|
-
|
416
|
+
- It's clearer.
|
417
|
+
- You can add behaviour to the enumeration class.
|
418
|
+
- You can reuse the enumeration inside other classes.
|
399
419
|
|
400
420
|
#### Can I use `enumerate_it` gem without Rails?
|
401
421
|
|
@@ -403,20 +423,22 @@ You sure can! 😄
|
|
403
423
|
|
404
424
|
#### What versions of Ruby and Rails are supported?
|
405
425
|
|
406
|
-
|
407
|
-
|
426
|
+
- **Ruby**: `3.0+`
|
427
|
+
- **Rails** `6.0+`
|
408
428
|
|
409
429
|
All versions are tested via
|
410
430
|
[GitHub Actions](https://github.com/lucascaton/enumerate_it/blob/HEAD/.github/workflows/ci.yml).
|
411
431
|
|
412
432
|
#### Can I set a value to always be at the end of a sorted list?
|
413
433
|
|
414
|
-
Yes,
|
434
|
+
Yes,
|
435
|
+
[see more details here](https://github.com/lucascaton/enumerate_it/issues/60).
|
415
436
|
|
416
437
|
## I18n
|
417
438
|
|
418
|
-
I18n lookup is provided on both `_humanized` and `Enumeration#to_a` methods,
|
419
|
-
a Symbol. The I18n strings are located on
|
439
|
+
I18n lookup is provided on both `_humanized` and `Enumeration#to_a` methods,
|
440
|
+
given the hash key is a Symbol. The I18n strings are located on
|
441
|
+
`enumerations.<enumeration_name>.<key>`:
|
420
442
|
|
421
443
|
```yaml
|
422
444
|
# Your locale file
|
@@ -454,22 +476,22 @@ RelationshipStatus.t(status)
|
|
454
476
|
|
455
477
|
### Translate a name-spaced enumeration
|
456
478
|
|
457
|
-
In order to translate an enumeration in a specific namespace (say
|
458
|
-
you can add the following:
|
479
|
+
In order to translate an enumeration in a specific namespace (say
|
480
|
+
`Design::Color`), you can add the following:
|
459
481
|
|
460
482
|
```yaml
|
461
483
|
pt-BR:
|
462
484
|
enumerations:
|
463
|
-
|
485
|
+
"design/color":
|
464
486
|
blue: Azul
|
465
487
|
red: Vermelho
|
466
488
|
```
|
467
489
|
|
468
490
|
## Handling a legacy database
|
469
491
|
|
470
|
-
**EnumerateIt** can help you build a Rails application around a legacy database
|
471
|
-
with those small and unchangeable tables used to create foreign
|
472
|
-
following example:
|
492
|
+
**EnumerateIt** can help you build a Rails application around a legacy database
|
493
|
+
which was filled with those small and unchangeable tables used to create foreign
|
494
|
+
key constraints everywhere, like the following example:
|
473
495
|
|
474
496
|
```sql
|
475
497
|
Table "public.relationship_status"
|
@@ -491,14 +513,16 @@ code | description
|
|
491
513
|
3 | Divorced
|
492
514
|
```
|
493
515
|
|
494
|
-
You might also have something like a `users` table with a `relationship_status`
|
495
|
-
key pointing to the `relationship_status` table.
|
516
|
+
You might also have something like a `users` table with a `relationship_status`
|
517
|
+
column and a foreign key pointing to the `relationship_status` table.
|
496
518
|
|
497
|
-
While this is a good thing from the database normalization perspective, managing
|
498
|
-
tests is very hard. Doing database joins just to get the
|
499
|
-
And, more than this, referencing them in
|
500
|
-
|
501
|
-
|
519
|
+
While this is a good thing from the database normalization perspective, managing
|
520
|
+
these values in tests is very hard. Doing database joins just to get the
|
521
|
+
description of some value is absurd. And, more than this, referencing them in
|
522
|
+
the code using
|
523
|
+
[magic numbers](<https://en.wikipedia.org/wiki/Magic_number_(programming)>) was
|
524
|
+
terrible and meaningless: what does it mean when we say that someone or
|
525
|
+
something is `2`?
|
502
526
|
|
503
527
|
To solve this, you can pass a **hash** to your enumeration values:
|
504
528
|
|
@@ -526,14 +550,18 @@ you can see them on the [releases page](../../releases).
|
|
526
550
|
|
527
551
|
## Note on Patches/Pull Requests
|
528
552
|
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
553
|
+
- Fork the project.
|
554
|
+
- Make your feature addition or bug fix.
|
555
|
+
- Add tests for it. This is important so we don't break it in a future version
|
556
|
+
unintentionally.
|
557
|
+
- [Optional] Run the tests against a specific Gemfile:
|
558
|
+
`$ bundle exec appraisal rails_7.0 rake spec`.
|
559
|
+
- Run the tests against all supported versions: `$ bundle exec rake` (or
|
560
|
+
`$ bundle exec wwtd`)
|
561
|
+
- Commit, but please do not mess with `Rakefile`, version, or history.
|
562
|
+
- Send a Pull Request. Bonus points for topic branches.
|
536
563
|
|
537
564
|
## Copyright
|
538
565
|
|
539
|
-
Copyright (c) 2010-
|
566
|
+
Copyright (c) 2010-2024 Cássio Marques and Lucas Caton. See `LICENSE` file for
|
567
|
+
details.
|
data/enumerate_it.gemspec
CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |gem|
|
|
12
12
|
gem.name = 'enumerate_it'
|
13
13
|
gem.require_paths = ['lib']
|
14
14
|
gem.version = EnumerateIt::VERSION
|
15
|
-
gem.required_ruby_version = '>=
|
15
|
+
gem.required_ruby_version = '>= 3.0.0'
|
16
16
|
|
17
17
|
gem.metadata = {
|
18
18
|
'source_code_uri' => 'https://github.com/lucascaton/enumerate_it',
|
@@ -20,8 +20,9 @@ Gem::Specification.new do |gem|
|
|
20
20
|
'rubygems_mfa_required' => 'true'
|
21
21
|
}
|
22
22
|
|
23
|
-
gem.add_dependency 'activesupport', '>=
|
23
|
+
gem.add_dependency 'activesupport', '>= 6.0.0'
|
24
24
|
|
25
|
+
gem.add_development_dependency 'activerecord'
|
25
26
|
gem.add_development_dependency 'appraisal'
|
26
27
|
gem.add_development_dependency 'bundler'
|
27
28
|
gem.add_development_dependency 'pry'
|
@@ -30,5 +31,5 @@ Gem::Specification.new do |gem|
|
|
30
31
|
gem.add_development_dependency 'rubocop'
|
31
32
|
gem.add_development_dependency 'rubocop-rake'
|
32
33
|
gem.add_development_dependency 'rubocop-rspec'
|
33
|
-
gem.add_development_dependency '
|
34
|
+
gem.add_development_dependency 'sqlite3', '< 2'
|
34
35
|
end
|
data/gemfiles/rails_6.0.gemfile
CHANGED
data/gemfiles/rails_6.1.gemfile
CHANGED
data/gemfiles/rails_7.0.gemfile
CHANGED
@@ -5,6 +5,7 @@ module EnumerateIt
|
|
5
5
|
|
6
6
|
define_enumeration_class(attribute, options)
|
7
7
|
create_enumeration_humanize_method(options[:with], attribute)
|
8
|
+
create_enumeration_key_method(options[:with], attribute)
|
8
9
|
store_enumeration(options[:with], attribute)
|
9
10
|
|
10
11
|
handle_options(attribute, options)
|
@@ -38,6 +39,16 @@ module EnumerateIt
|
|
38
39
|
end
|
39
40
|
end
|
40
41
|
|
42
|
+
def create_enumeration_key_method(klass, attribute_name)
|
43
|
+
class_eval do
|
44
|
+
define_method "#{attribute_name}_key" do
|
45
|
+
value = public_send(attribute_name)
|
46
|
+
|
47
|
+
value ? klass.key_for(value) : nil
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
41
52
|
def create_helper_methods(klass, attribute_name, helpers)
|
42
53
|
prefix_name = "#{attribute_name}_" if helpers.is_a?(Hash) && helpers[:prefix]
|
43
54
|
|
data/lib/enumerate_it/version.rb
CHANGED
data/spec/enumerate_it_spec.rb
CHANGED
@@ -28,6 +28,15 @@ describe EnumerateIt do
|
|
28
28
|
expect(target.foobar_humanize).to be_nil
|
29
29
|
end
|
30
30
|
|
31
|
+
it 'creates the key method' do
|
32
|
+
expect(target.foobar_key).to eq(:value_2)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'if the attribute is blank, the key method returns nil' do
|
36
|
+
target.foobar = nil
|
37
|
+
expect(target.foobar_key).to be_nil
|
38
|
+
end
|
39
|
+
|
31
40
|
it 'defaults to not creating helper methods' do
|
32
41
|
expect(target).not_to respond_to(:value_1?)
|
33
42
|
end
|
@@ -288,7 +297,7 @@ describe EnumerateIt do
|
|
288
297
|
end
|
289
298
|
end
|
290
299
|
|
291
|
-
it 'when called, the scopes create the correct query', sqlite
|
300
|
+
it 'when called, the scopes create the correct query', :sqlite do
|
292
301
|
ActiveRecord::Schema.define { create_table :test_class_with_scopes }
|
293
302
|
|
294
303
|
TestEnumeration.enumeration.each do |symbol, pair|
|
data/spec/spec_helper.rb
CHANGED
@@ -5,7 +5,7 @@ require 'enumerate_it'
|
|
5
5
|
require 'active_support/all'
|
6
6
|
require 'active_record'
|
7
7
|
|
8
|
-
Dir['./spec/support/**/*.rb'].
|
8
|
+
Dir['./spec/support/**/*.rb'].each { |f| require f }
|
9
9
|
|
10
10
|
I18n.config.enforce_available_locales = false
|
11
11
|
I18n.load_path = Dir['spec/i18n/*.yml']
|
@@ -13,7 +13,7 @@ I18n.load_path = Dir['spec/i18n/*.yml']
|
|
13
13
|
RSpec.configure do |config|
|
14
14
|
config.filter_run_when_matching :focus
|
15
15
|
|
16
|
-
config.before(:each, sqlite
|
16
|
+
config.before(:each, :sqlite) do
|
17
17
|
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
|
18
18
|
end
|
19
19
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumerate_it
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 4.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cássio Marques
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2024-04-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -17,14 +17,28 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version:
|
20
|
+
version: 6.0.0
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version:
|
27
|
+
version: 6.0.0
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: activerecord
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
28
42
|
- !ruby/object:Gem::Dependency
|
29
43
|
name: appraisal
|
30
44
|
requirement: !ruby/object:Gem::Requirement
|
@@ -138,25 +152,26 @@ dependencies:
|
|
138
152
|
- !ruby/object:Gem::Version
|
139
153
|
version: '0'
|
140
154
|
- !ruby/object:Gem::Dependency
|
141
|
-
name:
|
155
|
+
name: sqlite3
|
142
156
|
requirement: !ruby/object:Gem::Requirement
|
143
157
|
requirements:
|
144
|
-
- - "
|
158
|
+
- - "<"
|
145
159
|
- !ruby/object:Gem::Version
|
146
|
-
version: '
|
160
|
+
version: '2'
|
147
161
|
type: :development
|
148
162
|
prerelease: false
|
149
163
|
version_requirements: !ruby/object:Gem::Requirement
|
150
164
|
requirements:
|
151
|
-
- - "
|
165
|
+
- - "<"
|
152
166
|
- !ruby/object:Gem::Version
|
153
|
-
version: '
|
167
|
+
version: '2'
|
154
168
|
description: Enumerations for Ruby with some magic powers!
|
155
169
|
email:
|
156
170
|
executables: []
|
157
171
|
extensions: []
|
158
172
|
extra_rdoc_files: []
|
159
173
|
files:
|
174
|
+
- ".github/dependabot.yml"
|
160
175
|
- ".github/workflows/ci.yml"
|
161
176
|
- ".gitignore"
|
162
177
|
- ".rspec"
|
@@ -169,12 +184,10 @@ files:
|
|
169
184
|
- README.md
|
170
185
|
- Rakefile
|
171
186
|
- enumerate_it.gemspec
|
172
|
-
- gemfiles/rails_5.0.gemfile
|
173
|
-
- gemfiles/rails_5.1.gemfile
|
174
|
-
- gemfiles/rails_5.2.gemfile
|
175
187
|
- gemfiles/rails_6.0.gemfile
|
176
188
|
- gemfiles/rails_6.1.gemfile
|
177
189
|
- gemfiles/rails_7.0.gemfile
|
190
|
+
- gemfiles/rails_7.1.gemfile
|
178
191
|
- lib/enumerate_it.rb
|
179
192
|
- lib/enumerate_it/base.rb
|
180
193
|
- lib/enumerate_it/class_methods.rb
|
@@ -204,14 +217,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
204
217
|
requirements:
|
205
218
|
- - ">="
|
206
219
|
- !ruby/object:Gem::Version
|
207
|
-
version:
|
220
|
+
version: 3.0.0
|
208
221
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
209
222
|
requirements:
|
210
223
|
- - ">="
|
211
224
|
- !ruby/object:Gem::Version
|
212
225
|
version: '0'
|
213
226
|
requirements: []
|
214
|
-
rubygems_version: 3.
|
227
|
+
rubygems_version: 3.5.9
|
215
228
|
signing_key:
|
216
229
|
specification_version: 4
|
217
230
|
summary: Ruby Enumerations
|
data/gemfiles/rails_5.0.gemfile
DELETED