enumerations 2.4.0 → 2.5.3

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: 7a43aa264af10522517e6bfcf47daee1ea28d382d2187ca12690d19f2018781b
4
- data.tar.gz: ba01b7f7c44242da5a792839038a3967719e9a2bd6edc9ff8dc73cf66f1d090e
3
+ metadata.gz: 7415435429c08e2093220592c091dffca83b50fd26f6d71836873594da483778
4
+ data.tar.gz: 63538f83e93d43926f2401297c213a03c814efb667d96259f8f86cfa241072e3
5
5
  SHA512:
6
- metadata.gz: 4032983040ac1421779fc12c5433635e572073c6b2da731294de2696dc8074aa4c42677d5831b9e3e02f9ae6deecf0d2a6d91e792d6a1c360ff20cec05a9a41d
7
- data.tar.gz: 7dbd89ebab4b368b5971fb011ab386bc5ad25d6670d8f3c9225c0ccf44a5e353d8903ec997e970ef1fafd01b3c5ad7fd03e8d76a5f407e6440fd50cf47e2bba7
6
+ metadata.gz: dfcb47a129cd32afa78c25897aa681bef271266c878ba9fc077df12ed15543ebdab1f97e71c580adb9e19523bbf3287b3ec0a46a52b247f42030c4f9ade31b1d
7
+ data.tar.gz: 2dbce2808dcadd1cb3e0b16193b9f66e567846c8d9dca89478d30edc6c1b583e8b8b9e8443e08ec162e7e30dc78423cf7b90edf5de41de2b64722b664785a1d2
@@ -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,59 @@
1
1
  # Change Log
2
2
 
