enumerate_it 1.4.1 → 1.5.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/.rubocop.yml +7 -0
- data/.travis.yml +12 -7
- data/Appraisals +3 -2
- data/Gemfile.lock +18 -15
- data/LICENSE +1 -1
- data/README.md +56 -41
- data/enumerate_it.gemspec +2 -1
- data/gemfiles/.bundle/config +2 -0
- data/gemfiles/rails_3.0.gemfile +1 -1
- data/gemfiles/rails_3.1.gemfile +1 -1
- data/gemfiles/rails_3.2.gemfile +1 -1
- data/gemfiles/rails_4.0.gemfile +1 -1
- data/gemfiles/rails_4.1.gemfile +1 -1
- data/gemfiles/rails_4.2.gemfile +3 -3
- data/gemfiles/rails_5.0.gemfile +3 -3
- data/lib/enumerate_it/base.rb +0 -1
- data/lib/enumerate_it/class_methods.rb +4 -4
- data/lib/enumerate_it/version.rb +1 -1
- data/spec/enumerate_it/base_spec.rb +16 -16
- data/spec/enumerate_it_spec.rb +5 -5
- data/spec/support/test_classes.rb +4 -4
- metadata +19 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 73ff26eab60abc3e49c03132a0c3e069fc985da4
|
|
4
|
+
data.tar.gz: 2681588dd7bc52233d62f47023dee81c7fc3724a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b08595f1b91a21a996b6bf1b36ea41e8235d516b9c99b96416b1149dae08a0097b7e47c7c5522d6ef63a15bdb278b7ff7727c7e3d2dd3e410e3f331dd770ed89
|
|
7
|
+
data.tar.gz: 400d30edda48484eec9eac387e64fe7d30c929a84a25cecea3e2f85a6f7264b636dde7d03d4686889f612e73b2dcbcb47e5993f0a773a37c2f83759a5d99f01a
|
data/.rubocop.yml
CHANGED
|
@@ -17,6 +17,10 @@ Lint/EndAlignment:
|
|
|
17
17
|
Metrics/LineLength:
|
|
18
18
|
Max: 100
|
|
19
19
|
|
|
20
|
+
Metrics/BlockLength:
|
|
21
|
+
Exclude:
|
|
22
|
+
- 'spec/**/*'
|
|
23
|
+
|
|
20
24
|
Style/AlignHash:
|
|
21
25
|
EnforcedLastArgumentHashStyle: ignore_implicit
|
|
22
26
|
|
|
@@ -50,3 +54,6 @@ RSpec/NestedGroups:
|
|
|
50
54
|
|
|
51
55
|
RSpec/MessageExpectation:
|
|
52
56
|
Enabled: false
|
|
57
|
+
|
|
58
|
+
RSpec/MessageSpies:
|
|
59
|
+
Enabled: false
|
data/.travis.yml
CHANGED
|
@@ -5,15 +5,15 @@ sudo: false
|
|
|
5
5
|
cache: bundler
|
|
6
6
|
|
|
7
7
|
script:
|
|
8
|
-
- "ruby -e \"RUBY_VERSION == '2.
|
|
8
|
+
- "ruby -e \"RUBY_VERSION == '2.4.1' ? system('bundle exec rubocop') : exit(0)\""
|
|
9
9
|
- bundle exec rake spec
|
|
10
10
|
|
|
11
11
|
rvm:
|
|
12
|
-
- 1.9.3
|
|
13
12
|
- 2.0.0
|
|
14
13
|
- 2.1.10
|
|
15
|
-
- 2.2.
|
|
16
|
-
- 2.3.
|
|
14
|
+
- 2.2.7
|
|
15
|
+
- 2.3.4
|
|
16
|
+
- 2.4.1
|
|
17
17
|
|
|
18
18
|
gemfile:
|
|
19
19
|
- gemfiles/rails_3.0.gemfile
|
|
@@ -25,15 +25,20 @@ gemfile:
|
|
|
25
25
|
- gemfiles/rails_5.0.gemfile
|
|
26
26
|
|
|
27
27
|
matrix:
|
|
28
|
-
# Rails 5 requires Ruby version 2.2+
|
|
29
28
|
exclude:
|
|
30
|
-
|
|
31
|
-
gemfile: gemfiles/rails_5.0.gemfile
|
|
29
|
+
# Rails 5 requires Ruby version 2.2+
|
|
32
30
|
- rvm: 2.0.0
|
|
33
31
|
gemfile: gemfiles/rails_5.0.gemfile
|
|
34
32
|
- rvm: 2.1.10
|
|
35
33
|
gemfile: gemfiles/rails_5.0.gemfile
|
|
36
34
|
|
|
35
|
+
# Rails 4.0 and 4.1 requires an old version of `json` gem, which isn't compatible with Ruby 2.4+
|
|
36
|
+
# https://github.com/flori/json/issues/308
|
|
37
|
+
- rvm: 2.4.1
|
|
38
|
+
gemfile: gemfiles/rails_4.0.gemfile
|
|
39
|
+
- rvm: 2.4.1
|
|
40
|
+
gemfile: gemfiles/rails_4.1.gemfile
|
|
41
|
+
|
|
37
42
|
addons:
|
|
38
43
|
code_climate:
|
|
39
44
|
repo_token: 60e4a18e2a4bc86a98f92847f16756876c13d1e772058a9b3296643b04a697d7
|
data/Appraisals
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
require 'net/http'
|
|
1
2
|
require 'json'
|
|
2
3
|
|
|
3
4
|
rails_versions = JSON.parse(Net::HTTP.get(URI('https://rubygems.org/api/v1/versions/rails.json')))
|
|
4
|
-
.group_by { |version| version['number'] }.keys.
|
|
5
|
+
.group_by { |version| version['number'] }.keys.reject { |key| key =~ /rc|racecar|beta|pre/ }
|
|
5
6
|
|
|
6
|
-
%w
|
|
7
|
+
%w[3.0 3.1 3.2 4.0 4.1 4.2 5.0].each do |version|
|
|
7
8
|
appraise "rails_#{version}" do
|
|
8
9
|
current_version = rails_versions.select { |key| key.match(/\A#{version}/) }.max
|
|
9
10
|
|
data/Gemfile.lock
CHANGED
|
@@ -1,36 +1,37 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
enumerate_it (1.
|
|
4
|
+
enumerate_it (1.5.0)
|
|
5
5
|
activesupport (>= 3.0.0)
|
|
6
6
|
|
|
7
7
|
GEM
|
|
8
8
|
remote: https://rubygems.org/
|
|
9
9
|
specs:
|
|
10
|
-
activesupport (5.0.
|
|
10
|
+
activesupport (5.0.2)
|
|
11
11
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
12
12
|
i18n (~> 0.7)
|
|
13
13
|
minitest (~> 5.1)
|
|
14
14
|
tzinfo (~> 1.1)
|
|
15
|
-
appraisal (2.
|
|
15
|
+
appraisal (2.2.0)
|
|
16
16
|
bundler
|
|
17
17
|
rake
|
|
18
18
|
thor (>= 0.14.0)
|
|
19
19
|
ast (2.3.0)
|
|
20
20
|
coderay (1.1.1)
|
|
21
|
-
concurrent-ruby (1.0.
|
|
22
|
-
diff-lcs (1.
|
|
23
|
-
i18n (0.
|
|
21
|
+
concurrent-ruby (1.0.5)
|
|
22
|
+
diff-lcs (1.3)
|
|
23
|
+
i18n (0.8.1)
|
|
24
24
|
method_source (0.8.2)
|
|
25
25
|
minitest (5.10.1)
|
|
26
|
-
parser (2.
|
|
26
|
+
parser (2.4.0.0)
|
|
27
27
|
ast (~> 2.2)
|
|
28
28
|
powerpack (0.1.1)
|
|
29
29
|
pry (0.10.4)
|
|
30
30
|
coderay (~> 1.1.0)
|
|
31
31
|
method_source (~> 0.8.1)
|
|
32
32
|
slop (~> 3.4)
|
|
33
|
-
rainbow (2.
|
|
33
|
+
rainbow (2.2.2)
|
|
34
|
+
rake
|
|
34
35
|
rake (12.0.0)
|
|
35
36
|
rspec (3.5.0)
|
|
36
37
|
rspec-core (~> 3.5.0)
|
|
@@ -45,21 +46,22 @@ GEM
|
|
|
45
46
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
46
47
|
rspec-support (~> 3.5.0)
|
|
47
48
|
rspec-support (3.5.0)
|
|
48
|
-
rubocop (0.
|
|
49
|
-
parser (>= 2.3.
|
|
49
|
+
rubocop (0.48.1)
|
|
50
|
+
parser (>= 2.3.3.1, < 3.0)
|
|
50
51
|
powerpack (~> 0.1)
|
|
51
52
|
rainbow (>= 1.99.1, < 3.0)
|
|
52
53
|
ruby-progressbar (~> 1.7)
|
|
53
54
|
unicode-display_width (~> 1.0, >= 1.0.1)
|
|
54
|
-
rubocop-rspec (1.
|
|
55
|
+
rubocop-rspec (1.15.0)
|
|
55
56
|
rubocop (>= 0.42.0)
|
|
56
57
|
ruby-progressbar (1.8.1)
|
|
57
58
|
slop (3.6.0)
|
|
58
59
|
thor (0.19.4)
|
|
59
|
-
thread_safe (0.3.
|
|
60
|
-
tzinfo (1.2.
|
|
60
|
+
thread_safe (0.3.6)
|
|
61
|
+
tzinfo (1.2.3)
|
|
61
62
|
thread_safe (~> 0.1)
|
|
62
|
-
unicode-display_width (1.1
|
|
63
|
+
unicode-display_width (1.2.1)
|
|
64
|
+
wwtd (1.3.0)
|
|
63
65
|
|
|
64
66
|
PLATFORMS
|
|
65
67
|
ruby
|
|
@@ -73,6 +75,7 @@ DEPENDENCIES
|
|
|
73
75
|
rspec
|
|
74
76
|
rubocop
|
|
75
77
|
rubocop-rspec
|
|
78
|
+
wwtd
|
|
76
79
|
|
|
77
80
|
BUNDLED WITH
|
|
78
|
-
1.
|
|
81
|
+
1.14.6
|
data/LICENSE
CHANGED
data/README.md
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
# EnumerateIt
|
|
2
2
|
|
|
3
|
-
Ruby Enumerations with magic powers!
|
|
3
|
+
💎 Ruby Enumerations with magic powers! 🎩
|
|
4
4
|
|
|
5
5
|
[](https://travis-ci.org/lucascaton/enumerate_it)
|
|
6
6
|
[](https://rubygems.org/gems/enumerate_it)
|
|
7
|
+
[](https://gemnasium.com/github.com/lucascaton/enumerate_it)
|
|
7
8
|
[](https://codeclimate.com/github/lucascaton/enumerate_it)
|
|
8
9
|
[](https://github.com/lucascaton/enumerate_it/releases)
|
|
9
10
|
|
|
@@ -11,25 +12,27 @@ Ruby Enumerations with magic powers!
|
|
|
11
12
|
|
|
12
13
|
### Why would I want a gem if Rails already has native enumerations support?
|
|
13
14
|
|
|
14
|
-
Firstly, **EnumerateIt** works
|
|
15
|
-
It means you can add it to any **Ruby** project!
|
|
15
|
+
Firstly, although **EnumerateIt** works well with **Rails**, it isn't required!
|
|
16
|
+
It means you can add it to any **Ruby** project! Secondly, you can
|
|
17
|
+
[define your enumerations in classes](https://github.com/lucascaton/enumerate_it#creating-enumerations),
|
|
18
|
+
so you can **add behaviour** and also **reuse** them! 😀
|
|
16
19
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
model, your database will no longer be consistent.
|
|
21
|
-
Database storage pricing is not a problem nowadays, so it's recommended to use `strings` columns.
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
<!-- Tocer[start]: Auto-generated, don't remove. -->
|
|
22
23
|
|
|
23
24
|
- [Installation](#installation)
|
|
24
25
|
- [Using with Rails](#using-with-rails)
|
|
25
26
|
- [Creating enumerations](#creating-enumerations)
|
|
26
27
|
- [Sorting enumerations](#sorting-enumerations)
|
|
27
28
|
- [Using enumerations](#using-enumerations)
|
|
28
|
-
- [I18n](#i18n)
|
|
29
|
-
- [Using enumerations to handle a legacy database](#using-enumerations-to-handle-a-legacy-database)
|
|
30
29
|
- [FAQ](#faq)
|
|
30
|
+
- [I18n](#i18n)
|
|
31
|
+
- [Handling a legacy database](#handling-a-legacy-database)
|
|
31
32
|
- [Changelog](#changelog)
|
|
32
33
|
|
|
34
|
+
<!-- Tocer[finish]: Auto-generated, don't remove. -->
|
|
35
|
+
|
|
33
36
|
## Installation
|
|
34
37
|
|
|
35
38
|
```bash
|
|
@@ -79,7 +82,7 @@ This will create some nice stuff:
|
|
|
79
82
|
|
|
80
83
|
```ruby
|
|
81
84
|
RelationshipStatus::SINGLE
|
|
82
|
-
|
|
85
|
+
#=> 'single'
|
|
83
86
|
|
|
84
87
|
RelationshipStatus::MARRIED
|
|
85
88
|
#=> 'married'
|
|
@@ -99,8 +102,8 @@ This will create some nice stuff:
|
|
|
99
102
|
#=> "[{\"value\":\"divorced\",\"label\":\"Divorced\"},{\"value\":\"married\", ...
|
|
100
103
|
```
|
|
101
104
|
|
|
102
|
-
* You can get an array of options, ready to use with the
|
|
103
|
-
|
|
105
|
+
* You can get an array of options, ready to use with the `select`, `select_tag`, etc. family of
|
|
106
|
+
Rails helpers.
|
|
104
107
|
|
|
105
108
|
```ruby
|
|
106
109
|
RelationshipStatus.to_a
|
|
@@ -155,25 +158,28 @@ This will create some nice stuff:
|
|
|
155
158
|
|
|
156
159
|
### Sorting enumerations
|
|
157
160
|
|
|
158
|
-
When calling methods like `to_a`, `to_json` and `list`, the returned values will be sorted
|
|
159
|
-
|
|
160
|
-
|
|
161
|
+
When calling methods like `to_a`, `to_json` and `list`, the returned values will be sorted by
|
|
162
|
+
default in the same order passed to `associate_values` call.
|
|
163
|
+
|
|
164
|
+
However, if you want to overwrite the default sort mode, you can use the `sort_by` class method:
|
|
165
|
+
|
|
161
166
|
|
|
162
167
|
```ruby
|
|
163
168
|
class RelationshipStatus < EnumerateIt::Base
|
|
164
|
-
associate_values
|
|
169
|
+
associate_values :single, :married
|
|
165
170
|
|
|
166
171
|
sort_by :translation
|
|
167
172
|
end
|
|
168
173
|
```
|
|
169
174
|
|
|
170
|
-
The `sort_by`
|
|
175
|
+
The `sort_by` method accepts one of the following values:
|
|
171
176
|
|
|
172
177
|
| Value | Behavior |
|
|
173
178
|
| :------------- | :------------------------------------------------------------------------------------------- |
|
|
174
179
|
| `:none` | The default behavior, will return values in order that was passed to `associate_values` call |
|
|
175
|
-
| `:translation` | will sort the returned values based on translations |
|
|
176
180
|
| `:name` | Will sort the returned values based on the name of each enumeration option |
|
|
181
|
+
| `:translation` | will sort the returned values based on their translations |
|
|
182
|
+
| `:value` | See [Handling a legacy database](#handling-a-legacy-database) section for more details |
|
|
177
183
|
|
|
178
184
|
## Using enumerations
|
|
179
185
|
|
|
@@ -206,7 +212,7 @@ This will create:
|
|
|
206
212
|
```
|
|
207
213
|
|
|
208
214
|
* A translation for your options, if you include a locale to represent it
|
|
209
|
-
(see more in the [
|
|
215
|
+
(see more in the [I18n section](#i18n)).
|
|
210
216
|
|
|
211
217
|
```ruby
|
|
212
218
|
p = Person.new
|
|
@@ -326,7 +332,7 @@ This will create:
|
|
|
326
332
|
#=> SELECT "users".* FROM "users" WHERE "users"."relationship_status" = "married"
|
|
327
333
|
```
|
|
328
334
|
|
|
329
|
-
The `:create_scopes` also accepts
|
|
335
|
+
The `:create_scopes` also accepts `prefix` option.
|
|
330
336
|
|
|
331
337
|
```ruby
|
|
332
338
|
class Person < ActiveRecord::Base
|
|
@@ -382,12 +388,30 @@ This will create:
|
|
|
382
388
|
Remember that you can add validations to any kind of class and not only to those derived from
|
|
383
389
|
`ActiveRecord::Base`.
|
|
384
390
|
|
|
385
|
-
|
|
391
|
+
## FAQ
|
|
392
|
+
|
|
393
|
+
#### Why to define enumerations outside the class that uses them?
|
|
386
394
|
|
|
387
395
|
* It's clearer.
|
|
388
396
|
* You can add behaviour to the enumeration class.
|
|
389
397
|
* You can reuse the enumeration inside other classes.
|
|
390
398
|
|
|
399
|
+
#### Can I use `enumerate_it` gem without Rails?
|
|
400
|
+
|
|
401
|
+
You sure can! 😄
|
|
402
|
+
|
|
403
|
+
#### What versions of Ruby and Rails are supported?
|
|
404
|
+
|
|
405
|
+
* **Ruby** `2.0` or higher
|
|
406
|
+
* **Rails** `3.0` or higher
|
|
407
|
+
|
|
408
|
+
All versions are tested via
|
|
409
|
+
[Travis](https://github.com/lucascaton/enumerate_it/blob/master/.travis.yml).
|
|
410
|
+
|
|
411
|
+
#### Can I set a value to always be at the end of a sorted list?
|
|
412
|
+
|
|
413
|
+
Yes, [see more details here](https://github.com/lucascaton/enumerate_it/issues/60).
|
|
414
|
+
|
|
391
415
|
## I18n
|
|
392
416
|
|
|
393
417
|
I18n lookup is provided on both `_humanized` and `Enumeration#to_a` methods, given the hash key is
|
|
@@ -411,7 +435,7 @@ end
|
|
|
411
435
|
|
|
412
436
|
p = Person.new
|
|
413
437
|
p.relationship_status = RelationshipStatus::MARRIED
|
|
414
|
-
p.relationship_status_humanize
|
|
438
|
+
p.relationship_status_humanize # Existent key
|
|
415
439
|
#=> 'Casado'
|
|
416
440
|
|
|
417
441
|
p.relationship_status = RelationshipStatus::SINGLE
|
|
@@ -427,7 +451,7 @@ RelationshipStatus.t(status)
|
|
|
427
451
|
#=> 'Casado'
|
|
428
452
|
```
|
|
429
453
|
|
|
430
|
-
##
|
|
454
|
+
## Handling a legacy database
|
|
431
455
|
|
|
432
456
|
**EnumerateIt** can help you build a Rails application around a legacy database which was filled
|
|
433
457
|
with those small and unchangeable tables used to create foreign key constraints everywhere, like the
|
|
@@ -458,10 +482,11 @@ key pointing to the `relationship_status` table.
|
|
|
458
482
|
|
|
459
483
|
While this is a good thing from the database normalization perspective, managing these values in
|
|
460
484
|
tests is very hard. Doing database joins just to get the description of some value is absurd.
|
|
461
|
-
And, more than this, referencing them in the code using
|
|
462
|
-
|
|
485
|
+
And, more than this, referencing them in the code using
|
|
486
|
+
[magic numbers](https://en.wikipedia.org/wiki/Magic_number_(programming)) was terrible and
|
|
487
|
+
meaningless: what does it mean when we say that someone or something is `2`?
|
|
463
488
|
|
|
464
|
-
|
|
489
|
+
To solve this, you can pass a **hash** to your enumeration values:
|
|
465
490
|
|
|
466
491
|
```ruby
|
|
467
492
|
class RelationshipStatus < EnumerateIt::Base
|
|
@@ -478,17 +503,7 @@ RelationshipStatus::MARRIED
|
|
|
478
503
|
#=> 2
|
|
479
504
|
```
|
|
480
505
|
|
|
481
|
-
You can also sort it by its value
|
|
482
|
-
|
|
483
|
-
## FAQ
|
|
484
|
-
|
|
485
|
-
#### What versions of Ruby and Rails are supported?
|
|
486
|
-
|
|
487
|
-
Please check out [travis config file](https://github.com/lucascaton/enumerate_it/blob/master/.travis.yml).
|
|
488
|
-
|
|
489
|
-
#### Can I set a value to always be at the end of a sorted list?
|
|
490
|
-
|
|
491
|
-
Yes, please see [issue #60](https://github.com/lucascaton/enumerate_it/issues/60).
|
|
506
|
+
You can also sort it by its **value** using `sort_by :value`.
|
|
492
507
|
|
|
493
508
|
## Changelog
|
|
494
509
|
|
|
@@ -499,11 +514,11 @@ Changes are maintained under [Releases](https://github.com/lucascaton/enumerate_
|
|
|
499
514
|
* Fork the project.
|
|
500
515
|
* Make your feature addition or bug fix.
|
|
501
516
|
* Add tests for it. This is important so we don't break it in a future version unintentionally.
|
|
502
|
-
* [Optional] Run the tests agaist a specific Gemfile: `$ appraisal rails_5.0 rake spec`.
|
|
503
|
-
* Run the tests agaist all supported versions: `$ rake
|
|
517
|
+
* [Optional] Run the tests agaist a specific Gemfile: `$ bundle exec appraisal rails_5.0 rake spec`.
|
|
518
|
+
* Run the tests agaist all supported versions: `$ bundle exec rake` (or `$ bundle exec wwtd`)
|
|
504
519
|
* Commit, but please do not mess with `Rakefile`, version, or history.
|
|
505
520
|
* Send a Pull Request. Bonus points for topic branches.
|
|
506
521
|
|
|
507
522
|
## Copyright
|
|
508
523
|
|
|
509
|
-
Copyright (c) 2010-
|
|
524
|
+
Copyright (c) 2010-2017 Cássio Marques and Lucas Caton. See `LICENSE` file for details.
|
data/enumerate_it.gemspec
CHANGED
|
@@ -11,7 +11,7 @@ Gem::Specification.new do |gem|
|
|
|
11
11
|
gem.name = 'enumerate_it'
|
|
12
12
|
gem.require_paths = ['lib']
|
|
13
13
|
gem.version = EnumerateIt::VERSION
|
|
14
|
-
gem.required_ruby_version = '>=
|
|
14
|
+
gem.required_ruby_version = '>= 2.0.0'
|
|
15
15
|
|
|
16
16
|
gem.add_dependency 'activesupport', '>= 3.0.0'
|
|
17
17
|
|
|
@@ -20,6 +20,7 @@ Gem::Specification.new do |gem|
|
|
|
20
20
|
gem.add_development_dependency 'pry'
|
|
21
21
|
gem.add_development_dependency 'rake'
|
|
22
22
|
gem.add_development_dependency 'rspec'
|
|
23
|
+
gem.add_development_dependency 'wwtd'
|
|
23
24
|
|
|
24
25
|
if RUBY_VERSION >= '2.2.0'
|
|
25
26
|
gem.add_development_dependency 'rubocop'
|
data/gemfiles/rails_3.0.gemfile
CHANGED
data/gemfiles/rails_3.1.gemfile
CHANGED
data/gemfiles/rails_3.2.gemfile
CHANGED
data/gemfiles/rails_4.0.gemfile
CHANGED
data/gemfiles/rails_4.1.gemfile
CHANGED
data/gemfiles/rails_4.2.gemfile
CHANGED
data/gemfiles/rails_5.0.gemfile
CHANGED
data/lib/enumerate_it/base.rb
CHANGED
|
@@ -18,7 +18,7 @@ module EnumerateIt
|
|
|
18
18
|
set_validations(attribute, options) unless options[:skip_validation]
|
|
19
19
|
|
|
20
20
|
if options[:create_helpers]
|
|
21
|
-
%w
|
|
21
|
+
%w[create_helper_methods create_mutator_methods create_polymorphic_methods].each do |method|
|
|
22
22
|
send(method, options[:with], attribute, options[:create_helpers])
|
|
23
23
|
end
|
|
24
24
|
end
|
|
@@ -53,12 +53,12 @@ module EnumerateIt
|
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
def create_scopes(klass, attribute_name, helpers)
|
|
56
|
+
return unless respond_to? :scope
|
|
57
|
+
|
|
56
58
|
prefix_name = "#{attribute_name}_" if helpers.is_a?(Hash) && helpers[:prefix]
|
|
57
59
|
|
|
58
60
|
klass.enumeration.keys.each do |key|
|
|
59
|
-
|
|
60
|
-
scope "#{prefix_name}#{key}", -> { where(attribute_name => klass.enumeration[key].first) }
|
|
61
|
-
end
|
|
61
|
+
scope("#{prefix_name}#{key}", -> { where(attribute_name => klass.enumeration[key].first) })
|
|
62
62
|
end
|
|
63
63
|
end
|
|
64
64
|
|
data/lib/enumerate_it/version.rb
CHANGED
|
@@ -21,7 +21,7 @@ describe EnumerateIt::Base do
|
|
|
21
21
|
|
|
22
22
|
describe '.list' do
|
|
23
23
|
it "creates a method that returns the allowed values in the enumeration's class" do
|
|
24
|
-
expect(TestEnumeration.list).to eq(%w
|
|
24
|
+
expect(TestEnumeration.list).to eq(%w[1 2 3])
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
context 'specifying a default sort mode' do
|
|
@@ -30,25 +30,25 @@ describe EnumerateIt::Base do
|
|
|
30
30
|
context 'by value' do
|
|
31
31
|
let(:sort_mode) { :value }
|
|
32
32
|
|
|
33
|
-
it { is_expected.to eq(%w
|
|
33
|
+
it { is_expected.to eq(%w[0 1 2 3]) }
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
context 'by name' do
|
|
37
37
|
let(:sort_mode) { :name }
|
|
38
38
|
|
|
39
|
-
it { is_expected.to eq(%w
|
|
39
|
+
it { is_expected.to eq(%w[2 1 3 0]) }
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
context 'by translation' do
|
|
43
43
|
let(:sort_mode) { :translation }
|
|
44
44
|
|
|
45
|
-
it { is_expected.to eq(%w
|
|
45
|
+
it { is_expected.to eq(%w[3 2 0 1]) }
|
|
46
46
|
end
|
|
47
47
|
|
|
48
48
|
context 'by nothing' do
|
|
49
49
|
let(:sort_mode) { :none }
|
|
50
50
|
|
|
51
|
-
it { is_expected.to eq(%w
|
|
51
|
+
it { is_expected.to eq(%w[1 2 3 0]) }
|
|
52
52
|
end
|
|
53
53
|
end
|
|
54
54
|
end
|
|
@@ -107,7 +107,7 @@ describe EnumerateIt::Base do
|
|
|
107
107
|
end
|
|
108
108
|
|
|
109
109
|
it 'can be extended from the enumeration class' do
|
|
110
|
-
expect(TestEnumerationWithExtendedBehaviour.to_a).to eq([%w
|
|
110
|
+
expect(TestEnumerationWithExtendedBehaviour.to_a).to eq([%w[Second 2], %w[First 1]])
|
|
111
111
|
end
|
|
112
112
|
end
|
|
113
113
|
|
|
@@ -140,12 +140,12 @@ describe EnumerateIt::Base do
|
|
|
140
140
|
|
|
141
141
|
describe '.values_for' do
|
|
142
142
|
it "returns an array representing some of the enumeration's values" do
|
|
143
|
-
expect(TestEnumeration.values_for(%w
|
|
143
|
+
expect(TestEnumeration.values_for(%w[VALUE_1 VALUE_2]))
|
|
144
144
|
.to eq([TestEnumeration::VALUE_1, TestEnumeration::VALUE_2])
|
|
145
145
|
end
|
|
146
146
|
|
|
147
147
|
it 'returns nil if the a constant named after one of the given strings cannot be found' do
|
|
148
|
-
expect(TestEnumeration.values_for(%w
|
|
148
|
+
expect(TestEnumeration.values_for(%w[VALUE_1 THIS_IS_WRONG]))
|
|
149
149
|
.to eq([TestEnumeration::VALUE_1, nil])
|
|
150
150
|
end
|
|
151
151
|
end
|
|
@@ -182,7 +182,7 @@ describe EnumerateIt::Base do
|
|
|
182
182
|
|
|
183
183
|
describe '.keys' do
|
|
184
184
|
it 'returns a list with the keys used to define the enumeration' do
|
|
185
|
-
expect(TestEnumeration.keys).to eq([
|
|
185
|
+
expect(TestEnumeration.keys).to eq(%i[value_1 value_2 value_3])
|
|
186
186
|
end
|
|
187
187
|
end
|
|
188
188
|
|
|
@@ -203,7 +203,7 @@ describe EnumerateIt::Base do
|
|
|
203
203
|
end
|
|
204
204
|
|
|
205
205
|
it 'returns an array with the values and human representations' do
|
|
206
|
-
expect(TestEnumerationWithList.to_a).to eq([%w
|
|
206
|
+
expect(TestEnumerationWithList.to_a).to eq([%w[First first], %w[Second second]])
|
|
207
207
|
end
|
|
208
208
|
end
|
|
209
209
|
|
|
@@ -211,7 +211,7 @@ describe EnumerateIt::Base do
|
|
|
211
211
|
subject { create_enumeration_class_with_sort_mode(nil).to_a }
|
|
212
212
|
|
|
213
213
|
it 'does not sort' do
|
|
214
|
-
is_expected.to eq([%w
|
|
214
|
+
is_expected.to eq([%w[xyz 1], %w[fgh 2], %w[abc 3], %w[jkl 0]])
|
|
215
215
|
end
|
|
216
216
|
end
|
|
217
217
|
|
|
@@ -221,25 +221,25 @@ describe EnumerateIt::Base do
|
|
|
221
221
|
context 'by value' do
|
|
222
222
|
let(:sort_mode) { :value }
|
|
223
223
|
|
|
224
|
-
it { is_expected.to eq([%w
|
|
224
|
+
it { is_expected.to eq([%w[jkl 0], %w[xyz 1], %w[fgh 2], %w[abc 3]]) }
|
|
225
225
|
end
|
|
226
226
|
|
|
227
227
|
context 'by name' do
|
|
228
228
|
let(:sort_mode) { :name }
|
|
229
229
|
|
|
230
|
-
it { is_expected.to eq([%w
|
|
230
|
+
it { is_expected.to eq([%w[fgh 2], %w[xyz 1], %w[abc 3], %w[jkl 0]]) }
|
|
231
231
|
end
|
|
232
232
|
|
|
233
233
|
context 'by translation' do
|
|
234
234
|
let(:sort_mode) { :translation }
|
|
235
235
|
|
|
236
|
-
it { is_expected.to eq([%w
|
|
236
|
+
it { is_expected.to eq([%w[abc 3], %w[fgh 2], %w[jkl 0], %w[xyz 1]]) }
|
|
237
237
|
end
|
|
238
238
|
|
|
239
239
|
context 'by nothing' do
|
|
240
240
|
let(:sort_mode) { :none }
|
|
241
241
|
|
|
242
|
-
it { is_expected.to eq([%w
|
|
242
|
+
it { is_expected.to eq([%w[xyz 1], %w[fgh 2], %w[abc 3], %w[jkl 0]]) }
|
|
243
243
|
end
|
|
244
244
|
end
|
|
245
245
|
|
|
@@ -291,7 +291,7 @@ describe EnumerateIt::Base do
|
|
|
291
291
|
end
|
|
292
292
|
end
|
|
293
293
|
|
|
294
|
-
it '
|
|
294
|
+
it 'does not require the attribute by default' do
|
|
295
295
|
expect(ActiveRecordStub).not_to receive(:validates_presence_of)
|
|
296
296
|
class ActiveRecordStub
|
|
297
297
|
has_enumeration_for :bla, with: TestEnumeration
|
data/spec/enumerate_it_spec.rb
CHANGED
|
@@ -120,12 +120,12 @@ describe EnumerateIt do
|
|
|
120
120
|
context 'when using a nested class as the enumeration' do
|
|
121
121
|
before do
|
|
122
122
|
class NestedEnum < EnumerateIt::Base
|
|
123
|
-
associate_values foo: %w
|
|
123
|
+
associate_values foo: %w[1 Fooo], bar: %w[2 Barrrr]
|
|
124
124
|
end
|
|
125
125
|
|
|
126
126
|
class ClassWithNestedEnum
|
|
127
127
|
class NestedEnum < EnumerateIt::Base
|
|
128
|
-
associate_values foo: %w
|
|
128
|
+
associate_values foo: %w[1 Blerrgh], bar: ['2' => 'Blarghhh']
|
|
129
129
|
end
|
|
130
130
|
|
|
131
131
|
extend EnumerateIt
|
|
@@ -165,7 +165,7 @@ describe EnumerateIt do
|
|
|
165
165
|
end
|
|
166
166
|
|
|
167
167
|
it 'creates a mutator method for each enumeration value' do
|
|
168
|
-
[
|
|
168
|
+
%i[value_1 value_2 value_3].each do |value|
|
|
169
169
|
expect(TestClassWithHelper.new(TestEnumeration::VALUE_1)).to respond_to(:"#{value}!")
|
|
170
170
|
end
|
|
171
171
|
end
|
|
@@ -189,7 +189,7 @@ describe EnumerateIt do
|
|
|
189
189
|
end
|
|
190
190
|
|
|
191
191
|
it 'creates a mutator method for each enumeration value' do
|
|
192
|
-
[
|
|
192
|
+
%i[value_1 value_2 value_3].each do |value|
|
|
193
193
|
expect(TestClassWithHelper.new(TestEnumeration::VALUE_1))
|
|
194
194
|
.to respond_to(:"foobar_#{value}!")
|
|
195
195
|
end
|
|
@@ -275,7 +275,7 @@ describe EnumerateIt do
|
|
|
275
275
|
end
|
|
276
276
|
end
|
|
277
277
|
|
|
278
|
-
context 'when the hosting class
|
|
278
|
+
context 'when the hosting class does not respond to :scope' do
|
|
279
279
|
before do
|
|
280
280
|
class GenericClass
|
|
281
281
|
extend EnumerateIt
|
|
@@ -65,10 +65,10 @@ def create_enumeration_class_with_sort_mode(sort_mode)
|
|
|
65
65
|
sort_by(sort_mode)
|
|
66
66
|
|
|
67
67
|
associate_values(
|
|
68
|
-
foo: %w
|
|
69
|
-
bar: %w
|
|
70
|
-
omg: %w
|
|
71
|
-
zomg: %w
|
|
68
|
+
foo: %w[1 xyz],
|
|
69
|
+
bar: %w[2 fgh],
|
|
70
|
+
omg: %w[3 abc],
|
|
71
|
+
zomg: %w[0 jkl]
|
|
72
72
|
)
|
|
73
73
|
end
|
|
74
74
|
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: 1.
|
|
4
|
+
version: 1.5.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: 2017-04-25 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: activesupport
|
|
@@ -95,6 +95,20 @@ dependencies:
|
|
|
95
95
|
- - ">="
|
|
96
96
|
- !ruby/object:Gem::Version
|
|
97
97
|
version: '0'
|
|
98
|
+
- !ruby/object:Gem::Dependency
|
|
99
|
+
name: wwtd
|
|
100
|
+
requirement: !ruby/object:Gem::Requirement
|
|
101
|
+
requirements:
|
|
102
|
+
- - ">="
|
|
103
|
+
- !ruby/object:Gem::Version
|
|
104
|
+
version: '0'
|
|
105
|
+
type: :development
|
|
106
|
+
prerelease: false
|
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
108
|
+
requirements:
|
|
109
|
+
- - ">="
|
|
110
|
+
- !ruby/object:Gem::Version
|
|
111
|
+
version: '0'
|
|
98
112
|
- !ruby/object:Gem::Dependency
|
|
99
113
|
name: rubocop
|
|
100
114
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -141,6 +155,7 @@ files:
|
|
|
141
155
|
- README.md
|
|
142
156
|
- Rakefile
|
|
143
157
|
- enumerate_it.gemspec
|
|
158
|
+
- gemfiles/.bundle/config
|
|
144
159
|
- gemfiles/rails_3.0.gemfile
|
|
145
160
|
- gemfiles/rails_3.1.gemfile
|
|
146
161
|
- gemfiles/rails_3.2.gemfile
|
|
@@ -176,7 +191,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
176
191
|
requirements:
|
|
177
192
|
- - ">="
|
|
178
193
|
- !ruby/object:Gem::Version
|
|
179
|
-
version:
|
|
194
|
+
version: 2.0.0
|
|
180
195
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
181
196
|
requirements:
|
|
182
197
|
- - ">="
|
|
@@ -184,7 +199,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
184
199
|
version: '0'
|
|
185
200
|
requirements: []
|
|
186
201
|
rubyforge_project:
|
|
187
|
-
rubygems_version: 2.
|
|
202
|
+
rubygems_version: 2.6.11
|
|
188
203
|
signing_key:
|
|
189
204
|
specification_version: 4
|
|
190
205
|
summary: Ruby Enumerations
|