enumerations 2.3.3 → 2.5.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b3236e6559134898d597bcdd10d713f1e8b0221ebc86c40c51b99ee99b9a902f
4
- data.tar.gz: a16037b68fca739949c3952260e44cde3ad904de8072ea4f9fe9216870bba39b
3
+ metadata.gz: 34bf65bbe1b914b7d56e3b2ef93b0379deccc09709e37900ea4d6ed8ea97fa3d
4
+ data.tar.gz: da852c0b54ab6c0013564f2b30b0d6df7ccdb1ddd6017c551d078994874f3691
5
5
  SHA512:
6
- metadata.gz: 375ca1a05df8bf65c6ecf4086fd9bb0a4db797398079a9b414aea37851f7ed1bcf56f88b2acfeb2e509a9b4f674b9ce45b2c807377acb8ed5253d981625f5134
7
- data.tar.gz: 4e02e59780bc7321acf18233b784dfc0b3f1d45e9b8d97e7b43c2a3c99e8c9b688adb6a5a15d884b4d2d0def1b9918bd824c7936f84a55eb38de55d5779f46d4
6
+ metadata.gz: ee7e94705e02796314e0f494d6c55231173b76697d34c691e38bc511f7448216306af75fa016558247903210b965a19316fa358210dd83c00ea42e589f05d211
7
+ data.tar.gz: 485c5e53c7dd4b3b5a85c5725f321354e822907fddb64f76c14abeed130c084cd11ea77469376519e08897e594d75b3a5445b7fcc9c7f10c64160f1af344b8df
@@ -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-latest
13
+ strategy:
14
+ matrix:
15
+ ruby: [2.6, 2.7, '3.0', 3.1]
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 CHANGED
@@ -1,5 +1,48 @@
1
1
  # Change Log
2
2
 
