enum_i18n 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +89 -0
- data/Rakefile +1 -0
- data/enum_i18n.gemspec +24 -0
- data/lib/enum_i18n.rb +4 -0
- data/lib/enum_i18n/i18n.rb +67 -0
- data/lib/enum_i18n/railtie.rb +7 -0
- data/lib/enum_i18n/version.rb +3 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5d77592b104b72e3ee96d81850ef2e7058f209f8
|
4
|
+
data.tar.gz: 063afc07f4e6254d2a5559c2ecaa5028bcf12518
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 685c5c35761bc7d0d99fac2f075370fcec050fe0ea3b51b317ff66e6b2431d03b42c653859adf23fa52d8b27c654603454ac9dcb7be270301134c14537cc7379
|
7
|
+
data.tar.gz: 29e77f13f67b7c6b00c7ee1e33557d5c5a867f6c6487751fe89c1acaef38a790fd18168b84520533d5f4f2433a0815cf6c983a6e441de62d97ca6e4b62b07e4b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 quangtt
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# EnumI18n
|
2
|
+
|
3
|
+
This gem helps ActiveRecord::Enum work smoothly wish Internationalization.
|
4
|
+
|
5
|
+
From Rails 4.1.0, ActiveRecord supported Enum method. But if you want do deal with I18n, you do it your self, many of redundant code.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'enum_i18n'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install enum_i18n
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Required Rails 4.1.x
|
26
|
+
|
27
|
+
In model file:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class Person < ActiveRecord::Base
|
31
|
+
enum character: { nice: 0, bad: 1 }
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
In locale file:
|
36
|
+
|
37
|
+
```yaml
|
38
|
+
# config/locales/models/person/ja.yml
|
39
|
+
ja:
|
40
|
+
activerecord:
|
41
|
+
models:
|
42
|
+
person: 人
|
43
|
+
attributes:
|
44
|
+
person:
|
45
|
+
name: 名前
|
46
|
+
character: キャラクター
|
47
|
+
characters:
|
48
|
+
nice: 良い
|
49
|
+
bad: 悪い
|
50
|
+
```
|
51
|
+
|
52
|
+
Using:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
person = Person.first
|
56
|
+
person.character
|
57
|
+
# > "nice"
|
58
|
+
person.character_i18n
|
59
|
+
# > "良い"
|
60
|
+
|
61
|
+
Person.characters_i18n
|
62
|
+
# > {"nice"=>"良い", "bad"=>"悪い"}
|
63
|
+
```
|
64
|
+
|
65
|
+
```erb
|
66
|
+
# Using within a form
|
67
|
+
<%= f.select :character, options_for_select(Person.characters_options_i18n) %>
|
68
|
+
```
|
69
|
+
|
70
|
+
## Development
|
71
|
+
|
72
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
73
|
+
|
74
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
75
|
+
|
76
|
+
## Contributing
|
77
|
+
|
78
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/quangtt/enum_i18n. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
79
|
+
|
80
|
+
1. Fork it ( http://github.com/quangtt/enum_i18n/fork )
|
81
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
82
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
83
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
84
|
+
5. Create new Pull Request
|
85
|
+
|
86
|
+
|
87
|
+
## License
|
88
|
+
|
89
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/enum_i18n.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'enum_i18n/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "enum_i18n"
|
8
|
+
spec.version = EnumI18n::VERSION
|
9
|
+
spec.authors = ["Quang Tran"]
|
10
|
+
spec.email = ["quangtt2812@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Make ActiveRecord::Enum run smoothly with ActiveRecord Internationalization}
|
13
|
+
spec.homepage = "https://github.com/quangtt/enum_i18n"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
24
|
+
end
|
data/lib/enum_i18n.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
module EnumI18n
|
2
|
+
module I18n
|
3
|
+
# overwrite the enum method
|
4
|
+
def enum( definitions )
|
5
|
+
super( definitions )
|
6
|
+
definitions.each do |attr_name, _|
|
7
|
+
Helper.define_attr_i18n_method(self, attr_name)
|
8
|
+
Helper.define_collection_i18n_method(self, attr_name)
|
9
|
+
Helper.define_options_i18n_method(self, attr_name)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.extended(receiver)
|
14
|
+
# receiver.class_eval do
|
15
|
+
# # alias_method_chain :enum, :enum_i18n
|
16
|
+
# end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module Helper
|
21
|
+
def self.define_attr_i18n_method(klass, attr_name)
|
22
|
+
attr_i18n_method_name = "#{attr_name}_i18n"
|
23
|
+
|
24
|
+
klass.class_eval <<-METHOD, __FILE__, __LINE__
|
25
|
+
def #{attr_i18n_method_name}
|
26
|
+
enum_symbol = self.send(:#{attr_name})
|
27
|
+
if enum_symbol
|
28
|
+
::EnumI18n::Helper.translate_enum_symbol(self.class, :#{attr_name}, enum_symbol)
|
29
|
+
else
|
30
|
+
nil
|
31
|
+
end
|
32
|
+
end
|
33
|
+
METHOD
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.define_collection_i18n_method(klass, attr_name)
|
37
|
+
collection_method_name = "#{attr_name.to_s.pluralize}"
|
38
|
+
collection_i18n_method_name = "#{collection_method_name}_i18n"
|
39
|
+
|
40
|
+
klass.instance_eval <<-METHOD, __FILE__, __LINE__
|
41
|
+
def #{collection_i18n_method_name}
|
42
|
+
collection_array = #{collection_method_name}.collect do |symbol, _|
|
43
|
+
[symbol, ::EnumI18n::Helper.translate_enum_symbol(self, :#{attr_name}, symbol)]
|
44
|
+
end
|
45
|
+
Hash[collection_array].with_indifferent_access
|
46
|
+
end
|
47
|
+
METHOD
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.define_options_i18n_method(klass, attr_name)
|
51
|
+
collection_method_name = "#{attr_name.to_s.pluralize}"
|
52
|
+
options_i18n_method_name = "#{collection_method_name}_options_i18n"
|
53
|
+
klass.instance_eval <<-METHOD, __FILE__, __LINE__
|
54
|
+
def #{options_i18n_method_name}
|
55
|
+
collection_array = #{collection_method_name}.collect do |symbol, _|
|
56
|
+
[::EnumI18n::Helper.translate_enum_symbol(self, :#{attr_name}, symbol), symbol]
|
57
|
+
end
|
58
|
+
Hash[collection_array].with_indifferent_access
|
59
|
+
end
|
60
|
+
METHOD
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.translate_enum_symbol(klass, attr_name, enum_symbol)
|
64
|
+
::I18n.t("activerecord.attributes.#{klass.to_s.underscore.gsub('/', '.')}.#{attr_name.to_s.pluralize}.#{enum_symbol}", default: enum_symbol.humanize)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enum_i18n
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Quang Tran
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- quangtt2812@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- enum_i18n.gemspec
|
68
|
+
- lib/enum_i18n.rb
|
69
|
+
- lib/enum_i18n/i18n.rb
|
70
|
+
- lib/enum_i18n/railtie.rb
|
71
|
+
- lib/enum_i18n/version.rb
|
72
|
+
homepage: https://github.com/quangtt/enum_i18n
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.5.1
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Make ActiveRecord::Enum run smoothly with ActiveRecord Internationalization
|
96
|
+
test_files: []
|