enumerate_it 4.0.0 → 4.2.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/ci.yml +24 -2
- data/.prettierrc.json +4 -0
- data/.rubocop.yml +7 -3
- data/Appraisals +15 -3
- data/Gemfile.lock +72 -63
- data/LICENSE +1 -1
- data/README.md +138 -52
- data/enumerate_it.gemspec +1 -1
- data/gemfiles/rails_6.0.gemfile +3 -0
- data/gemfiles/rails_6.1.gemfile +5 -2
- data/gemfiles/rails_7.0.gemfile +5 -2
- data/gemfiles/rails_7.1.gemfile +3 -3
- data/gemfiles/rails_7.2.gemfile +9 -0
- data/gemfiles/rails_8.0.gemfile +9 -0
- data/gemfiles/rails_8.1.gemfile +9 -0
- data/lib/enumerate_it/base.rb +29 -7
- data/lib/enumerate_it/class_methods.rb +19 -3
- data/lib/enumerate_it/version.rb +1 -1
- data/spec/enumerate_it/base_spec.rb +88 -25
- data/spec/enumerate_it_spec.rb +74 -1
- data/spec/spec_helper.rb +1 -0
- data/spec/support/test_classes.rb +17 -1
- metadata +11 -11
data/README.md
CHANGED
|
@@ -4,19 +4,44 @@ Enumerations for Ruby with some magic powers! 🎩
|
|
|
4
4
|
|
|
5
5
|
[](https://github.com/lucascaton/enumerate_it/actions?query=workflow%3ACI)
|
|
6
6
|
[](https://rubygems.org/gems/enumerate_it)
|
|
7
|
-
[](https://codeclimate.com/github/lucascaton/enumerate_it)
|
|
8
7
|
[](https://rubygems.org/gems/enumerate_it)
|
|
9
8
|
[](https://github.com/lucascaton/enumerate_it/releases)
|
|
10
9
|
|
|
11
|
-
**EnumerateIt** helps you
|
|
10
|
+
**EnumerateIt** helps you declare and use enumerations in a very simple and
|
|
12
11
|
flexible way.
|
|
13
12
|
|
|
14
|
-
### Why would I want a gem if Rails already has native
|
|
13
|
+
### Why would I want a gem if Rails already has native enumeration support?
|
|
15
14
|
|
|
16
15
|
Firstly, although **EnumerateIt** works well with **Rails**, it isn't required!
|
|
17
|
-
|
|
18
|
-
[define your enumerations in classes](
|
|
19
|
-
|
|
16
|
+
This means you can add it to any **Ruby** project! Secondly, you can
|
|
17
|
+
[define your enumerations in classes](#creating-enumerations), allowing you to
|
|
18
|
+
**add behavior** and **reuse** them! 😀
|
|
19
|
+
|
|
20
|
+
At a glance:
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
class RelationshipStatus < EnumerateIt::Base
|
|
24
|
+
associate_values :single, :married, :divorced
|
|
25
|
+
|
|
26
|
+
custom_helpers do
|
|
27
|
+
def ever_married?(value)
|
|
28
|
+
[MARRIED, DIVORCED].include?(value)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
class Person < ApplicationRecord
|
|
34
|
+
has_enumeration_for :relationship_status, create_helpers: true, create_scopes: true
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
person = Person.new(relationship_status: RelationshipStatus::MARRIED)
|
|
38
|
+
|
|
39
|
+
person.married? #=> true
|
|
40
|
+
person.ever_married? #=> true
|
|
41
|
+
person.relationship_status_humanize #=> 'Married'
|
|
42
|
+
Person.married.to_sql #=> SELECT "people".* FROM "people" WHERE "people"."relationship_status" = "married"
|
|
43
|
+
RelationshipStatus.to_a #=> [['Divorced', 'divorced'], ['Married', 'married'], ['Single', 'single']]
|
|
44
|
+
```
|
|
20
45
|
|
|
21
46
|
---
|
|
22
47
|
|
|
@@ -24,21 +49,18 @@ so you can **add behaviour** and also **reuse** them! 😀
|
|
|
24
49
|
|
|
25
50
|
## Table of Contents
|
|
26
51
|
|
|
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
52
|
- [Installation](#installation)
|
|
31
53
|
- [Using with Rails](#using-with-rails)
|
|
32
54
|
- [Creating enumerations](#creating-enumerations)
|
|
33
55
|
- [Sorting enumerations](#sorting-enumerations)
|
|
34
56
|
- [Using enumerations](#using-enumerations)
|
|
35
57
|
- [FAQ](#faq)
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
58
|
+
- [Why define enumerations outside the class that uses them?](#why-define-enumerations-outside-the-class-that-uses-them)
|
|
59
|
+
- [Can I use `enumerate_it` gem without Rails?](#can-i-use-enumerate_it-gem-without-rails)
|
|
60
|
+
- [What versions of Ruby and Rails are supported?](#what-versions-of-ruby-and-rails-are-supported)
|
|
61
|
+
- [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
62
|
- [I18n](#i18n)
|
|
41
|
-
- [Translate a
|
|
63
|
+
- [Translate a namespaced enumeration](#translate-a-namespaced-enumeration)
|
|
42
64
|
- [Handling a legacy database](#handling-a-legacy-database)
|
|
43
65
|
- [Changelog](#changelog)
|
|
44
66
|
- [Note on Patches/Pull Requests](#note-on-patchespull-requests)
|
|
@@ -68,11 +90,11 @@ rails generate enumerate_it:enum --help
|
|
|
68
90
|
|
|
69
91
|
## Creating enumerations
|
|
70
92
|
|
|
71
|
-
Enumerations are created as classes and
|
|
93
|
+
Enumerations are created as classes and should be placed inside the
|
|
72
94
|
`app/enumerations` folder.
|
|
73
95
|
|
|
74
|
-
You can pass an array of symbols,
|
|
75
|
-
|
|
96
|
+
You can pass an array of symbols, where each symbol's value will be its
|
|
97
|
+
stringified version:
|
|
76
98
|
|
|
77
99
|
```ruby
|
|
78
100
|
class RelationshipStatus < EnumerateIt::Base
|
|
@@ -84,9 +106,9 @@ class RelationshipStatus < EnumerateIt::Base
|
|
|
84
106
|
end
|
|
85
107
|
```
|
|
86
108
|
|
|
87
|
-
This will
|
|
109
|
+
This will generate some nice stuff:
|
|
88
110
|
|
|
89
|
-
-
|
|
111
|
+
- Constants for each enumeration value:
|
|
90
112
|
|
|
91
113
|
```ruby
|
|
92
114
|
RelationshipStatus::SINGLE
|
|
@@ -96,22 +118,21 @@ This will create some nice stuff:
|
|
|
96
118
|
#=> 'married'
|
|
97
119
|
```
|
|
98
120
|
|
|
99
|
-
-
|
|
121
|
+
- A list of all enumeration codes:
|
|
100
122
|
|
|
101
123
|
```ruby
|
|
102
124
|
RelationshipStatus.list
|
|
103
125
|
#=> ['divorced', 'married', 'single']
|
|
104
126
|
```
|
|
105
127
|
|
|
106
|
-
-
|
|
128
|
+
- A JSON representation:
|
|
107
129
|
|
|
108
130
|
```ruby
|
|
109
131
|
RelationshipStatus.to_json
|
|
110
132
|
#=> "[{\"value\":\"divorced\",\"label\":\"Divorced\"},{\"value\":\"married\", ...
|
|
111
133
|
```
|
|
112
134
|
|
|
113
|
-
-
|
|
114
|
-
etc. family of Rails helpers.
|
|
135
|
+
- An array of options for Rails helpers, such as `select`, `select_tag`, etc.:
|
|
115
136
|
|
|
116
137
|
```ruby
|
|
117
138
|
RelationshipStatus.to_a
|
|
@@ -166,11 +187,10 @@ This will create some nice stuff:
|
|
|
166
187
|
|
|
167
188
|
### Sorting enumerations
|
|
168
189
|
|
|
169
|
-
When calling methods like `to_a`, `to_json` and `list`,
|
|
170
|
-
|
|
190
|
+
When calling methods like `to_a`, `to_json` and `list`, values are sorted in the
|
|
191
|
+
order they were passed to `associate_values`, by default.
|
|
171
192
|
|
|
172
|
-
|
|
173
|
-
`sort_by` class method:
|
|
193
|
+
You can override this with the `sort_by` class method:
|
|
174
194
|
|
|
175
195
|
```ruby
|
|
176
196
|
class RelationshipStatus < EnumerateIt::Base
|
|
@@ -180,18 +200,20 @@ class RelationshipStatus < EnumerateIt::Base
|
|
|
180
200
|
end
|
|
181
201
|
```
|
|
182
202
|
|
|
183
|
-
|
|
203
|
+
Accepted values for `sort_by`:
|
|
184
204
|
|
|
185
|
-
| Value
|
|
186
|
-
|
|
|
187
|
-
| `:none`
|
|
188
|
-
| `:name`
|
|
189
|
-
| `:translation`
|
|
190
|
-
| `:
|
|
205
|
+
| Value | Behavior |
|
|
206
|
+
| :----------------------- | :------------------------------------------------------------------------------------ |
|
|
207
|
+
| `:none` | Uses the original order from `associate_values` |
|
|
208
|
+
| `:name` | Sorts by the name of each enumeration option |
|
|
209
|
+
| `:translation` | Sorts by their translations |
|
|
210
|
+
| `:normalize_translation` | Sorts by their translations normalized with NFKD unicode method (without accents) |
|
|
211
|
+
| `:value` | Sorts by assigned values (useful for [legacy databases](#handling-a-legacy-database)) |
|
|
191
212
|
|
|
192
213
|
## Using enumerations
|
|
193
214
|
|
|
194
|
-
The cool part is that you can use these enumerations
|
|
215
|
+
The cool part is that you can use these enumerations in any class, whether
|
|
216
|
+
ActiveRecord-based or not:
|
|
195
217
|
|
|
196
218
|
```ruby
|
|
197
219
|
# ActiveRecord instance
|
|
@@ -210,11 +232,13 @@ class Person
|
|
|
210
232
|
end
|
|
211
233
|
```
|
|
212
234
|
|
|
213
|
-
|
|
214
|
-
>
|
|
215
|
-
>
|
|
235
|
+
<!-- prettier-ignore -->
|
|
236
|
+
> [!NOTE]
|
|
237
|
+
> If the enumeration class name differs from the attribute name, use the `with` option:
|
|
216
238
|
>
|
|
217
|
-
>
|
|
239
|
+
> ```ruby
|
|
240
|
+
> has_enumeration_for :relationship_status, with: RelationshipStatus
|
|
241
|
+
> ```
|
|
218
242
|
|
|
219
243
|
This will create:
|
|
220
244
|
|
|
@@ -234,7 +258,7 @@ This will create:
|
|
|
234
258
|
p = Person.new
|
|
235
259
|
p.relationship_status = RelationshipStatus::DIVORCED
|
|
236
260
|
p.relationship_status_humanize
|
|
237
|
-
#=> 'Divorciado'
|
|
261
|
+
#=> 'Divorciado' # with a pt-BR locale loaded
|
|
238
262
|
```
|
|
239
263
|
|
|
240
264
|
- The associated enumerations, which can be retrieved with the `enumerations`
|
|
@@ -340,6 +364,63 @@ This will create:
|
|
|
340
364
|
#=> true
|
|
341
365
|
```
|
|
342
366
|
|
|
367
|
+
<!-- prettier-ignore -->
|
|
368
|
+
> [!IMPORTANT]
|
|
369
|
+
> On ActiveRecord objects, mutator methods like `married!` call `save!`
|
|
370
|
+
> immediately, running callbacks and raising on validation failure. They are
|
|
371
|
+
> not pure setters.
|
|
372
|
+
|
|
373
|
+
You can also define custom helper methods on the enumeration class using
|
|
374
|
+
a `custom_helpers` block:
|
|
375
|
+
|
|
376
|
+
```ruby
|
|
377
|
+
class RelationshipStatus < EnumerateIt::Base
|
|
378
|
+
associate_values :single, :married, :divorced
|
|
379
|
+
|
|
380
|
+
custom_helpers do
|
|
381
|
+
def ever_married?(value)
|
|
382
|
+
[MARRIED, DIVORCED].include?(value)
|
|
383
|
+
end
|
|
384
|
+
end
|
|
385
|
+
end
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
These become class methods on the enumeration:
|
|
389
|
+
|
|
390
|
+
```ruby
|
|
391
|
+
RelationshipStatus.ever_married?(RelationshipStatus::MARRIED) #=> true
|
|
392
|
+
RelationshipStatus.ever_married?(RelationshipStatus::DIVORCED) #=> true
|
|
393
|
+
RelationshipStatus.ever_married?(RelationshipStatus::SINGLE) #=> false
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
When `create_helpers: true` is used, they also become instance methods on the
|
|
397
|
+
model:
|
|
398
|
+
|
|
399
|
+
```ruby
|
|
400
|
+
class Person < ApplicationRecord
|
|
401
|
+
has_enumeration_for :relationship_status, with: RelationshipStatus, create_helpers: true
|
|
402
|
+
end
|
|
403
|
+
|
|
404
|
+
person = Person.new(relationship_status: RelationshipStatus::DIVORCED)
|
|
405
|
+
person.ever_married? #=> true
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
The `prefix` option also applies to custom helpers:
|
|
409
|
+
|
|
410
|
+
```ruby
|
|
411
|
+
class Person < ApplicationRecord
|
|
412
|
+
has_enumeration_for :relationship_status,
|
|
413
|
+
with: RelationshipStatus, create_helpers: { prefix: true }
|
|
414
|
+
end
|
|
415
|
+
|
|
416
|
+
person = Person.new(relationship_status: RelationshipStatus::DIVORCED)
|
|
417
|
+
person.relationship_status_ever_married? #=> true
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
Custom helper methods return `nil` when the attribute is `nil`. If a method
|
|
421
|
+
name collides with an already-defined class method on the enumeration, an
|
|
422
|
+
`ArgumentError` is raised at load time.
|
|
423
|
+
|
|
343
424
|
- A scope method for each enumeration option if you pass the `create_scopes`
|
|
344
425
|
option as `true`:
|
|
345
426
|
|
|
@@ -349,7 +430,7 @@ This will create:
|
|
|
349
430
|
end
|
|
350
431
|
|
|
351
432
|
Person.married.to_sql
|
|
352
|
-
#=> SELECT "
|
|
433
|
+
#=> SELECT "people".* FROM "people" WHERE "people"."relationship_status" = "married"
|
|
353
434
|
```
|
|
354
435
|
|
|
355
436
|
The `:create_scopes` also accepts `prefix` option.
|
|
@@ -406,6 +487,12 @@ This will create:
|
|
|
406
487
|
#=> true
|
|
407
488
|
```
|
|
408
489
|
|
|
490
|
+
<!-- prettier-ignore -->
|
|
491
|
+
> [!NOTE]
|
|
492
|
+
> `skip_validation: true` disables all validations added by EnumerateIt,
|
|
493
|
+
> including the presence validation from `required: true`. If both are
|
|
494
|
+
> passed, `skip_validation` wins and `required` is silently ignored.
|
|
495
|
+
|
|
409
496
|
Remember that you can add validations to any kind of class and not only
|
|
410
497
|
`ActiveRecord` ones.
|
|
411
498
|
|
|
@@ -414,7 +501,7 @@ Remember that you can add validations to any kind of class and not only
|
|
|
414
501
|
#### Why define enumerations outside the class that uses them?
|
|
415
502
|
|
|
416
503
|
- It's clearer.
|
|
417
|
-
- You can add
|
|
504
|
+
- You can add behavior to the enumeration class.
|
|
418
505
|
- You can reuse the enumeration inside other classes.
|
|
419
506
|
|
|
420
507
|
#### Can I use `enumerate_it` gem without Rails?
|
|
@@ -424,7 +511,7 @@ You sure can! 😄
|
|
|
424
511
|
#### What versions of Ruby and Rails are supported?
|
|
425
512
|
|
|
426
513
|
- **Ruby**: `3.0+`
|
|
427
|
-
- **Rails
|
|
514
|
+
- **Rails**: `6.0+`
|
|
428
515
|
|
|
429
516
|
All versions are tested via
|
|
430
517
|
[GitHub Actions](https://github.com/lucascaton/enumerate_it/blob/HEAD/.github/workflows/ci.yml).
|
|
@@ -436,7 +523,7 @@ Yes,
|
|
|
436
523
|
|
|
437
524
|
## I18n
|
|
438
525
|
|
|
439
|
-
I18n lookup is provided
|
|
526
|
+
I18n lookup is provided for both `_humanize` and `Enumeration#to_a` methods,
|
|
440
527
|
given the hash key is a Symbol. The I18n strings are located on
|
|
441
528
|
`enumerations.<enumeration_name>.<key>`:
|
|
442
529
|
|
|
@@ -458,11 +545,11 @@ end
|
|
|
458
545
|
|
|
459
546
|
p = Person.new
|
|
460
547
|
p.relationship_status = RelationshipStatus::MARRIED
|
|
461
|
-
p.relationship_status_humanize #
|
|
548
|
+
p.relationship_status_humanize # Key found in the locale file
|
|
462
549
|
#=> 'Casado'
|
|
463
550
|
|
|
464
551
|
p.relationship_status = RelationshipStatus::SINGLE
|
|
465
|
-
p.relationship_status_humanize #
|
|
552
|
+
p.relationship_status_humanize # Key missing, falls back to the humanized name
|
|
466
553
|
#=> 'Single'
|
|
467
554
|
```
|
|
468
555
|
|
|
@@ -474,10 +561,10 @@ RelationshipStatus.t(status)
|
|
|
474
561
|
#=> 'Casado'
|
|
475
562
|
```
|
|
476
563
|
|
|
477
|
-
### Translate a
|
|
564
|
+
### Translate a namespaced enumeration
|
|
478
565
|
|
|
479
566
|
In order to translate an enumeration in a specific namespace (say
|
|
480
|
-
`Design::Color`),
|
|
567
|
+
`Design::Color`), use the following structure:
|
|
481
568
|
|
|
482
569
|
```yaml
|
|
483
570
|
pt-BR:
|
|
@@ -555,13 +642,12 @@ you can see them on the [releases page](../../releases).
|
|
|
555
642
|
- Add tests for it. This is important so we don't break it in a future version
|
|
556
643
|
unintentionally.
|
|
557
644
|
- [Optional] Run the tests against a specific Gemfile:
|
|
558
|
-
|
|
559
|
-
- Run the tests against all supported versions:
|
|
560
|
-
`$ bundle exec wwtd`)
|
|
645
|
+
`bundle exec appraisal rails_8.1 rake spec`.
|
|
646
|
+
- Run the tests against all supported versions: `bundle exec rake`
|
|
561
647
|
- Commit, but please do not mess with `Rakefile`, version, or history.
|
|
562
648
|
- Send a Pull Request. Bonus points for topic branches.
|
|
563
649
|
|
|
564
650
|
## Copyright
|
|
565
651
|
|
|
566
|
-
Copyright (c) 2010-
|
|
652
|
+
Copyright (c) 2010-2026 Cássio Marques and Lucas Caton. See `LICENSE` file for
|
|
567
653
|
details.
|
data/enumerate_it.gemspec
CHANGED
|
@@ -31,5 +31,5 @@ Gem::Specification.new do |gem|
|
|
|
31
31
|
gem.add_development_dependency 'rubocop'
|
|
32
32
|
gem.add_development_dependency 'rubocop-rake'
|
|
33
33
|
gem.add_development_dependency 'rubocop-rspec'
|
|
34
|
-
gem.add_development_dependency 'sqlite3'
|
|
34
|
+
gem.add_development_dependency 'sqlite3'
|
|
35
35
|
end
|
data/gemfiles/rails_6.0.gemfile
CHANGED
data/gemfiles/rails_6.1.gemfile
CHANGED
|
@@ -2,8 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
source 'https://rubygems.org'
|
|
4
4
|
|
|
5
|
-
gem 'activerecord', '~> 6.1.7.
|
|
6
|
-
gem 'activesupport', '~> 6.1.7.
|
|
5
|
+
gem 'activerecord', '~> 6.1.7.10'
|
|
6
|
+
gem 'activesupport', '~> 6.1.7.10'
|
|
7
|
+
gem 'base64'
|
|
8
|
+
gem 'bigdecimal'
|
|
9
|
+
gem 'mutex_m'
|
|
7
10
|
gem 'sqlite3', '< 2'
|
|
8
11
|
|
|
9
12
|
gemspec path: '../'
|
data/gemfiles/rails_7.0.gemfile
CHANGED
|
@@ -2,8 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
source 'https://rubygems.org'
|
|
4
4
|
|
|
5
|
-
gem 'activerecord', '~> 7.0.
|
|
6
|
-
gem 'activesupport', '~> 7.0.
|
|
5
|
+
gem 'activerecord', '~> 7.0.10'
|
|
6
|
+
gem 'activesupport', '~> 7.0.10'
|
|
7
|
+
gem 'base64'
|
|
8
|
+
gem 'bigdecimal'
|
|
9
|
+
gem 'mutex_m'
|
|
7
10
|
gem 'sqlite3', '< 2'
|
|
8
11
|
|
|
9
12
|
gemspec path: '../'
|
data/gemfiles/rails_7.1.gemfile
CHANGED
data/lib/enumerate_it/base.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
require 'forwardable'
|
|
2
2
|
|
|
3
3
|
module EnumerateIt
|
|
4
|
-
class Base
|
|
4
|
+
class Base # rubocop:disable Metrics/ClassLength
|
|
5
5
|
class << self
|
|
6
6
|
extend Forwardable
|
|
7
7
|
|
|
@@ -95,6 +95,25 @@ module EnumerateIt
|
|
|
95
95
|
I18n.t("enumerations.#{name.underscore}.#{value.to_s.underscore}", default: default)
|
|
96
96
|
end
|
|
97
97
|
|
|
98
|
+
def custom_helpers(&block)
|
|
99
|
+
@custom_helper_methods ||= []
|
|
100
|
+
|
|
101
|
+
mod = Module.new(&block)
|
|
102
|
+
methods = mod.instance_methods(false)
|
|
103
|
+
collisions = methods & singleton_class.instance_methods
|
|
104
|
+
|
|
105
|
+
raise ArgumentError, <<~MESSAGE.chomp if collisions.any?
|
|
106
|
+
Custom helper(s) '#{collisions.join(', ')}' would override existing EnumerateIt::Base methods
|
|
107
|
+
MESSAGE
|
|
108
|
+
|
|
109
|
+
@custom_helper_methods.concat(methods)
|
|
110
|
+
extend mod
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def custom_helper_methods
|
|
114
|
+
@custom_helper_methods || []
|
|
115
|
+
end
|
|
116
|
+
|
|
98
117
|
private
|
|
99
118
|
|
|
100
119
|
def sorted_map
|
|
@@ -105,12 +124,17 @@ module EnumerateIt
|
|
|
105
124
|
|
|
106
125
|
def sort_lambda
|
|
107
126
|
{
|
|
108
|
-
value:
|
|
109
|
-
name:
|
|
110
|
-
translation:
|
|
127
|
+
value: ->(_k, v) { v[0] },
|
|
128
|
+
name: ->(k, _v) { k },
|
|
129
|
+
translation: ->(_k, v) { translate(v[1]) },
|
|
130
|
+
normalize_translation: ->(_k, v) { normalize_translation(translate(v[1])) }
|
|
111
131
|
}[sort_mode]
|
|
112
132
|
end
|
|
113
133
|
|
|
134
|
+
def normalize_translation(text)
|
|
135
|
+
text.unicode_normalize(:nfkd).gsub(/[^\x00-\x7F]/, '')
|
|
136
|
+
end
|
|
137
|
+
|
|
114
138
|
def normalize_enumeration(values_hash)
|
|
115
139
|
values_hash.each_pair do |key, value|
|
|
116
140
|
values_hash[key] = [value, key] unless value.is_a? Array
|
|
@@ -129,9 +153,7 @@ module EnumerateIt
|
|
|
129
153
|
def values_hash(args)
|
|
130
154
|
return args.first if args.first.is_a?(Hash)
|
|
131
155
|
|
|
132
|
-
args.
|
|
133
|
-
hash[value] = value.to_s
|
|
134
|
-
end
|
|
156
|
+
args.to_h { |value| [value, value.to_s] }
|
|
135
157
|
end
|
|
136
158
|
end
|
|
137
159
|
end
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
module EnumerateIt
|
|
2
|
-
module ClassMethods
|
|
2
|
+
module ClassMethods # rubocop:disable Metrics/ModuleLength
|
|
3
3
|
def has_enumeration_for(attribute, options = {})
|
|
4
4
|
self.enumerations = enumerations.dup
|
|
5
5
|
|
|
@@ -17,7 +17,8 @@ module EnumerateIt
|
|
|
17
17
|
set_validations(attribute, options) unless options[:skip_validation]
|
|
18
18
|
|
|
19
19
|
if options[:create_helpers]
|
|
20
|
-
%w[create_helper_methods create_mutator_methods create_polymorphic_methods
|
|
20
|
+
%w[create_helper_methods create_mutator_methods create_polymorphic_methods
|
|
21
|
+
create_custom_helper_methods].each do |method|
|
|
21
22
|
send(method, options[:with], attribute, options[:create_helpers])
|
|
22
23
|
end
|
|
23
24
|
end
|
|
@@ -100,12 +101,27 @@ module EnumerateIt
|
|
|
100
101
|
end
|
|
101
102
|
end
|
|
102
103
|
|
|
104
|
+
def create_custom_helper_methods(klass, attribute_name, helpers)
|
|
105
|
+
return unless klass.custom_helper_methods.any?
|
|
106
|
+
|
|
107
|
+
prefix_name = "#{attribute_name}_" if helpers.is_a?(Hash) && helpers[:prefix]
|
|
108
|
+
|
|
109
|
+
class_eval do
|
|
110
|
+
klass.custom_helper_methods.each do |method_name|
|
|
111
|
+
define_method "#{prefix_name}#{method_name}" do
|
|
112
|
+
value = send(attribute_name)
|
|
113
|
+
klass.public_send(method_name, value) unless value.nil?
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
103
119
|
def define_enumeration_class(attribute, options)
|
|
104
120
|
return if options[:with]
|
|
105
121
|
|
|
106
122
|
inner_enum_class_name = attribute.to_s.camelize.to_sym
|
|
107
123
|
|
|
108
|
-
options[:with] = if
|
|
124
|
+
options[:with] = if const_defined?(inner_enum_class_name)
|
|
109
125
|
const_get(inner_enum_class_name)
|
|
110
126
|
else
|
|
111
127
|
attribute.to_s.camelize.constantize
|
data/lib/enumerate_it/version.rb
CHANGED