enumerations 2.5.4 → 3.0.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/test.yml +1 -1
- data/CHANGELOG.md +20 -0
- data/README.md +2 -1
- data/enumerations.gemspec +6 -3
- data/lib/enumerations/base.rb +33 -8
- data/lib/enumerations/reflection.rb +1 -1
- data/lib/enumerations/version.rb +3 -1
- data/test/configuration/enumerations_test.rb +10 -0
- data/test/enumerations_test.rb +11 -1
- data/test/helpers/database_helper.rb +10 -0
- data/test/helpers/test_helper.rb +29 -3
- metadata +7 -10
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1754e7d5747cf0444bf6b19ba7915f8517eecc6d847dc76af169442bb8d31aed
|
|
4
|
+
data.tar.gz: e00f367b9c16a09155ee517163a6803afbed756d6ae24ea2bf945c5b062cbc01
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c43fe2bc75f47f1bd53c17f676a2648a2be94ea1598d42af96df85cb31add41db2cf0faf9a8b2bca47a3bb9fda2849f52be4076386fddbcacafe395e410a9cc2
|
|
7
|
+
data.tar.gz: 9a858dcb732e394343c0f323fcf41433a9caf695d9d6d300f813cdb98f4d5bb4574c9c60eb46fe9be8005f374c7eed67f2a7604a655aa046f83c4bb701a083cf
|
data/.github/workflows/test.yml
CHANGED
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## [v3.0.0](https://github.com/infinum/enumerations/tree/v3.0.0) (2026-01-16)
|
|
4
|
+
[Full Changelog](https://github.com/infinum/enumerations/compare/v2.6.0...v3.0.0)
|
|
5
|
+
|
|
6
|
+
**Breaking change:**
|
|
7
|
+
|
|
8
|
+
- enumeration classes with overridden `to_s` method might not work as expected anymore
|
|
9
|
+
(previously, overriding `to_s` would affect the value saved to the database, now it doesn't anymore)
|
|
10
|
+
|
|
11
|
+
**Implemented enhancements:**
|
|
12
|
+
|
|
13
|
+
- Drop support for ruby 2.6 and 2.7, only versions '>= 3.0' are now supported
|
|
14
|
+
- Resolve `ActiveSupport::Multibyte::Chars` deprecation warning
|
|
15
|
+
|
|
16
|
+
## [v2.6.0](https://github.com/infinum/enumerations/tree/v2.6.0) (2024-10-22)
|
|
17
|
+
[Full Changelog](https://github.com/infinum/enumerations/compare/v2.5.4...v2.6.0)
|
|
18
|
+
|
|
19
|
+
**Implemented enhancements:**
|
|
20
|
+
|
|
21
|
+
- Add ability to specify foreign key suffix on enum class.
|
|
22
|
+
|
|
3
23
|
## [v2.5.4](https://github.com/infinum/enumerations/tree/v2.5.4) (2023-01-09)
|
|
4
24
|
[Full Changelog](https://github.com/infinum/enumerations/compare/v2.5.3...v2.5.4)
|
|
5
25
|
|
data/README.md
CHANGED
|
@@ -379,11 +379,12 @@ post = Post.new
|
|
|
379
379
|
post.status = Status.draft # => post.status_id = 1
|
|
380
380
|
```
|
|
381
381
|
|
|
382
|
-
If you want to configure primary key per enumeration class, you can use `primary_key=` class method:
|
|
382
|
+
If you want to configure primary key or foreign key suffix per enumeration class, you can use `primary_key=` and `foreign_key_suffix=` class method:
|
|
383
383
|
|
|
384
384
|
```ruby
|
|
385
385
|
class Status < Enumerations::Base
|
|
386
386
|
self.primary_key = :id
|
|
387
|
+
self.foreign_key_suffix = :id
|
|
387
388
|
|
|
388
389
|
value :draft, id: 1, name: 'Draft'
|
|
389
390
|
value :review_pending, id: 2, name: 'Review pending'
|
data/enumerations.gemspec
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require File.expand_path('lib/enumerations/version', __dir__)
|
|
2
4
|
|
|
3
5
|
Gem::Specification.new do |s|
|
|
4
6
|
s.name = 'enumerations'
|
|
@@ -8,8 +10,9 @@ Gem::Specification.new do |s|
|
|
|
8
10
|
s.description = 'Extends ActiveRecord with enumeration capabilites.'
|
|
9
11
|
s.homepage = 'https://github.com/infinum/enumerations'
|
|
10
12
|
s.license = 'MIT'
|
|
13
|
+
s.required_ruby_version = '>= 3.0'
|
|
11
14
|
|
|
12
|
-
s.authors
|
|
15
|
+
s.authors = [
|
|
13
16
|
'Tomislav Car', 'Nikica Jokić', 'Nikola Santić', 'Stjepan Hadjić', 'Petar Ćurković'
|
|
14
17
|
]
|
|
15
18
|
|
|
@@ -24,7 +27,7 @@ Gem::Specification.new do |s|
|
|
|
24
27
|
s.add_development_dependency 'pry-byebug'
|
|
25
28
|
s.add_development_dependency 'rake'
|
|
26
29
|
s.add_development_dependency 'simplecov'
|
|
27
|
-
s.add_development_dependency 'sqlite3'
|
|
30
|
+
s.add_development_dependency 'sqlite3'
|
|
28
31
|
|
|
29
32
|
s.files = `git ls-files`.split("\n")
|
|
30
33
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
data/lib/enumerations/base.rb
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'active_support/core_ext/class/attribute'
|
|
2
4
|
require 'active_support/core_ext/string/inflections'
|
|
3
5
|
require 'enumerations/value'
|
|
4
6
|
require 'enumerations/finder_methods'
|
|
5
7
|
|
|
6
8
|
module Enumerations
|
|
7
|
-
class Base <
|
|
9
|
+
class Base < String
|
|
8
10
|
extend Enumerations::FinderMethods
|
|
9
11
|
include Enumerations::Value
|
|
10
12
|
|
|
11
|
-
class_attribute :_values, :_symbol_index, :_primary_key
|
|
13
|
+
class_attribute :_values, :_symbol_index, :_primary_key, :_foreign_key_suffix
|
|
12
14
|
|
|
13
15
|
self._values = {}
|
|
14
16
|
self._symbol_index = {}
|
|
15
17
|
self._primary_key = nil
|
|
18
|
+
self._foreign_key_suffix = nil
|
|
16
19
|
|
|
17
20
|
# Adding new value to enumeration
|
|
18
21
|
#
|
|
@@ -108,6 +111,34 @@ module Enumerations
|
|
|
108
111
|
_primary_key || Enumerations.configuration.primary_key
|
|
109
112
|
end
|
|
110
113
|
|
|
114
|
+
# Sets foreign key suffix for enumeration class.
|
|
115
|
+
#
|
|
116
|
+
# Example:
|
|
117
|
+
#
|
|
118
|
+
# class Status < Enumeration::Base
|
|
119
|
+
# foreign_key_suffix = :id
|
|
120
|
+
# end
|
|
121
|
+
|
|
122
|
+
def self.foreign_key_suffix=(suffix)
|
|
123
|
+
self._foreign_key_suffix = suffix && suffix.to_sym
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# Gets foreign key suffix for enumeration class.
|
|
127
|
+
#
|
|
128
|
+
# Example:
|
|
129
|
+
#
|
|
130
|
+
# Status.foreign_key_suffix => # nil
|
|
131
|
+
#
|
|
132
|
+
# class Status < Enumeration::Base
|
|
133
|
+
# foreign_key_suffix = :id
|
|
134
|
+
# end
|
|
135
|
+
#
|
|
136
|
+
# Status.foreign_key_suffix => # :id
|
|
137
|
+
#
|
|
138
|
+
def self.foreign_key_suffix
|
|
139
|
+
_foreign_key_suffix || Enumerations.configuration.foreign_key_suffix
|
|
140
|
+
end
|
|
141
|
+
|
|
111
142
|
def self.validate_symbol_and_primary_key(symbol, attributes)
|
|
112
143
|
raise Enumerations::DuplicatedSymbolError if find(symbol)
|
|
113
144
|
|
|
@@ -133,11 +164,5 @@ module Enumerations
|
|
|
133
164
|
|
|
134
165
|
create_instance_methods
|
|
135
166
|
end
|
|
136
|
-
|
|
137
|
-
private
|
|
138
|
-
|
|
139
|
-
def chars(string)
|
|
140
|
-
string
|
|
141
|
-
end
|
|
142
167
|
end
|
|
143
168
|
end
|
data/lib/enumerations/version.rb
CHANGED
|
@@ -47,5 +47,15 @@ module Configuration
|
|
|
47
47
|
|
|
48
48
|
assert_equal query_hash, custom_enum_id: 1
|
|
49
49
|
end
|
|
50
|
+
|
|
51
|
+
def test_enumerations_overrides
|
|
52
|
+
assert_equal :id, OverridableStatus.primary_key
|
|
53
|
+
assert_equal :id, OverridableStatus.foreign_key_suffix
|
|
54
|
+
|
|
55
|
+
model = OverridableModel.new
|
|
56
|
+
model.overridable_status = OverridableStatus.draft
|
|
57
|
+
|
|
58
|
+
assert_equal 1, model.overridable_status_id
|
|
59
|
+
end
|
|
50
60
|
end
|
|
51
61
|
end
|
data/test/enumerations_test.rb
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require_relative 'helpers/test_helper'
|
|
2
4
|
require 'enumerations/errors'
|
|
3
5
|
|
|
4
|
-
class EnumerationsTest < Minitest::Test
|
|
6
|
+
class EnumerationsTest < Minitest::Test # rubocop:disable Metrics/ClassLength
|
|
5
7
|
def test_reflect_on_all_enumerations
|
|
6
8
|
enumerations = Post.reflect_on_all_enumerations
|
|
7
9
|
|
|
@@ -157,4 +159,12 @@ class EnumerationsTest < Minitest::Test
|
|
|
157
159
|
user = User.new(role: nil)
|
|
158
160
|
assert_nil user.role
|
|
159
161
|
end
|
|
162
|
+
|
|
163
|
+
def test_type_cast_when_saving_value_to_database
|
|
164
|
+
client = ApiClient.new(api_client_permission: ApiClientPermission.people_first_name)
|
|
165
|
+
client.save
|
|
166
|
+
|
|
167
|
+
# overriding to_s doesn't have an effect on the value saved to the database
|
|
168
|
+
assert_equal 'people_first_name', client[:api_client_permission]
|
|
169
|
+
end
|
|
160
170
|
end
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'logger'
|
|
2
4
|
|
|
3
5
|
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
|
|
@@ -17,4 +19,12 @@ ActiveRecord::Schema.define do
|
|
|
17
19
|
t.integer :custom_enum_id
|
|
18
20
|
t.integer :custom_enum
|
|
19
21
|
end
|
|
22
|
+
|
|
23
|
+
create_table :overridable_models, force: true do |t|
|
|
24
|
+
t.integer :overridable_status_id
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
create_table :api_clients, force: true do |t|
|
|
28
|
+
t.string :api_client_permission
|
|
29
|
+
end
|
|
20
30
|
end
|
data/test/helpers/test_helper.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'simplecov'
|
|
2
4
|
SimpleCov.start
|
|
3
5
|
|
|
@@ -10,9 +12,9 @@ require_relative 'database_helper'
|
|
|
10
12
|
require_relative 'locale_helper'
|
|
11
13
|
|
|
12
14
|
class Status < Enumerations::Base
|
|
13
|
-
values draft:
|
|
14
|
-
review_pending:
|
|
15
|
-
published:
|
|
15
|
+
values draft: { id: 1, name: 'Draft' },
|
|
16
|
+
review_pending: { id: 2, name: 'Review pending' },
|
|
17
|
+
published: { id: 3, name: 'Published' }
|
|
16
18
|
|
|
17
19
|
value :none, id: 4, name: 'None', visible: true, deleted: false
|
|
18
20
|
value :deleted, id: 5, deleted: true
|
|
@@ -29,6 +31,19 @@ class Role < Enumerations::Base
|
|
|
29
31
|
end
|
|
30
32
|
end
|
|
31
33
|
|
|
34
|
+
class ApiClientPermission < Enumerations::Base
|
|
35
|
+
value :people_first_name, id: 'people.first_name'
|
|
36
|
+
value :people_last_name, id: 'people.last_name'
|
|
37
|
+
|
|
38
|
+
def to_s
|
|
39
|
+
id
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
class ApiClient < ActiveRecord::Base
|
|
44
|
+
enumeration :api_client_permission
|
|
45
|
+
end
|
|
46
|
+
|
|
32
47
|
class Post < ActiveRecord::Base
|
|
33
48
|
enumeration :status
|
|
34
49
|
enumeration :different_status, foreign_key: :some_other_status, class_name: 'Status'
|
|
@@ -40,3 +55,14 @@ class User < ActiveRecord::Base
|
|
|
40
55
|
enumeration :role
|
|
41
56
|
enumeration :status
|
|
42
57
|
end
|
|
58
|
+
|
|
59
|
+
class OverridableStatus < Enumerations::Base
|
|
60
|
+
self.primary_key = :id
|
|
61
|
+
self.foreign_key_suffix = :id
|
|
62
|
+
|
|
63
|
+
value :draft, id: 1
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
class OverridableModel < ActiveRecord::Base
|
|
67
|
+
enumeration :overridable_status
|
|
68
|
+
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:
|
|
4
|
+
version: 3.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Tomislav Car
|
|
@@ -9,7 +9,6 @@ authors:
|
|
|
9
9
|
- Nikola Santić
|
|
10
10
|
- Stjepan Hadjić
|
|
11
11
|
- Petar Ćurković
|
|
12
|
-
autorequire:
|
|
13
12
|
bindir: bin
|
|
14
13
|
cert_chain: []
|
|
15
14
|
date: 2017-09-08 00:00:00.000000000 Z
|
|
@@ -102,16 +101,16 @@ dependencies:
|
|
|
102
101
|
name: sqlite3
|
|
103
102
|
requirement: !ruby/object:Gem::Requirement
|
|
104
103
|
requirements:
|
|
105
|
-
- - "
|
|
104
|
+
- - ">="
|
|
106
105
|
- !ruby/object:Gem::Version
|
|
107
|
-
version:
|
|
106
|
+
version: '0'
|
|
108
107
|
type: :development
|
|
109
108
|
prerelease: false
|
|
110
109
|
version_requirements: !ruby/object:Gem::Requirement
|
|
111
110
|
requirements:
|
|
112
|
-
- - "
|
|
111
|
+
- - ">="
|
|
113
112
|
- !ruby/object:Gem::Version
|
|
114
|
-
version:
|
|
113
|
+
version: '0'
|
|
115
114
|
description: Extends ActiveRecord with enumeration capabilites.
|
|
116
115
|
email:
|
|
117
116
|
- tomislav@infinum.hr
|
|
@@ -160,7 +159,6 @@ homepage: https://github.com/infinum/enumerations
|
|
|
160
159
|
licenses:
|
|
161
160
|
- MIT
|
|
162
161
|
metadata: {}
|
|
163
|
-
post_install_message:
|
|
164
162
|
rdoc_options: []
|
|
165
163
|
require_paths:
|
|
166
164
|
- lib
|
|
@@ -168,15 +166,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
168
166
|
requirements:
|
|
169
167
|
- - ">="
|
|
170
168
|
- !ruby/object:Gem::Version
|
|
171
|
-
version: '0'
|
|
169
|
+
version: '3.0'
|
|
172
170
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
173
171
|
requirements:
|
|
174
172
|
- - ">="
|
|
175
173
|
- !ruby/object:Gem::Version
|
|
176
174
|
version: '0'
|
|
177
175
|
requirements: []
|
|
178
|
-
rubygems_version: 3.4
|
|
179
|
-
signing_key:
|
|
176
|
+
rubygems_version: 3.6.4
|
|
180
177
|
specification_version: 4
|
|
181
178
|
summary: Enumerations for ActiveRecord!
|
|
182
179
|
test_files:
|