3
+ ## [v2.5.2](https://github.com/infinum/enumerations/tree/v2.5.2) (2022-01-27)
4
+ [Full Changelog](https://github.com/infinum/enumerations/compare/v2.5.1...v2.5.2)
5
+
6
+ **Implemented enhancements:**
7
+
8
+ - Adds Ruby 3.1 to test suite. Changes the runner to ubuntu-latest.
9
+
10
+ **Closed issues:**
11
+
12
+ - Ruby 3.0, `respond_to? :name` always returns true [\#55](https://github.com/infinum/enumerations/issues/55)
13
+
14
+ **Merged pull requests:**
15
+
16
+ - Initialize `ActiveSupport::Multibyte::Chars` with a String [\#56](https://github.com/infinum/enumerations/pull/56) ([lovro-bikic](https://github.com/lovro-bikic))
17
+ - Test against Ruby 3.1 [\#57](https://github.com/infinum/enumerations/pull/57) ([lovro-bikic](https://github.com/lovro-bikic))
18
+
19
+ ## [v2.5.1](https://github.com/infinum/enumerations/tree/v2.5.1) (2021-05-24)
20
+ [Full Changelog](https://github.com/infinum/enumerations/compare/v2.5.0...v2.5.1)
21
+
22
+ **Implemented enhancements:**
23
+
24
+ - Add support for Ruby 3 [\#52](https://github.com/infinum/enumerations/issues/52)
25
+
26
+ ## [v2.5.0](https://github.com/infinum/enumerations/tree/v2.5.0) (2021-03-03)
27
+ [Full Changelog](https://github.com/infinum/enumerations/compare/v2.4.0...v2.5.0)
28
+
29
+ **Implemented enhancements:**
30
+
31
+ - Add `raise_invalid_value_error` configuration option to disable raising errors on invalid values
32
+
33
+ ## [v2.4.0](https://github.com/infinum/enumerations/tree/v2.4.0) (2019-02-07)
34
+ [Full Changelog](https://github.com/infinum/enumerations/compare/v2.3.3...v2.4.0)
35
+
36
+ **Implemented enhancements:**
37
+
38
+ - Add `translate_attributes` configuration option to disable translation of attributes
39
+
40
+ ## [v2.3.3](https://github.com/infinum/enumerations/tree/v2.3.1) (2019-19-09)
41
+ [Full Changelog](https://github.com/infinum/enumerations/compare/v2.3.2...v2.3.3)
42
+
43
+ ## [v2.3.2](https://github.com/infinum/enumerations/tree/v2.3.1) (2019-03-27)
44
+ [Full Changelog](https://github.com/infinum/enumerations/compare/v2.3.1...v2.3.2)
45
+
3
46
  ## [v2.3.1](https://github.com/infinum/enumerations/tree/v2.3.1) (2017-09-08)
4
47
  [Full Changelog](https://github.com/infinum/enumerations/compare/v2.3.0...v2.3.1)
5
48
 
data/README.md CHANGED
@@ -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 two configuration options.
301
- You can customize primary key and foreign key suffix.
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 = :id
311
- config.foreign_key_suffix = :id
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
 
data/enumerations.gemspec CHANGED
@@ -23,7 +23,7 @@ Gem::Specification.new do |s|
23
23
  s.add_development_dependency 'pry-byebug'
24
24
  s.add_development_dependency 'rake'
25
25
  s.add_development_dependency 'simplecov'
26
- s.add_development_dependency 'sqlite3', '~> 1.3.6'
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")
@@ -126,7 +126,7 @@ module Enumerations
126
126
  attr_reader :symbol, :attributes
127
127
 
128
128
  def initialize(symbol, attributes)
129
- super(symbol)
129
+ super(symbol.to_s)
130
130
 
131
131
  @symbol = symbol
132
132
  @attributes = attributes
@@ -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 = nil
20
- @foreign_key_suffix = nil
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)
@@ -54,7 +54,7 @@ module Enumerations
54
54
  #
55
55
  def define_attributes_getters
56
56
  @attributes.each do |key, _|
57
- next if respond_to?(key)
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 respond_to?(method_name.to_sym)
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?
@@ -1,3 +1,3 @@
1
1
  module Enumerations
2
- VERSION = '2.3.3'.freeze
2
+ VERSION = '2.5.2'.freeze
3
3
  end
data/lib/enumerations.rb CHANGED
@@ -82,7 +82,7 @@ module Enumerations
82
82
  #
83
83
  def define_getter_method(reflection)
84
84
  define_method(reflection.name) do
85
- reflection.enumerator_class.find(self[reflection.foreign_key])
85
+ reflection.enumerator_class.find(self[reflection.foreign_key]) || self[reflection.foreign_key]
86
86
  end
87
87
  end
88
88
 
@@ -96,11 +96,15 @@ module Enumerations
96
96
  define_method("#{reflection.name}=") do |other|
97
97
  enumeration_value = reflection.enumerator_class.find(other)
98
98
 
99
- raise Enumerations::InvalidValueError if other.present? && enumeration_value.nil?
99
+ if other.present? && enumeration_value.nil?
100
+ raise Enumerations::InvalidValueError if Enumerations.configuration.raise_invalid_value_error
100
101
 
101
- self[reflection.foreign_key] =
102
- enumeration_value &&
103
- enumeration_value.send(reflection.enumerator_class.primary_key || :symbol)
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
104
108
  end
105
109
  end
106
110
 
@@ -26,6 +26,12 @@ class EnumerationsTest < Minitest::Test
26
26
  assert_equal 'draft', p.status.to_s
27
27
  end
28
28
 
29
+ def test_model_uniqueness_validation
30
+ p = Post.new(status: :draft)
31
+
32
+ assert_equal true, p.valid?
33
+ end
34
+
29
35
  def test_model_bang_assignment_with_custom_name
30
36
  p = Post.new
31
37
  p.different_status_draft!
@@ -133,6 +139,20 @@ class EnumerationsTest < Minitest::Test
133
139
  end
134
140
  end
135
141
 
142
+ def test_nonexistent_value_assignment_with_error_raising_turned_off
143
+ Enumerations.configuration.raise_invalid_value_error = false
144
+
145
+ user = User.new(role: :nonexistent_value)
146
+
147
+ assert_equal user.role, 'nonexistent_value'
148
+
149
+ user.role = :other_nonexistent_value
150
+
151
+ assert_equal user.role, 'other_nonexistent_value'
152
+
153
+ Enumerations.configuration.raise_invalid_value_error = true
154
+ end
155
+
136
156
  def test_on_nil_value_assignment
137
157
  user = User.new(role: nil)
138
158
  assert_nil user.role
@@ -32,6 +32,8 @@ end
32
32
  class Post < ActiveRecord::Base
33
33
  enumeration :status
34
34
  enumeration :different_status, foreign_key: :some_other_status, class_name: 'Status'
35
+
36
+ validates :status, uniqueness: true
35
37
  end
36
38
 
37
39
  class User < ActiveRecord::Base
@@ -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
data/test/value_test.rb CHANGED
@@ -82,4 +82,22 @@ class ValueTest < Minitest::Test
82
82
 
83
83
  assert_equal status.name?, false
84
84
  end
85
+
86
+ def test_enumeration_raises_error_for_nonexistent_attribute
87
+ enum = Class.new(Enumerations::Base) do
88
+ value :foobar
89
+ end
90
+
91
+ assert_raises NoMethodError do
92
+ enum.foobar.name
93
+ end
94
+ end
95
+
96
+ def test_enumeration_does_not_respond_to_nonexistent_attribute
97
+ enum = Class.new(Enumerations::Base) do
98
+ value :foobar
99
+ end
100
+
101
+ assert_equal enum.foobar.respond_to?(:name), false
102
+ end
85
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.3.3
4
+ version: 2.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomislav Car
@@ -9,7 +9,7 @@ authors:
9
9
  - Nikola Santić
10
10
  - Stjepan Hadjić
11
11
  - Petar Ćurković
12
- autorequire:
12
+ autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
15
  date: 2017-09-08 00:00:00.000000000 Z
@@ -104,14 +104,14 @@ dependencies:
104
104
  requirements:
105
105
  - - "~>"
106
106
  - !ruby/object:Gem::Version
107
- version: 1.3.6
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: 1.3.6
114
+ version: 1.4.0
115
115
  description: Extends ActiveRecord with enumeration capabilites.
116
116
  email:
117
117
  - tomislav@infinum.hr
@@ -123,6 +123,7 @@ executables: []
123
123
  extensions: []
124
124
  extra_rdoc_files: []
125
125
  files:
126
+ - ".github/workflows/test.yml"
126
127
  - ".gitignore"
127
128
  - ".rubocop.yml"
128
129
  - CHANGELOG.md
@@ -157,7 +158,7 @@ files:
157
158
  homepage: https://github.com/infinum/enumerations
158
159
  licenses: []
159
160
  metadata: {}
160
- post_install_message:
161
+ post_install_message:
161
162
  rdoc_options: []
162
163
  require_paths:
163
164
  - lib
@@ -172,24 +173,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
172
173
  - !ruby/object:Gem::Version
173
174
  version: '0'
174
175
  requirements: []
175
- rubyforge_project:
176
- rubygems_version: 2.7.6
177
- signing_key:
176
+ rubygems_version: 3.3.3
177
+ signing_key:
178
178
  specification_version: 4
179
179
  summary: Enumerations for ActiveRecord!
180
- test_files:
181
- - test/base_test.rb
182
- - test/configuration/configuration.rb
183
- - test/configuration/custom_configuration_test.rb
184
- - test/configuration/enumerations_test.rb
185
- - test/configuration/finder_test.rb
186
- - test/configuration_test.rb
187
- - test/enumerations_test.rb
188
- - test/finder_test.rb
189
- - test/helpers/database_helper.rb
190
- - test/helpers/locale_helper.rb
191
- - test/helpers/test_helper.rb
192
- - test/locales/hr.yml
193
- - test/reflection_test.rb
194
- - test/translation_test.rb
195
- - test/value_test.rb
180
+ test_files: []