3
+ ## [v2.5.3](https://github.com/infinum/enumerations/tree/v2.5.3) (2022-02-02)
4
+ [Full Changelog](https://github.com/infinum/enumerations/compare/v2.5.2...v2.5.3)
5
+
6
+ **Fixed bugs:**
7
+
8
+ - Prevent `method_missing` instantiates new instance of `Enumerations::Base`; returns `String` instead
9
+
10
+ **Merged pull requests:**
11
+
12
+ - Prevent method_missing to instantiates new Enumerations::Base [\#58](https://github.com/infinum/enumerations/pull/58) ([PetarCurkovic](https://github.com/PetarCurkovic))
13
+
14
+ ## [v2.5.2](https://github.com/infinum/enumerations/tree/v2.5.2) (2022-01-27)
15
+ [Full Changelog](https://github.com/infinum/enumerations/compare/v2.5.1...v2.5.2)
16
+
17
+ **Implemented enhancements:**
18
+
19
+ - Adds Ruby 3.1 to test suite. Changes the runner to ubuntu-latest.
20
+
21
+ **Closed issues:**
22
+
23
+ - Ruby 3.0, `respond_to? :name` always returns true [\#55](https://github.com/infinum/enumerations/issues/55)
24
+
25
+ **Merged pull requests:**
26
+
27
+ - Initialize `ActiveSupport::Multibyte::Chars` with a String [\#56](https://github.com/infinum/enumerations/pull/56) ([lovro-bikic](https://github.com/lovro-bikic))
28
+ - Test against Ruby 3.1 [\#57](https://github.com/infinum/enumerations/pull/57) ([lovro-bikic](https://github.com/lovro-bikic))
29
+
30
+ ## [v2.5.1](https://github.com/infinum/enumerations/tree/v2.5.1) (2021-05-24)
31
+ [Full Changelog](https://github.com/infinum/enumerations/compare/v2.5.0...v2.5.1)
32
+
33
+ **Implemented enhancements:**
34
+
35
+ - Add support for Ruby 3 [\#52](https://github.com/infinum/enumerations/issues/52)
36
+
37
+ ## [v2.5.0](https://github.com/infinum/enumerations/tree/v2.5.0) (2021-03-03)
38
+ [Full Changelog](https://github.com/infinum/enumerations/compare/v2.4.0...v2.5.0)
39
+
40
+ **Implemented enhancements:**
41
+
42
+ - Add `raise_invalid_value_error` configuration option to disable raising errors on invalid values
43
+
44
+ ## [v2.4.0](https://github.com/infinum/enumerations/tree/v2.4.0) (2019-02-07)
45
+ [Full Changelog](https://github.com/infinum/enumerations/compare/v2.3.3...v2.4.0)
46
+
47
+ **Implemented enhancements:**
48
+
49
+ - Add `translate_attributes` configuration option to disable translation of attributes
50
+
51
+ ## [v2.3.3](https://github.com/infinum/enumerations/tree/v2.3.1) (2019-19-09)
52
+ [Full Changelog](https://github.com/infinum/enumerations/compare/v2.3.2...v2.3.3)
53
+
54
+ ## [v2.3.2](https://github.com/infinum/enumerations/tree/v2.3.1) (2019-03-27)
55
+ [Full Changelog](https://github.com/infinum/enumerations/compare/v2.3.1...v2.3.2)
56
+
3
57
  ## [v2.3.1](https://github.com/infinum/enumerations/tree/v2.3.1) (2017-09-08)
4
58
  [Full Changelog](https://github.com/infinum/enumerations/compare/v2.3.0...v2.3.1)
5
59
 
data/README.md CHANGED
@@ -4,7 +4,7 @@ Enumerations
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
6
  [![Test Coverage](https://api.codeclimate.com/v1/badges/c3b96c5afceaa9be2173/test_coverage)](https://codeclimate.com/github/infinum/enumerations/test_coverage)
7
- [![Build Status](https://semaphoreci.com/api/v1/infinum/enumerations/branches/master/shields_badge.svg)](https://semaphoreci.com/infinum/enumerations)
7
+ ![Build Status](https://github.com/infinum/enumerations/actions/workflows/test.yml/badge.svg)
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
 
@@ -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,9 +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
312
- config.translate_attributes = true
336
+ config.primary_key = :id
337
+ config.foreign_key_suffix = :id
338
+ config.translate_attributes = true
339
+ config.raise_invalid_value_error = true
313
340
  end
314
341
  ```
315
342
 
@@ -126,12 +126,18 @@ 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
133
133
 
134
134
  create_instance_methods
135
135
  end
136
+
137
+ private
138
+
139
+ def chars(string)
140
+ string
141
+ end
136
142
  end
137
143
  end
@@ -17,11 +17,13 @@ module Enumerations
17
17
  attr_accessor :primary_key
18
18
  attr_accessor :foreign_key_suffix
19
19
  attr_accessor :translate_attributes
20
+ attr_accessor :raise_invalid_value_error
20
21
 
21
22
  def initialize
22
- @primary_key = nil
23
- @foreign_key_suffix = nil
24
- @translate_attributes = true
23
+ @primary_key = nil
24
+ @foreign_key_suffix = nil
25
+ @translate_attributes = true
26
+ @raise_invalid_value_error = true
25
27
  end
26
28
  end
27
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]
@@ -102,7 +102,7 @@ module Enumerations
102
102
  @attributes.each do |key, _|
103
103
  method_name = "#{key}?"
104
104
 
105
- next if respond_to?(method_name.to_sym)
105
+ next if self.class.method_defined?(method_name.to_sym)
106
106
 
107
107
  self.class.send :define_method, method_name do
108
108
  @attributes[key].present?
@@ -1,3 +1,3 @@
1
1
  module Enumerations
2
- VERSION = '2.4.0'.freeze
2
+ VERSION = '2.5.3'.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
data/test/value_test.rb CHANGED
@@ -82,4 +82,30 @@ 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
103
+
104
+ def test_string_methods_on_value
105
+ enum = Class.new(Enumerations::Base) do
106
+ value :foobar
107
+ end
108
+
109
+ assert_equal enum.foobar.upcase, 'FOOBAR'
110
+ end
85
111
  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.0
4
+ version: 2.5.3
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
@@ -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: []