activerecord-enum_translation 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +25 -0
- data/Gemfile.lock +1 -1
- data/LICENSE.txt +22 -0
- data/README.md +8 -6
- data/activerecord-enum_translation.gemspec +1 -0
- data/lib/activerecord/enum_translation.rb +16 -7
- data/lib/activerecord/enum_translation/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dbf4f2ef6f45d81466d61cedef509795f25fae8bc0ecce8e5bd852f0c99f814c
|
4
|
+
data.tar.gz: 0bbfda5aa2830d39d20d64db65cc408e56570cea305048e2a552c8b369b3edc3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a9c078857b0ba9995b74076847247b38473d878058dedc6c880f94516c4a28c75046e9f64843e314db59ef093ad715f2f8c4d039565cfefa227cef7cc2a63113
|
7
|
+
data.tar.gz: 5e1087ed582fb2bf1c6d3ec3159552ac14e80760b6d544899756137ac54525a7ad9ead67ac14f125f50124f8cb9652e406ac0e5dc2cb2eefc367f69dad19099d
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## v0.2.0
|
9
|
+
|
10
|
+
### Added
|
11
|
+
|
12
|
+
- Add `:count` option.
|
13
|
+
- Add `"attributes.#{enum_name}.#{enum_value}" key.
|
14
|
+
- Add humanized enum value as default value.
|
15
|
+
- Support `i18n_scope` override.
|
16
|
+
|
17
|
+
### Fixed
|
18
|
+
|
19
|
+
- Fix broken `:default` option on `#human_enum_name_reader_for`.
|
20
|
+
|
21
|
+
## v0.1.0 - 2020-09-12
|
22
|
+
|
23
|
+
### Added
|
24
|
+
|
25
|
+
- Add 1st implementation.
|
data/Gemfile.lock
CHANGED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2020 Ryo Nakamura
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# ActiveRecord::EnumTranslation
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/activerecord-enum_translation.svg)](https://rubygems.org/gems/activerecord-enum_translation)
|
4
|
+
|
3
5
|
Provides integration between ActiveRecord::Enum and I18n.
|
4
6
|
|
5
7
|
## Installation
|
@@ -39,23 +41,23 @@ end
|
|
39
41
|
Add translations in your locale files:
|
40
42
|
|
41
43
|
```yaml
|
42
|
-
|
44
|
+
ja:
|
43
45
|
activerecord:
|
44
46
|
attributes:
|
45
47
|
user:
|
46
48
|
status:
|
47
|
-
active:
|
48
|
-
inactive:
|
49
|
+
active: 利用中
|
50
|
+
inactive: 停止中
|
49
51
|
```
|
50
52
|
|
51
53
|
Get the translation by `human_enum_name_for`:
|
52
54
|
|
53
55
|
```ruby
|
54
56
|
user = User.new(status: :active)
|
55
|
-
user.human_enum_name_for(:status)
|
57
|
+
user.human_enum_name_for(:status) #=> "利用中"
|
56
58
|
```
|
57
59
|
|
58
|
-
This gem also provides `
|
60
|
+
This gem also provides `human_enum_name_reader_for`:
|
59
61
|
|
60
62
|
```ruby
|
61
63
|
class User < ApplicationRecord
|
@@ -68,7 +70,7 @@ Finally you can use `human_enum_name_for_status`:
|
|
68
70
|
|
69
71
|
```ruby
|
70
72
|
user = User.new(status: :active)
|
71
|
-
user.human_enum_name_for_status
|
73
|
+
user.human_enum_name_for_status #=> "利用中"
|
72
74
|
```
|
73
75
|
|
74
76
|
## Development
|
@@ -3,6 +3,7 @@ require_relative 'lib/activerecord/enum_translation/version'
|
|
3
3
|
Gem::Specification.new do |spec|
|
4
4
|
spec.name = "activerecord-enum_translation"
|
5
5
|
spec.version = ActiveRecord::EnumTranslation::VERSION
|
6
|
+
spec.licenses = ["MIT"]
|
6
7
|
spec.authors = ["Ryo Nakamura"]
|
7
8
|
spec.email = ["r7kamura@gmail.com"]
|
8
9
|
|
@@ -7,6 +7,7 @@ module ActiveRecord
|
|
7
7
|
|
8
8
|
# Translates enum identifiers into a more human format, such as `"Active"` instead of `:active`.
|
9
9
|
# @param [Symbol] enum_name
|
10
|
+
# @param [Integer] count
|
10
11
|
# @param [Symbol, String, nil] default
|
11
12
|
# @return [String, nil]
|
12
13
|
# @example
|
@@ -14,33 +15,41 @@ module ActiveRecord
|
|
14
15
|
# User.new.human_enum_name_for(:status) #=> nil
|
15
16
|
# User.new.human_enum_name_for(:status, default: "Unknown") #=> "Unknown"
|
16
17
|
# User.new.human_enum_name_for(:status, default: :"common.unknown") #=> "Unknown"
|
17
|
-
def human_enum_name_for(enum_name, default: nil)
|
18
|
+
def human_enum_name_for(enum_name, count: 1, default: nil)
|
19
|
+
options = { count: count }
|
18
20
|
enum_value = public_send(enum_name)
|
21
|
+
scope = "#{self.class.i18n_scope}.attributes"
|
22
|
+
|
19
23
|
defaults = []
|
20
24
|
if enum_value
|
21
25
|
defaults += self.class.lookup_ancestors.map do |ancestor|
|
22
|
-
:"
|
26
|
+
:"#{scope}.#{ancestor.model_name.i18n_key}.#{enum_name}.#{enum_value}"
|
23
27
|
end
|
28
|
+
defaults << :"attributes.#{enum_name}.#{enum_value}"
|
24
29
|
else
|
25
30
|
defaults << nil
|
26
31
|
end
|
27
32
|
defaults << default if default
|
28
|
-
|
33
|
+
defaults << enum_value.humanize if enum_value
|
34
|
+
|
35
|
+
key = defaults.shift
|
36
|
+
options[:default] = defaults.empty? ? nil : defaults
|
37
|
+
::I18n.t(key, options)
|
29
38
|
end
|
30
39
|
|
31
40
|
module ClassMethods
|
32
41
|
# Defines handy reader method for enum translation.
|
33
42
|
# @param [Symbol] enum_name
|
34
|
-
# @param [
|
43
|
+
# @param [Hash] options
|
35
44
|
# @example
|
36
45
|
# class User < ApplicationRecord
|
37
46
|
# human_enum_name_reader_for :status
|
38
47
|
# end
|
39
48
|
#
|
40
49
|
# User.new(status: :active).human_enum_name_for_status #=> "Active"
|
41
|
-
def human_enum_name_reader_for(enum_name
|
42
|
-
define_method("human_enum_name_for_#{enum_name}") do
|
43
|
-
human_enum_name_for(enum_name,
|
50
|
+
def human_enum_name_reader_for(enum_name)
|
51
|
+
define_method("human_enum_name_for_#{enum_name}") do |**options|
|
52
|
+
human_enum_name_for(enum_name, **options)
|
44
53
|
end
|
45
54
|
end
|
46
55
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-enum_translation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryo Nakamura
|
@@ -47,8 +47,10 @@ extra_rdoc_files: []
|
|
47
47
|
files:
|
48
48
|
- ".gitignore"
|
49
49
|
- ".travis.yml"
|
50
|
+
- CHANGELOG.md
|
50
51
|
- Gemfile
|
51
52
|
- Gemfile.lock
|
53
|
+
- LICENSE.txt
|
52
54
|
- README.md
|
53
55
|
- Rakefile
|
54
56
|
- activerecord-enum_translation.gemspec
|
@@ -57,7 +59,8 @@ files:
|
|
57
59
|
- lib/activerecord/enum_translation.rb
|
58
60
|
- lib/activerecord/enum_translation/version.rb
|
59
61
|
homepage: https://github.com/r7kamura/activerecord-enum_translation
|
60
|
-
licenses:
|
62
|
+
licenses:
|
63
|
+
- MIT
|
61
64
|
metadata:
|
62
65
|
homepage_uri: https://github.com/r7kamura/activerecord-enum_translation
|
63
66
|
source_code_uri: https://github.com/r7kamura/activerecord-enum_translation
|