enumerations 2.4.0 → 2.5.0

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: 662c2ae6e522b2f97a03e6dc0c848b8d6d45d77000f36ca5f08a3b6dc4ce6433
4
+ data.tar.gz: 8d5a3f9e31fe5f1bf514e0b7deb2c6269a6acce629f14d18078110165994cda2
5
5
  SHA512:
6
- metadata.gz: 4032983040ac1421779fc12c5433635e572073c6b2da731294de2696dc8074aa4c42677d5831b9e3e02f9ae6deecf0d2a6d91e792d6a1c360ff20cec05a9a41d
7
- data.tar.gz: 7dbd89ebab4b368b5971fb011ab386bc5ad25d6670d8f3c9225c0ccf44a5e353d8903ec997e970ef1fafd01b3c5ad7fd03e8d76a5f407e6440fd50cf47e2bba7
6
+ metadata.gz: d9b80ce8946b5614c07d15e7625f366186bfdbb463b807faa4b18458af32ea69c623214ac87b83f165d683e49047d1fdada5222ac299b8d2bb53d3c3ca8d2e3d
7
+ data.tar.gz: b05ffbd75a2fd6744c71b211aa966c72e74cba905a73cd4efdbb1dde43a3809314d0da4be8626609b9b761fa85f2b96872d8eb01826b6c27a4ce78226999f0cb
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Change Log
2
2
 
3
+ ## [v2.4.0](https://github.com/infinum/enumerations/tree/v2.4.0) (2019-02-07)
4
+ [Full Changelog](https://github.com/infinum/enumerations/compare/v2.3.3...v2.4.0)
5
+
6
+ **Implemented enhancements:**
7
+
8
+ - Add `translate_attributes` configuration option to disable translation of attributes
9
+
10
+ ## [v2.3.3](https://github.com/infinum/enumerations/tree/v2.3.1) (2019-19-09)
11
+ [Full Changelog](https://github.com/infinum/enumerations/compare/v2.3.2...v2.3.3)
12
+
13
+ ## [v2.3.2](https://github.com/infinum/enumerations/tree/v2.3.1) (2019-03-27)
14
+ [Full Changelog](https://github.com/infinum/enumerations/compare/v2.3.1...v2.3.2)
15
+
3
16
  ## [v2.3.1](https://github.com/infinum/enumerations/tree/v2.3.1) (2017-09-08)
4
17
  [Full Changelog](https://github.com/infinum/enumerations/compare/v2.3.0...v2.3.1)
5
18
 
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
 
@@ -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
 
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
 
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Enumerations
2
- VERSION = '2.4.0'.freeze
2
+ VERSION = '2.5.0'.freeze
3
3
  end
@@ -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
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.0
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
@@ -157,7 +157,7 @@ files:
157
157
  homepage: https://github.com/infinum/enumerations
158
158
  licenses: []
159
159
  metadata: {}
160
- post_install_message:
160
+ post_install_message:
161
161
  rdoc_options: []
162
162
  require_paths:
163
163
  - lib
@@ -172,9 +172,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
172
172
  - !ruby/object:Gem::Version
173
173
  version: '0'
174
174
  requirements: []
175
- rubyforge_project:
176
- rubygems_version: 2.7.6
177
- signing_key:
175
+ rubygems_version: 3.1.2
176
+ signing_key:
178
177
  specification_version: 4
179
178
  summary: Enumerations for ActiveRecord!
180
179
  test_files: