humanize_enum 0.1.8 → 0.1.9
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/README.md +97 -5
- data/lib/humanize_enum/class_methods.rb +55 -0
- data/lib/humanize_enum/enum_translation.rb +29 -0
- data/lib/humanize_enum/version.rb +1 -1
- data/lib/humanize_enum.rb +6 -2
- metadata +4 -3
- data/lib/humanize_enum/helpers.rb +0 -85
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d6163c9c0cc6895cda69e255abc7c953814c863e76300d70b2916c8e2b245eb
|
4
|
+
data.tar.gz: da44277aabc9a034de3090b5f202ff49339d68b71d0213714b7c7254e2f45208
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dbc0ce238f10c8215b7555aa964d5f5611bfb8133eb521ebb98ed3c3c66641c402d25aa222877bfe85d4c94e79be272f06f19963cb4c9ce65ccfb56a0d6d8898
|
7
|
+
data.tar.gz: 6815ee75ea944d1c6df5cfde200d5c7190d852898eb2ea79e1b071b74069e88e62a37cd22db0d5e3439b9e0fcd85c400c08422def32c63d5a222c062c292dcd8
|
data/README.md
CHANGED
@@ -62,7 +62,10 @@ payment.humanize_enums(:status)
|
|
62
62
|
|
63
63
|
```
|
64
64
|
|
65
|
-
|
65
|
+
### Dehumanize Enum Values
|
66
|
+
|
67
|
+
The `dehumanize_enum` method lets you find an enum key based on its human-readable form:
|
68
|
+
|
66
69
|
```ruby
|
67
70
|
str = 'Payment error'
|
68
71
|
Payment.dehumanize_enum(:status, str) # => "error"
|
@@ -71,6 +74,83 @@ Payment.dehumanize_enum(:status, str) # => "error"
|
|
71
74
|
Payment.dehumanize_enum(:status, 'blah') # => nil
|
72
75
|
```
|
73
76
|
|
77
|
+
### Advanced Usage
|
78
|
+
|
79
|
+
#### Translating Enums and Polymorphic Associations
|
80
|
+
|
81
|
+
This gem isn't just for enums; you can also use it to translate polymorphic association types. Here's how you can do it:
|
82
|
+
|
83
|
+
Let's consider a model `Document` with an enum field `status` and a polymorphic association called `documentable`:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
class Document < ApplicationRecord
|
87
|
+
enum status: { draft: 0, published: 1, archived: 2 }
|
88
|
+
belongs_to :documentable, polymorphic: true
|
89
|
+
end
|
90
|
+
```
|
91
|
+
|
92
|
+
And we have various models that include a concern `Documentable`:
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
|
96
|
+
module Documentable
|
97
|
+
extend ActiveSupport::Concern
|
98
|
+
included do
|
99
|
+
has_many :documents, as: :documentable
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
class Article < ApplicationRecord
|
104
|
+
include Documentable
|
105
|
+
end
|
106
|
+
|
107
|
+
class Report < ApplicationRecord
|
108
|
+
include Documentable
|
109
|
+
end
|
110
|
+
```
|
111
|
+
|
112
|
+
In your locale files, you can set translations for both the enum `status` and the polymorphic association type `documentable_type`:
|
113
|
+
|
114
|
+
```yaml
|
115
|
+
en:
|
116
|
+
activerecord:
|
117
|
+
attributes:
|
118
|
+
document:
|
119
|
+
status: "Status"
|
120
|
+
status/draft: "Draft"
|
121
|
+
status/published: "Published"
|
122
|
+
status/archived: "Archived"
|
123
|
+
documentable_type: "Type"
|
124
|
+
documentable_type/article: "Article"
|
125
|
+
documentable_type/report: "Report"
|
126
|
+
```
|
127
|
+
|
128
|
+
Now you can use `humanize_enum` to get these translations:
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
document = Document.last
|
132
|
+
document.status # => :published
|
133
|
+
document.humanize_enum(:status) # => 'Published'
|
134
|
+
|
135
|
+
document.documentable_type # => 'Article'
|
136
|
+
document.humanize_enum(:documentable_type) # => 'Article'
|
137
|
+
```
|
138
|
+
|
139
|
+
And to query a translation for a specific value:
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
Document.humanize_enum(:status, :archived) # => 'Archived'
|
143
|
+
Document.humanize_enum(:documentable_type, :article) # => 'Article'
|
144
|
+
```
|
145
|
+
|
146
|
+
To get the actual enum or polymorphic type from a human-readable string:
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
Document.dehumanize_enum(:status, 'Published') # => :published
|
150
|
+
Document.dehumanize_enum(:documentable_type, 'Article') # => :article
|
151
|
+
```
|
152
|
+
|
153
|
+
|
74
154
|
## Inspired by
|
75
155
|
|
76
156
|
- a post by [Juraj Kostolanský](https://www.kostolansky.sk/posts/localize-rails-enums/) and
|
@@ -83,10 +163,22 @@ Payment.dehumanize_enum(:status, 'blah') # => nil
|
|
83
163
|
- [ar_enum_i18n](https://github.com/dalpo/ar_enum_i18n)
|
84
164
|
- [translate_enum](https://github.com/shlima/translate_enum)
|
85
165
|
|
86
|
-
##
|
87
|
-
|
88
|
-
|
89
|
-
|
166
|
+
## Why Another Gem for Enum I18n?
|
167
|
+
|
168
|
+
You might be wondering, "Why create another gem for internationalizing enums when there are already alternatives?"
|
169
|
+
|
170
|
+
Here are some reasons why `humanize_enum` might be the right choice for you:
|
171
|
+
|
172
|
+
1. **Not Just for Enums**: Yeah, it's called `humanize_enum`, but you can totally use it for polymorphic associations too. Neat, right?
|
173
|
+
|
174
|
+
2. **Fast and Light**: We've trimmed the fat to make sure this gem runs smoothly, without slowing down your app.
|
175
|
+
|
176
|
+
3. **No Rocket Science**: Simple to get, simple to use. You don't need a Ph.D. to figure it out.
|
177
|
+
|
178
|
+
4. **Clean and Familiar Structure**: We keep your locale keys neat and tidy, like `status/paid`, and it's kinda similar to what you see in other gems like AASM. So, less headache for you.
|
179
|
+
|
180
|
+
|
181
|
+
|
90
182
|
|
91
183
|
## Development
|
92
184
|
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module HumanizeEnum
|
2
|
+
module ClassMethods
|
3
|
+
# @param enum_name [String, Symbol]
|
4
|
+
# @param enum_value [String, Symbol]
|
5
|
+
# @return [String] translated value of an enum
|
6
|
+
# @raise [UnknownEnumKey] if enum_name is not a defined enum
|
7
|
+
# @example
|
8
|
+
# Payment.humanize_enum(:status, :pending) # => 'Pending'
|
9
|
+
def humanize_enum(enum_name, enum_value)
|
10
|
+
check_enum!(enum_name)
|
11
|
+
I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{enum_name}/#{enum_value.to_s.underscore}")
|
12
|
+
end
|
13
|
+
|
14
|
+
# @return [Hash<String, String>] hash where key is enum name and value is its translation
|
15
|
+
# @example
|
16
|
+
# Payment.humanize_enums(:status)
|
17
|
+
# {
|
18
|
+
# "initial" => "Initial status",
|
19
|
+
# "paid" => "Payment processed",
|
20
|
+
# "error" => "Payment error"
|
21
|
+
# }
|
22
|
+
def humanize_enums(enum_name)
|
23
|
+
enum_options(enum_name).map do |enum|
|
24
|
+
[enum.value, enum.text]
|
25
|
+
end.to_h
|
26
|
+
end
|
27
|
+
|
28
|
+
# Get enum value from a translated enum text.
|
29
|
+
# Useful for parsing incoming i18n-ed text labels that were generated by +humanize_enum+
|
30
|
+
#
|
31
|
+
# @param enum_name [String, Symbol] enum name
|
32
|
+
# @param enum_text [String] text value of the enum
|
33
|
+
# @return [Integer, String, NilClass]
|
34
|
+
def dehumanize_enum(enum_name, enum_text)
|
35
|
+
humanize_enums(enum_name).key(enum_text)&.underscore
|
36
|
+
end
|
37
|
+
|
38
|
+
# @param enum_name [String, Symbol] enum key to be translated
|
39
|
+
# @return [Array<SelectOption>] array of structs with :id, :key, :text, :checked
|
40
|
+
# @raise [UnknownEnumKey] if enum_name is not a defined enum
|
41
|
+
def enum_options(enum_name)
|
42
|
+
check_enum!(enum_name)
|
43
|
+
enum_i18n_key = enum_name.to_s.pluralize
|
44
|
+
send(enum_i18n_key).map do |key, val|
|
45
|
+
SelectOption.new(val, key, humanize_enum(enum_name, key), nil)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# @param enum_name [String, Symbol]
|
50
|
+
# @raise [UnknownEnumKey] if enum_name is not a defined enum
|
51
|
+
def check_enum!(enum_name)
|
52
|
+
raise UnknownEnumKey unless defined_enums.has_key?(enum_name.to_s)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module HumanizeEnum
|
2
|
+
module EnumTranslation
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend HumanizeEnum::ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
# @return [String] translated enum value of an instance
|
9
|
+
# @example
|
10
|
+
# payment = Payment.new
|
11
|
+
# payment.status = 'initial'
|
12
|
+
# payment.humanize_enum(:status) # => 'Initial'
|
13
|
+
def humanize_enum(enum_name)
|
14
|
+
self.class.check_enum!(enum_name)
|
15
|
+
I18n.t("activerecord.attributes.#{self.class.model_name.i18n_key}.#{enum_name}/#{send(enum_name).to_s.underscore}")
|
16
|
+
end
|
17
|
+
|
18
|
+
# @see .humanize_enums
|
19
|
+
def humanize_enums(enum_name)
|
20
|
+
self.class.humanize_enums(enum_name)
|
21
|
+
end
|
22
|
+
|
23
|
+
# @return [Array<SelectOption>]
|
24
|
+
# @see .enum_options
|
25
|
+
def enum_options(enum_name)
|
26
|
+
self.class.enum_options(enum_name)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/humanize_enum.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative 'humanize_enum/version'
|
4
|
-
require_relative 'humanize_enum/
|
4
|
+
require_relative 'humanize_enum/enum_translation'
|
5
|
+
require_relative 'humanize_enum/class_methods'
|
5
6
|
require 'active_support/lazy_load_hooks'
|
6
7
|
require 'active_support/inflector'
|
7
8
|
|
@@ -10,9 +11,12 @@ module HumanizeEnum
|
|
10
11
|
# Error for an unknown enum key
|
11
12
|
UnknownEnumKey = Class.new(StandardError)
|
12
13
|
|
14
|
+
# Struct to be used in html selects and similar places
|
15
|
+
SelectOption = Struct.new(:id, :value, :text, :checked)
|
16
|
+
|
13
17
|
if defined?(ActiveSupport.on_load)
|
14
18
|
ActiveSupport.on_load(:active_record) do
|
15
|
-
include HumanizeEnum::
|
19
|
+
include HumanizeEnum::EnumTranslation
|
16
20
|
end
|
17
21
|
end
|
18
22
|
end
|
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.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Max Buslaev
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-10-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -39,7 +39,8 @@ files:
|
|
39
39
|
- README.md
|
40
40
|
- Rakefile
|
41
41
|
- lib/humanize_enum.rb
|
42
|
-
- lib/humanize_enum/
|
42
|
+
- lib/humanize_enum/class_methods.rb
|
43
|
+
- lib/humanize_enum/enum_translation.rb
|
43
44
|
- lib/humanize_enum/version.rb
|
44
45
|
- sig/humanize_enum.rbs
|
45
46
|
homepage: https://github.com/austerlitz/humanize_enum
|
@@ -1,85 +0,0 @@
|
|
1
|
-
module HumanizeEnum
|
2
|
-
module Helpers
|
3
|
-
# Struct to be used in html selects and similar places
|
4
|
-
SelectOption = Struct.new(:id, :value, :text, :checked)
|
5
|
-
|
6
|
-
def self.included(base)
|
7
|
-
base.extend ClassMethods
|
8
|
-
end
|
9
|
-
|
10
|
-
module ClassMethods
|
11
|
-
# @param enum_name [String, Symbol]
|
12
|
-
# @param enum_value [String, Symbol]
|
13
|
-
# @return [String] translated value of an enum
|
14
|
-
# @raise [UnknownEnumKey] if enum_name is not a defined enum
|
15
|
-
# @example
|
16
|
-
# Payment.humanize_enum(:status, :pending) # => 'Pending'
|
17
|
-
def humanize_enum(enum_name, enum_value)
|
18
|
-
check_enum!(enum_name)
|
19
|
-
I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{enum_name}/#{enum_value}")
|
20
|
-
end
|
21
|
-
|
22
|
-
# @return [Hash<String, String>] hash where key is enum name and value is its translation
|
23
|
-
# @example
|
24
|
-
# Payment.humanize_enums(:status)
|
25
|
-
# {
|
26
|
-
# "initial" => "Initial status",
|
27
|
-
# "paid" => "Payment processed",
|
28
|
-
# "error" => "Payment error"
|
29
|
-
# }
|
30
|
-
def humanize_enums(enum_name)
|
31
|
-
enum_options(enum_name).map do |enum|
|
32
|
-
[enum.value, enum.text]
|
33
|
-
end.to_h
|
34
|
-
end
|
35
|
-
|
36
|
-
# Get enum value from a translated enum text.
|
37
|
-
# Useful for parsing incoming i18n-ed text labels that were generated by +humanize_enum+
|
38
|
-
#
|
39
|
-
# @param enum_name [String, Symbol] enum name
|
40
|
-
# @param enum_text [String] text value of the enum
|
41
|
-
# @return [Integer, String, NilClass]
|
42
|
-
def dehumanize_enum(enum_name, enum_text)
|
43
|
-
humanize_enums(enum_name).invert[enum_text]
|
44
|
-
end
|
45
|
-
|
46
|
-
# @param enum_name [String, Symbol] enum key to be translated
|
47
|
-
# @return [Array<SelectOption>] array of structs with :id, :key, :text, :checked
|
48
|
-
# @raise [UnknownEnumKey] if enum_name is not a defined enum
|
49
|
-
def enum_options(enum_name)
|
50
|
-
check_enum!(enum_name)
|
51
|
-
enum_i18n_key = enum_name.to_s.pluralize
|
52
|
-
send(enum_i18n_key).map do |key, val|
|
53
|
-
SelectOption.new(val, key, humanize_enum(enum_name, key), nil)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
# @param enum_name [String, Symbol]
|
58
|
-
# @raise [UnknownEnumKey] if enum_name is not a defined enum
|
59
|
-
def check_enum!(enum_name)
|
60
|
-
raise UnknownEnumKey unless defined_enums.keys.include?(enum_name.to_s)
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
# @return [String] translated enum value of an instance
|
65
|
-
# @example
|
66
|
-
# payment = Payment.new
|
67
|
-
# payment.status = 'initial'
|
68
|
-
# payment.humanize_enum(:status) # => 'Initial'
|
69
|
-
def humanize_enum(enum_name)
|
70
|
-
self.class.check_enum!(enum_name)
|
71
|
-
I18n.t("activerecord.attributes.#{self.class.model_name.i18n_key}.#{enum_name}/#{send(enum_name)}")
|
72
|
-
end
|
73
|
-
|
74
|
-
# @see .humanize_enums
|
75
|
-
def humanize_enums(enum_name)
|
76
|
-
self.class.humanize_enums(enum_name)
|
77
|
-
end
|
78
|
-
|
79
|
-
# @return [Array<SelectOption>]
|
80
|
-
# @see .enum_options
|
81
|
-
def enum_options(enum_name)
|
82
|
-
self.class.enum_options(enum_name)
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|