humanize_enum 0.1.9 → 0.1.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -0
- data/lib/humanize_enum/class_methods.rb +1 -0
- data/lib/humanize_enum/version.rb +1 -1
- data/lib/humanize_enum.rb +19 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e87b96ee4668b93f22c2bc46b9875370731950ade9a93fc7f8bb39430eb6b442
|
4
|
+
data.tar.gz: 9a68e4e3fcb1bcfb1c053ea737dc4e9e8eeed78644a786850f042fe874c10d29
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 57553ef0eb58356028d0a5b9d4b7883284e80e1a06ea09cbface928763875df9ffa10141d94315a7f1419d9f4e3973df4495fc730ec210d79993061238344529
|
7
|
+
data.tar.gz: 3c3deb9f52281157c34dc207c5355b1eb90d2cf864ea865a8e145bc2bb9cf717bda7c3a7591628d083b988df7b99e4ac994bd376ed3fa26eb8392d27a2548d3e
|
data/README.md
CHANGED
@@ -151,6 +151,18 @@ Document.dehumanize_enum(:documentable_type, 'Article') # => :article
|
|
151
151
|
```
|
152
152
|
|
153
153
|
|
154
|
+
### Configuration
|
155
|
+
|
156
|
+
Before using `humanize_enum`, you should ensure it has been configured properly. Specifically, the `check_defined_enums` option should be set to `false` if you plan to use `humanize_enum` with polymorphic association types.
|
157
|
+
```ruby
|
158
|
+
# config/initializers/humanize_enum.rb
|
159
|
+
HumanizeEnum.configure do |config|
|
160
|
+
config.check_defined_enums = false
|
161
|
+
end
|
162
|
+
```
|
163
|
+
In absence of this configuration, humanize_enum would attempt to verify if the provided attribute is a defined enum, which is not what you want when working with polymorphic types. By default, `check_defined_enums` is true.
|
164
|
+
|
165
|
+
|
154
166
|
## Inspired by
|
155
167
|
|
156
168
|
- a post by [Juraj Kostolanský](https://www.kostolansky.sk/posts/localize-rails-enums/) and
|
@@ -49,6 +49,7 @@ module HumanizeEnum
|
|
49
49
|
# @param enum_name [String, Symbol]
|
50
50
|
# @raise [UnknownEnumKey] if enum_name is not a defined enum
|
51
51
|
def check_enum!(enum_name)
|
52
|
+
return unless HumanizeEnum.configuration.check_defined_enums
|
52
53
|
raise UnknownEnumKey unless defined_enums.has_key?(enum_name.to_s)
|
53
54
|
end
|
54
55
|
end
|
data/lib/humanize_enum.rb
CHANGED
@@ -7,6 +7,25 @@ require 'active_support/lazy_load_hooks'
|
|
7
7
|
require 'active_support/inflector'
|
8
8
|
|
9
9
|
module HumanizeEnum
|
10
|
+
class Configuration
|
11
|
+
attr_accessor :check_defined_enums
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
@check_defined_enums = true
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class << self
|
19
|
+
def configuration
|
20
|
+
@configuration ||= Configuration.new
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# create configure class method to yield the config class
|
25
|
+
def self.configure
|
26
|
+
self.configuration ||= Configuration.new
|
27
|
+
yield(configuration)
|
28
|
+
end
|
10
29
|
|
11
30
|
# Error for an unknown enum key
|
12
31
|
UnknownEnumKey = Class.new(StandardError)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: humanize_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Max Buslaev
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-04-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|