enumerations 2.3.1 → 2.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/test.yml +24 -0
- data/CHANGELOG.md +140 -0
- data/{Migrate.md → MIGRATE.md} +0 -0
- data/{Readme.md → README.md} +37 -9
- data/enumerations.gemspec +2 -2
- data/lib/enumerations.rb +18 -14
- data/lib/enumerations/base.rb +3 -1
- data/lib/enumerations/configuration.rb +8 -2
- data/lib/enumerations/finder_methods.rb +4 -2
- data/lib/enumerations/value.rb +4 -2
- data/lib/enumerations/version.rb +1 -1
- data/test/enumerations_test.rb +14 -0
- data/test/translation_test.rb +9 -0
- metadata +16 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 54447b9967c65df1318cac0ec59daab41b5f41ad118ad0ba32e260b8cbcf36d9
|
4
|
+
data.tar.gz: fa276b02570956e52d22915957154a125585983c5a2923453d3cc0c6146d908f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 63e002298b8365cc55c296d85963b3bf410bcd02501394666d45efbfffd3f0fd70a276e9413a51b3a7c81571599f9958a98a3d0b9d5c7671cff60cfd77002591
|
7
|
+
data.tar.gz: f7e3d0337b42d668fe78a6ea3037ad59820803db1ae65f1ddbcfaf6fcfc1e5d19e6081c0582fc5a1766e27b2c24747901117d78e9f03f2b5e690b87e79a7bb8f
|
@@ -0,0 +1,24 @@
|
|
1
|
+
name: Test suite
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [ master ]
|
6
|
+
pull_request:
|
7
|
+
branches: [ master ]
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
test:
|
11
|
+
name: Ruby ${{ matrix.ruby }}
|
12
|
+
runs-on: ubuntu-18.04
|
13
|
+
strategy:
|
14
|
+
matrix:
|
15
|
+
ruby: [2.6, 2.7, 3.0]
|
16
|
+
|
17
|
+
steps:
|
18
|
+
- uses: actions/checkout@v2
|
19
|
+
- uses: ruby/setup-ruby@v1
|
20
|
+
with:
|
21
|
+
ruby-version: ${{ matrix.ruby }}
|
22
|
+
bundler-cache: true
|
23
|
+
- name: Run tests
|
24
|
+
run: bundle exec rake test
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
# Change Log
|
2
|
+
|
3
|
+
## [v2.5.1](https://github.com/infinum/enumerations/tree/v2.5.1) (YYYY-MM-DD)
|
4
|
+
[Full Changelog](https://github.com/infinum/enumerations/compare/v2.5.0...v2.5.1)
|
5
|
+
|
6
|
+
**Implemented enhancements:**
|
7
|
+
|
8
|
+
- Add support for Ruby 3 [\#52](https://github.com/infinum/enumerations/issues/52)
|
9
|
+
|
10
|
+
## [v2.5.0](https://github.com/infinum/enumerations/tree/v2.5.0) (2021-03-03)
|
11
|
+
[Full Changelog](https://github.com/infinum/enumerations/compare/v2.4.0...v2.5.0)
|
12
|
+
|
13
|
+
**Implemented enhancements:**
|
14
|
+
|
15
|
+
- Add `raise_invalid_value_error` configuration option to disable raising errors on invalid values
|
16
|
+
|
17
|
+
## [v2.4.0](https://github.com/infinum/enumerations/tree/v2.4.0) (2019-02-07)
|
18
|
+
[Full Changelog](https://github.com/infinum/enumerations/compare/v2.3.3...v2.4.0)
|
19
|
+
|
20
|
+
**Implemented enhancements:**
|
21
|
+
|
22
|
+
- Add `translate_attributes` configuration option to disable translation of attributes
|
23
|
+
|
24
|
+
## [v2.3.3](https://github.com/infinum/enumerations/tree/v2.3.1) (2019-19-09)
|
25
|
+
[Full Changelog](https://github.com/infinum/enumerations/compare/v2.3.2...v2.3.3)
|
26
|
+
|
27
|
+
## [v2.3.2](https://github.com/infinum/enumerations/tree/v2.3.1) (2019-03-27)
|
28
|
+
[Full Changelog](https://github.com/infinum/enumerations/compare/v2.3.1...v2.3.2)
|
29
|
+
|
30
|
+
## [v2.3.1](https://github.com/infinum/enumerations/tree/v2.3.1) (2017-09-08)
|
31
|
+
[Full Changelog](https://github.com/infinum/enumerations/compare/v2.3.0...v2.3.1)
|
32
|
+
|
33
|
+
## [v2.3.0](https://github.com/infinum/enumerations/tree/v2.3.0) (2017-09-08)
|
34
|
+
[Full Changelog](https://github.com/infinum/enumerations/compare/v2.2.3...v2.3.0)
|
35
|
+
|
36
|
+
**Implemented enhancements:**
|
37
|
+
|
38
|
+
- Setting attribute to nil if enumeration value is unknown - surprising? [\#35](https://github.com/infinum/enumerations/issues/35)
|
39
|
+
- Add `without` methods [\#31](https://github.com/infinum/enumerations/issues/31)
|
40
|
+
- Add methods with '?' [\#19](https://github.com/infinum/enumerations/issues/19)
|
41
|
+
- Add with\_enumeration\(enumeration\) scope [\#18](https://github.com/infinum/enumerations/issues/18)
|
42
|
+
|
43
|
+
**Closed issues:**
|
44
|
+
|
45
|
+
- Not assigning the enumeration when tweaking the primary and foreign key [\#32](https://github.com/infinum/enumerations/issues/32)
|
46
|
+
- Undocumented feature - can't set attribute if it's not in the enumeration [\#30](https://github.com/infinum/enumerations/issues/30)
|
47
|
+
|
48
|
+
**Merged pull requests:**
|
49
|
+
|
50
|
+
- Feature/raising error on invalid enumeration value [\#38](https://github.com/infinum/enumerations/pull/38) ([PetarCurkovic](https://github.com/PetarCurkovic))
|
51
|
+
- Bugfix/config options [\#37](https://github.com/infinum/enumerations/pull/37) ([PetarCurkovic](https://github.com/PetarCurkovic))
|
52
|
+
- Feature/predicate methods for attributes [\#36](https://github.com/infinum/enumerations/pull/36) ([PetarCurkovic](https://github.com/PetarCurkovic))
|
53
|
+
- Feature/without scopes [\#34](https://github.com/infinum/enumerations/pull/34) ([PetarCurkovic](https://github.com/PetarCurkovic))
|
54
|
+
- Feature/with enumeration scope [\#33](https://github.com/infinum/enumerations/pull/33) ([PetarCurkovic](https://github.com/PetarCurkovic))
|
55
|
+
|
56
|
+
## [v2.2.3](https://github.com/infinum/enumerations/tree/v2.2.3) (2017-03-17)
|
57
|
+
[Full Changelog](https://github.com/infinum/enumerations/compare/v2.2.2...v2.2.3)
|
58
|
+
|
59
|
+
## [v2.2.2](https://github.com/infinum/enumerations/tree/v2.2.2) (2017-03-01)
|
60
|
+
[Full Changelog](https://github.com/infinum/enumerations/compare/v2.2.1...v2.2.2)
|
61
|
+
|
62
|
+
**Implemented enhancements:**
|
63
|
+
|
64
|
+
- Add id and quoted\_id methods [\#20](https://github.com/infinum/enumerations/issues/20)
|
65
|
+
|
66
|
+
**Fixed bugs:**
|
67
|
+
|
68
|
+
- Error when assiging nil [\#21](https://github.com/infinum/enumerations/issues/21)
|
69
|
+
|
70
|
+
**Closed issues:**
|
71
|
+
|
72
|
+
- upgrade to new codeclimate reporter [\#25](https://github.com/infinum/enumerations/issues/25)
|
73
|
+
- Giving a invalid enum name raises an error [\#23](https://github.com/infinum/enumerations/issues/23)
|
74
|
+
- Make second argument to Enumerations.value optional [\#22](https://github.com/infinum/enumerations/issues/22)
|
75
|
+
|
76
|
+
**Merged pull requests:**
|
77
|
+
|
78
|
+
- Make second argument to Enumerations.value optional [\#28](https://github.com/infinum/enumerations/pull/28) ([domagojnakic](https://github.com/domagojnakic))
|
79
|
+
- Fix giving invalid enum name raises error [\#27](https://github.com/infinum/enumerations/pull/27) ([domagojnakic](https://github.com/domagojnakic))
|
80
|
+
- Replaced CodeClimate::TestReporter with SimpleCov [\#26](https://github.com/infinum/enumerations/pull/26) ([PetarCurkovic](https://github.com/PetarCurkovic))
|
81
|
+
- Add migration readme [\#24](https://github.com/infinum/enumerations/pull/24) ([Narayanan170](https://github.com/Narayanan170))
|
82
|
+
|
83
|
+
## [v2.2.1](https://github.com/infinum/enumerations/tree/v2.2.1) (2016-09-20)
|
84
|
+
[Full Changelog](https://github.com/infinum/enumerations/compare/v2.2.0...v2.2.1)
|
85
|
+
|
86
|
+
**Closed issues:**
|
87
|
+
|
88
|
+
- Add option to store foreign\_key as string [\#14](https://github.com/infinum/enumerations/issues/14)
|
89
|
+
|
90
|
+
**Merged pull requests:**
|
91
|
+
|
92
|
+
- Bugfix/empty initializer [\#17](https://github.com/infinum/enumerations/pull/17) ([PetarCurkovic](https://github.com/PetarCurkovic))
|
93
|
+
|
94
|
+
## [v2.2.0](https://github.com/infinum/enumerations/tree/v2.2.0) (2016-09-19)
|
95
|
+
[Full Changelog](https://github.com/infinum/enumerations/compare/v2.1.0...v2.2.0)
|
96
|
+
|
97
|
+
**Merged pull requests:**
|
98
|
+
|
99
|
+
- Feature/remove [\#16](https://github.com/infinum/enumerations/pull/16) ([PetarCurkovic](https://github.com/PetarCurkovic))
|
100
|
+
|
101
|
+
## [v2.1.0](https://github.com/infinum/enumerations/tree/v2.1.0) (2016-08-19)
|
102
|
+
[Full Changelog](https://github.com/infinum/enumerations/compare/v2.0.0...v2.1.0)
|
103
|
+
|
104
|
+
**Closed issues:**
|
105
|
+
|
106
|
+
- Moved all back to Base [\#9](https://github.com/infinum/enumerations/issues/9)
|
107
|
+
- I18n.locale [\#6](https://github.com/infinum/enumerations/issues/6)
|
108
|
+
- Case statement [\#3](https://github.com/infinum/enumerations/issues/3)
|
109
|
+
|
110
|
+
**Merged pull requests:**
|
111
|
+
|
112
|
+
- Feature/where filtering [\#13](https://github.com/infinum/enumerations/pull/13) ([PetarCurkovic](https://github.com/PetarCurkovic))
|
113
|
+
- Feature/localized attributes [\#12](https://github.com/infinum/enumerations/pull/12) ([PetarCurkovic](https://github.com/PetarCurkovic))
|
114
|
+
|
115
|
+
## [v2.0.0](https://github.com/infinum/enumerations/tree/v2.0.0) (2016-08-17)
|
116
|
+
[Full Changelog](https://github.com/infinum/enumerations/compare/v1.3.0...v2.0.0)
|
117
|
+
|
118
|
+
**Merged pull requests:**
|
119
|
+
|
120
|
+
- Update version to 2.0.0 [\#11](https://github.com/infinum/enumerations/pull/11) ([PetarCurkovic](https://github.com/PetarCurkovic))
|
121
|
+
- Refactor [\#10](https://github.com/infinum/enumerations/pull/10) ([PetarCurkovic](https://github.com/PetarCurkovic))
|
122
|
+
|
123
|
+
## [v1.3.0](https://github.com/infinum/enumerations/tree/v1.3.0) (2016-08-11)
|
124
|
+
[Full Changelog](https://github.com/infinum/enumerations/compare/v1.1.1...v1.3.0)
|
125
|
+
|
126
|
+
## [v1.1.1](https://github.com/infinum/enumerations/tree/v1.1.1) (2016-06-07)
|
127
|
+
[Full Changelog](https://github.com/infinum/enumerations/compare/v1.1.0...v1.1.1)
|
128
|
+
|
129
|
+
**Merged pull requests:**
|
130
|
+
|
131
|
+
- Bugfix/value-id [\#8](https://github.com/infinum/enumerations/pull/8) ([nikajukic](https://github.com/nikajukic))
|
132
|
+
- Document configuration options [\#7](https://github.com/infinum/enumerations/pull/7) ([stankec](https://github.com/stankec))
|
133
|
+
- Refactoring all around [\#5](https://github.com/infinum/enumerations/pull/5) ([janvarljen](https://github.com/janvarljen))
|
134
|
+
- Added value method and support for custom attributes [\#4](https://github.com/infinum/enumerations/pull/4) ([PetarCurkovic](https://github.com/PetarCurkovic))
|
135
|
+
- Some refactoring and updated README [\#2](https://github.com/infinum/enumerations/pull/2) ([PetarCurkovic](https://github.com/PetarCurkovic))
|
136
|
+
|
137
|
+
## [v1.1.0](https://github.com/infinum/enumerations/tree/v1.1.0) (2012-08-19)
|
138
|
+
**Merged pull requests:**
|
139
|
+
|
140
|
+
- Gemspec. [\#1](https://github.com/infinum/enumerations/pull/1) ([neektza](https://github.com/neektza))
|
data/{Migrate.md → MIGRATE.md}
RENAMED
File without changes
|
data/{Readme.md → README.md}
RENAMED
@@ -2,9 +2,9 @@ Enumerations
|
|
2
2
|
============
|
3
3
|
|
4
4
|
[![Gem Version](https://badge.fury.io/rb/enumerations.svg)](https://badge.fury.io/rb/enumerations)
|
5
|
-
[![
|
5
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/c3b96c5afceaa9be2173/maintainability)](https://codeclimate.com/github/infinum/enumerations/maintainability)
|
6
|
+
[![Test Coverage](https://api.codeclimate.com/v1/badges/c3b96c5afceaa9be2173/test_coverage)](https://codeclimate.com/github/infinum/enumerations/test_coverage)
|
6
7
|
[![Build Status](https://semaphoreci.com/api/v1/infinum/enumerations/branches/master/shields_badge.svg)](https://semaphoreci.com/infinum/enumerations)
|
7
|
-
[![Test Coverage](https://codeclimate.com/github/infinum/enumerations/badges/coverage.svg)](https://codeclimate.com/github/infinum/enumerations/coverage)
|
8
8
|
|
9
9
|
Rails plugin for Enumerations in ActiveRecord models.
|
10
10
|
|
@@ -83,7 +83,7 @@ Or you can set enumerations by `symbol`:
|
|
83
83
|
@post.status = Status.find(:draft)
|
84
84
|
```
|
85
85
|
|
86
|
-
> If you try to set value that is not an Enumeration value (except `nil`), you will get an `Enumerations::InvalidValueError` exception.
|
86
|
+
> If you try to set value that is not an Enumeration value (except `nil`), you will get an `Enumerations::InvalidValueError` exception. You can turn this exception off in configuration.
|
87
87
|
|
88
88
|
Also, you can set enumeration value like this:
|
89
89
|
|
@@ -230,6 +230,32 @@ Use in forms:
|
|
230
230
|
= f.collection_select :status, Status.all, :symbol, :name
|
231
231
|
```
|
232
232
|
|
233
|
+
|
234
|
+
|
235
|
+
## Validating input
|
236
|
+
|
237
|
+
Enumerations will by default raise an exception if you try to set an invalid value. This prevents usage of validations, which you might want to add if you're developing an API and have to return meaningful errors to API clients.
|
238
|
+
|
239
|
+
You can enable validations by first disabling error raising on invalid input (see [configuration](#configuration)). Then, you should add an inclusion validation to enumerated attributes:
|
240
|
+
```ruby
|
241
|
+
class Post < ActiveRecord::Base
|
242
|
+
enumeration :status
|
243
|
+
|
244
|
+
validates :status, inclusion: { in: Status.all }
|
245
|
+
end
|
246
|
+
```
|
247
|
+
|
248
|
+
You'll now get an appropriate error message when you insert an invalid value:
|
249
|
+
```ruby
|
250
|
+
> post = Post.new(status: 'invalid')
|
251
|
+
> post.valid?
|
252
|
+
=> false
|
253
|
+
> post.errors.full_messages.to_sentence
|
254
|
+
=> "Status is not included in the list"
|
255
|
+
> post.status
|
256
|
+
=> "invalid"
|
257
|
+
```
|
258
|
+
|
233
259
|
Advanced Usage
|
234
260
|
=====
|
235
261
|
|
@@ -265,7 +291,7 @@ Status.other.name? # => false
|
|
265
291
|
Translations
|
266
292
|
=====
|
267
293
|
|
268
|
-
**Enumerations** uses power of I18n API to enable you to create a locale file
|
294
|
+
**Enumerations** uses power of I18n API (if translate_attributes configuration is set to true) to enable you to create a locale file
|
269
295
|
for enumerations like this:
|
270
296
|
|
271
297
|
```yaml
|
@@ -297,8 +323,8 @@ Configuration
|
|
297
323
|
|
298
324
|
Basically no configuration is needed.
|
299
325
|
|
300
|
-
**Enumerations** has
|
301
|
-
You can customize primary key
|
326
|
+
**Enumerations** has four configuration options.
|
327
|
+
You can customize primary key, foreign key suffix, whether to translate attributes and whether to raise `Enumerations::InvalidValueError` exception when setting invalid values.
|
302
328
|
Just add initializer file to `config/initializers/enumerations.rb`.
|
303
329
|
|
304
330
|
Example of configuration:
|
@@ -307,8 +333,10 @@ Example of configuration:
|
|
307
333
|
# config/initializers/enumerations.rb
|
308
334
|
|
309
335
|
Enumerations.configure do |config|
|
310
|
-
config.primary_key
|
311
|
-
config.foreign_key_suffix
|
336
|
+
config.primary_key = :id
|
337
|
+
config.foreign_key_suffix = :id
|
338
|
+
config.translate_attributes = true
|
339
|
+
config.raise_invalid_value_error = true
|
312
340
|
end
|
313
341
|
```
|
314
342
|
|
@@ -399,7 +427,7 @@ Credits
|
|
399
427
|
=======
|
400
428
|
**Enumerations** is maintained and sponsored by [Infinum](https://infinum.co)
|
401
429
|
|
402
|
-
Copyright ©
|
430
|
+
Copyright © 2010 - 2018 Infinum Ltd.
|
403
431
|
|
404
432
|
License
|
405
433
|
=======
|
data/enumerations.gemspec
CHANGED
@@ -22,8 +22,8 @@ Gem::Specification.new do |s|
|
|
22
22
|
s.add_dependency 'i18n'
|
23
23
|
s.add_development_dependency 'pry-byebug'
|
24
24
|
s.add_development_dependency 'rake'
|
25
|
-
s.add_development_dependency '
|
26
|
-
s.add_development_dependency 'sqlite3'
|
25
|
+
s.add_development_dependency 'simplecov'
|
26
|
+
s.add_development_dependency 'sqlite3', '~> 1.4.0'
|
27
27
|
|
28
28
|
s.files = `git ls-files`.split("\n")
|
29
29
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
data/lib/enumerations.rb
CHANGED
@@ -52,6 +52,15 @@ module Enumerations
|
|
52
52
|
_enumerations
|
53
53
|
end
|
54
54
|
|
55
|
+
def fetch_foreign_key_values(reflection, *symbols)
|
56
|
+
symbols.flatten.map do |symbol|
|
57
|
+
enumeration_value = reflection.enumerator_class.find(symbol)
|
58
|
+
|
59
|
+
enumeration_value &&
|
60
|
+
enumeration_value.send(reflection.enumerator_class.primary_key || :symbol)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
55
64
|
private
|
56
65
|
|
57
66
|
def add_enumeration(reflection)
|
@@ -73,7 +82,7 @@ module Enumerations
|
|
73
82
|
#
|
74
83
|
def define_getter_method(reflection)
|
75
84
|
define_method(reflection.name) do
|
76
|
-
reflection.enumerator_class.find(self[reflection.foreign_key])
|
85
|
+
reflection.enumerator_class.find(self[reflection.foreign_key]) || self[reflection.foreign_key]
|
77
86
|
end
|
78
87
|
end
|
79
88
|
|
@@ -87,11 +96,15 @@ module Enumerations
|
|
87
96
|
define_method("#{reflection.name}=") do |other|
|
88
97
|
enumeration_value = reflection.enumerator_class.find(other)
|
89
98
|
|
90
|
-
|
99
|
+
if other.present? && enumeration_value.nil?
|
100
|
+
raise Enumerations::InvalidValueError if Enumerations.configuration.raise_invalid_value_error
|
91
101
|
|
92
|
-
|
93
|
-
|
94
|
-
|
102
|
+
self[reflection.foreign_key] = other
|
103
|
+
else
|
104
|
+
self[reflection.foreign_key] =
|
105
|
+
enumeration_value &&
|
106
|
+
enumeration_value.send(reflection.enumerator_class.primary_key || :symbol)
|
107
|
+
end
|
95
108
|
end
|
96
109
|
end
|
97
110
|
|
@@ -150,15 +163,6 @@ module Enumerations
|
|
150
163
|
where.not(reflection.foreign_key => fetch_foreign_key_values(reflection, symbols))
|
151
164
|
end)
|
152
165
|
end
|
153
|
-
|
154
|
-
def fetch_foreign_key_values(reflection, *symbols)
|
155
|
-
symbols.flatten.map do |symbol|
|
156
|
-
enumeration_value = reflection.enumerator_class.find(symbol)
|
157
|
-
|
158
|
-
enumeration_value &&
|
159
|
-
enumeration_value.send(reflection.enumerator_class.primary_key || :symbol)
|
160
|
-
end
|
161
|
-
end
|
162
166
|
end
|
163
167
|
end
|
164
168
|
|
data/lib/enumerations/base.rb
CHANGED
@@ -4,7 +4,7 @@ require 'enumerations/value'
|
|
4
4
|
require 'enumerations/finder_methods'
|
5
5
|
|
6
6
|
module Enumerations
|
7
|
-
class Base
|
7
|
+
class Base < ActiveSupport::Multibyte::Chars
|
8
8
|
extend Enumerations::FinderMethods
|
9
9
|
include Enumerations::Value
|
10
10
|
|
@@ -126,6 +126,8 @@ module Enumerations
|
|
126
126
|
attr_reader :symbol, :attributes
|
127
127
|
|
128
128
|
def initialize(symbol, attributes)
|
129
|
+
super(symbol)
|
130
|
+
|
129
131
|
@symbol = symbol
|
130
132
|
@attributes = attributes
|
131
133
|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Enumerations
|
2
4
|
def self.configuration
|
3
5
|
@configuration ||= Configuration.new
|
@@ -14,10 +16,14 @@ module Enumerations
|
|
14
16
|
class Configuration
|
15
17
|
attr_accessor :primary_key
|
16
18
|
attr_accessor :foreign_key_suffix
|
19
|
+
attr_accessor :translate_attributes
|
20
|
+
attr_accessor :raise_invalid_value_error
|
17
21
|
|
18
22
|
def initialize
|
19
|
-
@primary_key
|
20
|
-
@foreign_key_suffix
|
23
|
+
@primary_key = nil
|
24
|
+
@foreign_key_suffix = nil
|
25
|
+
@translate_attributes = true
|
26
|
+
@raise_invalid_value_error = true
|
21
27
|
end
|
22
28
|
end
|
23
29
|
end
|
@@ -31,7 +31,7 @@ module Enumerations
|
|
31
31
|
# Role.find_by(name: 'Admin') => #<Enumerations::Value: @base=Role, @symbol=:admin...>
|
32
32
|
#
|
33
33
|
def find_by(**args)
|
34
|
-
where(args).first
|
34
|
+
where(**args).first
|
35
35
|
end
|
36
36
|
|
37
37
|
def find_by_key(key)
|
@@ -39,7 +39,9 @@ module Enumerations
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def find_by_primary_key(primary_key)
|
42
|
-
value_from_symbol_index(primary_key)
|
42
|
+
return value_from_symbol_index(primary_key.to_i) if primary_key.is_a?(String)
|
43
|
+
|
44
|
+
value_from_symbol_index(primary_key)
|
43
45
|
end
|
44
46
|
|
45
47
|
private
|
data/lib/enumerations/value.rb
CHANGED
@@ -54,7 +54,7 @@ module Enumerations
|
|
54
54
|
#
|
55
55
|
def define_attributes_getters
|
56
56
|
@attributes.each do |key, _|
|
57
|
-
next if
|
57
|
+
next if self.class.method_defined?(key)
|
58
58
|
|
59
59
|
self.class.send :define_method, key do |locale: I18n.locale|
|
60
60
|
case @attributes[key]
|
@@ -66,6 +66,8 @@ module Enumerations
|
|
66
66
|
end
|
67
67
|
|
68
68
|
def translate_attribute(key, locale)
|
69
|
+
return @attributes[key].to_s unless Enumerations.configuration.translate_attributes
|
70
|
+
|
69
71
|
I18n.t(key, scope: [:enumerations, self.class.name.demodulize.underscore, symbol],
|
70
72
|
default: @attributes[key].to_s,
|
71
73
|
locale: locale)
|
@@ -100,7 +102,7 @@ module Enumerations
|
|
100
102
|
@attributes.each do |key, _|
|
101
103
|
method_name = "#{key}?"
|
102
104
|
|
103
|
-
next if
|
105
|
+
next if self.class.method_defined?(method_name.to_sym)
|
104
106
|
|
105
107
|
self.class.send :define_method, method_name do
|
106
108
|
@attributes[key].present?
|
data/lib/enumerations/version.rb
CHANGED
data/test/enumerations_test.rb
CHANGED
@@ -133,6 +133,20 @@ class EnumerationsTest < Minitest::Test
|
|
133
133
|
end
|
134
134
|
end
|
135
135
|
|
136
|
+
def test_nonexistent_value_assignment_with_error_raising_turned_off
|
137
|
+
Enumerations.configuration.raise_invalid_value_error = false
|
138
|
+
|
139
|
+
user = User.new(role: :nonexistent_value)
|
140
|
+
|
141
|
+
assert_equal user.role, 'nonexistent_value'
|
142
|
+
|
143
|
+
user.role = :other_nonexistent_value
|
144
|
+
|
145
|
+
assert_equal user.role, 'other_nonexistent_value'
|
146
|
+
|
147
|
+
Enumerations.configuration.raise_invalid_value_error = true
|
148
|
+
end
|
149
|
+
|
136
150
|
def test_on_nil_value_assignment
|
137
151
|
user = User.new(role: nil)
|
138
152
|
assert_nil user.role
|
data/test/translation_test.rb
CHANGED
@@ -91,4 +91,13 @@ class TranslationTest < Minitest::Test
|
|
91
91
|
|
92
92
|
assert_nil status
|
93
93
|
end
|
94
|
+
|
95
|
+
def test_turn_of_translate
|
96
|
+
status = Status.find(:draft)
|
97
|
+
Enumerations.configuration.translate_attributes = false
|
98
|
+
name = status.name
|
99
|
+
Enumerations.configuration.translate_attributes = true
|
100
|
+
|
101
|
+
assert_equal name, 'Draft'
|
102
|
+
end
|
94
103
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumerations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomislav Car
|
@@ -85,33 +85,33 @@ dependencies:
|
|
85
85
|
- !ruby/object:Gem::Version
|
86
86
|
version: '0'
|
87
87
|
- !ruby/object:Gem::Dependency
|
88
|
-
name:
|
88
|
+
name: simplecov
|
89
89
|
requirement: !ruby/object:Gem::Requirement
|
90
90
|
requirements:
|
91
|
-
- - "
|
91
|
+
- - ">="
|
92
92
|
- !ruby/object:Gem::Version
|
93
|
-
version: '
|
93
|
+
version: '0'
|
94
94
|
type: :development
|
95
95
|
prerelease: false
|
96
96
|
version_requirements: !ruby/object:Gem::Requirement
|
97
97
|
requirements:
|
98
|
-
- - "
|
98
|
+
- - ">="
|
99
99
|
- !ruby/object:Gem::Version
|
100
|
-
version: '
|
100
|
+
version: '0'
|
101
101
|
- !ruby/object:Gem::Dependency
|
102
102
|
name: sqlite3
|
103
103
|
requirement: !ruby/object:Gem::Requirement
|
104
104
|
requirements:
|
105
|
-
- - "
|
105
|
+
- - "~>"
|
106
106
|
- !ruby/object:Gem::Version
|
107
|
-
version:
|
107
|
+
version: 1.4.0
|
108
108
|
type: :development
|
109
109
|
prerelease: false
|
110
110
|
version_requirements: !ruby/object:Gem::Requirement
|
111
111
|
requirements:
|
112
|
-
- - "
|
112
|
+
- - "~>"
|
113
113
|
- !ruby/object:Gem::Version
|
114
|
-
version:
|
114
|
+
version: 1.4.0
|
115
115
|
description: Extends ActiveRecord with enumeration capabilites.
|
116
116
|
email:
|
117
117
|
- tomislav@infinum.hr
|
@@ -123,12 +123,14 @@ executables: []
|
|
123
123
|
extensions: []
|
124
124
|
extra_rdoc_files: []
|
125
125
|
files:
|
126
|
+
- ".github/workflows/test.yml"
|
126
127
|
- ".gitignore"
|
127
128
|
- ".rubocop.yml"
|
129
|
+
- CHANGELOG.md
|
128
130
|
- Gemfile
|
129
|
-
-
|
131
|
+
- MIGRATE.md
|
132
|
+
- README.md
|
130
133
|
- Rakefile
|
131
|
-
- Readme.md
|
132
134
|
- enumerations.gemspec
|
133
135
|
- lib/enumerations.rb
|
134
136
|
- lib/enumerations/base.rb
|
@@ -171,24 +173,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
171
173
|
- !ruby/object:Gem::Version
|
172
174
|
version: '0'
|
173
175
|
requirements: []
|
174
|
-
|
175
|
-
rubygems_version: 2.6.11
|
176
|
+
rubygems_version: 3.0.1
|
176
177
|
signing_key:
|
177
178
|
specification_version: 4
|
178
179
|
summary: Enumerations for ActiveRecord!
|
179
|
-
test_files:
|
180
|
-
- test/base_test.rb
|
181
|
-
- test/configuration/configuration.rb
|
182
|
-
- test/configuration/custom_configuration_test.rb
|
183
|
-
- test/configuration/enumerations_test.rb
|
184
|
-
- test/configuration/finder_test.rb
|
185
|
-
- test/configuration_test.rb
|
186
|
-
- test/enumerations_test.rb
|
187
|
-
- test/finder_test.rb
|
188
|
-
- test/helpers/database_helper.rb
|
189
|
-
- test/helpers/locale_helper.rb
|
190
|
-
- test/helpers/test_helper.rb
|
191
|
-
- test/locales/hr.yml
|
192
|
-
- test/reflection_test.rb
|
193
|
-
- test/translation_test.rb
|
194
|
-
- test/value_test.rb
|
180
|
+
test_files: []
|