enum_local_i18n 0.0.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: eb253dc45b154e2fb9cc8fe06aa4e1736fb5313d1b8540daa6081c7d97ba2aaa
4
+ data.tar.gz: bf7410e099eb9fe2dfc433d80043993afb88c6db216232f8f8ee7d062b2c3eaa
5
+ SHA512:
6
+ metadata.gz: 963a5df1c517abbfc15778337ea3e4c14158afd56167c9c7d944067a467a7e865dd3b8c59a39de572586f5a8847f23ab6c13e96d3b800c5580b6de30f8e3bfb4
7
+ data.tar.gz: d1cf7391a770d5328f53f165562f068afd8e1b0ad0af21078ff868542b7ce675831c548fb6c2b27e2aebd727db54b2fd26c652e2e4d4f3c1429ca5796bb748d6
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .envrc
19
+ .env.*
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --profile 3
3
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in enum_help.gemspec
4
+ gemspec
5
+
6
+ gem 'bundler'
7
+ gem 'rake'
8
+ gem 'rspec'
9
+ gem 'sqlite3'
10
+
11
+ case version = ENV['RAILS_VERSION']
12
+ when nil
13
+ gem 'rails', '~> 7.0'
14
+ when 'edge'
15
+ gem 'rails', github: 'rails/rails'
16
+ else
17
+ gem 'rails', version
18
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 ParenJAH
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # EnumLocalI18n
2
+
3
+ Help I18n to work fine with ActiveRecord::Enum feature.
4
+
5
+ This gem can help you work fine with ActiveRecord supported Enum method and I18n localization
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'enum_local_i18n'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install enum_local_i18n
20
+
21
+ ## Usage
22
+
23
+
24
+ Required Rails 4.1.x
25
+
26
+ In model file:
27
+
28
+ ```ruby
29
+ class Order < ActiveRecord::Base
30
+ enum status: { "preparation" => 0, "active" => 1, "archive" => 2 }
31
+
32
+ def self.restricted_statuses
33
+ statuses.except :preparation
34
+ end
35
+ end
36
+ ```
37
+
38
+ You can call:
39
+
40
+ ```ruby
41
+ order = Order.first
42
+ order.update_attribute :status, 0
43
+ order.status
44
+ # > nopayment
45
+ order.status_i18n # if you have an i18n file defined as following, it will return "未支付".
46
+ # > 未支付
47
+ ```
48
+
49
+ You can also fetch the translated enum collection, if you need to:
50
+
51
+ ```ruby
52
+ Order.statuses_i18n
53
+ ```
54
+
55
+ I18n local file example:
56
+
57
+ ```yaml
58
+ # config/locales/model/order.zh-cn.yml
59
+ zh-cn:
60
+ enums:
61
+ order:
62
+ status:
63
+ finished: 完成
64
+ nopayment: 未支付
65
+ failed: 失败
66
+ destroyed: 已删除
67
+ ```
68
+
69
+
70
+ ## Notice
71
+ If you want to use enum feature, field of your table can't be named with `reference`.
72
+ When it is named with 'reference' and define enum in model file, there will be raised an error as below:
73
+
74
+ NoMethodError: super: no superclass method `enum' for...
75
+
76
+
77
+ ## Thanks
78
+
79
+ Thanks for all the [contributors](https://github.com/lyohich812/enum_local_i18n/graphs/contributors).
80
+
81
+ ## Contributing
82
+
83
+ 1. Fork it ( https://github.com/lyohich812/enum_local_i18n/fork )
84
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
85
+ 3. Run test `rspec`
86
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
87
+ 5. Push to the branch (`git push origin my-new-feature`)
88
+ 6. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "enum_local_i18n/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "enum_local_i18n"
8
+ spec.version = EnumLocalI18n::VERSION
9
+ spec.authors = ["ParenJAH"]
10
+ spec.email = ["prostotaktos@gmail.com"]
11
+ spec.summary = %q{ Internationalization of ActiveRecord::Enum }
12
+ spec.description = %q{ Help I18n to work fine with ActiveRecord::Enum feature }
13
+ spec.homepage = "https://github.com/parenJah/enum_local_i18n"
14
+ spec.license = "MIT"
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.metadata["source_code_uri"] = "https://github.com/parenJah/enum_local_i18n"
21
+ spec.add_dependency "activesupport", ">= 3.0.0"
22
+ end
@@ -0,0 +1,75 @@
1
+ module EnumLocalI18n
2
+
3
+ module I18n
4
+
5
+ if ActiveRecord::VERSION::MAJOR >= 7
6
+ # overwrite the enum method
7
+ def enum(name = nil, values = nil, **options)
8
+ super(name, values, **options)
9
+ if name # For new syntax in Rails7
10
+ Helper.define_attr_i18n_method(self, name)
11
+ Helper.define_collection_i18n_method(self, name)
12
+ else
13
+ definitions = options.slice!(:_prefix, :_suffix, :_scopes, :_default)
14
+ definitions.each do |name, _|
15
+ Helper.define_attr_i18n_method(self, name)
16
+ Helper.define_collection_i18n_method(self, name)
17
+ end
18
+ end
19
+ end
20
+ else
21
+ # overwrite the enum method
22
+ def enum( definitions )
23
+ super( definitions )
24
+ definitions.each do |name, _|
25
+ Helper.define_attr_i18n_method(self, name)
26
+ Helper.define_collection_i18n_method(self, name)
27
+ end
28
+ end
29
+ end
30
+
31
+ def self.extended(receiver)
32
+ # receiver.class_eval do
33
+ # # alias_method_chain :enum, :enum_help
34
+ # end
35
+ end
36
+
37
+ end
38
+
39
+ module Helper
40
+
41
+ def self.define_attr_i18n_method(klass, attr_name)
42
+ attr_i18n_method_name = "#{attr_name}_i18n"
43
+
44
+ klass.class_eval <<-METHOD, __FILE__, __LINE__
45
+ def #{attr_i18n_method_name}
46
+ enum_label = self.send(:#{attr_name})
47
+ if enum_label
48
+ ::EnumLocalI18n::Helper.translate_enum_label('#{klass}', :#{attr_name}, enum_label)
49
+ else
50
+ nil
51
+ end
52
+ end
53
+ METHOD
54
+ end
55
+
56
+ def self.define_collection_i18n_method(klass, attr_name)
57
+ collection_method_name = "#{attr_name.to_s.pluralize}"
58
+ collection_i18n_method_name = "#{collection_method_name}_i18n"
59
+
60
+ klass.instance_eval <<-METHOD, __FILE__, __LINE__
61
+ def #{collection_i18n_method_name}
62
+ collection_array = #{collection_method_name}.collect do |label, _|
63
+ [label, ::EnumLocalI18n::Helper.translate_enum_label('#{klass}', :#{attr_name}, label)]
64
+ end
65
+ Hash[collection_array].with_indifferent_access
66
+ end
67
+ METHOD
68
+ end
69
+
70
+ def self.translate_enum_label(klass, attr_name, enum_label)
71
+ ::I18n.t("enums.#{klass.to_s.underscore.gsub('/', '.')}.#{attr_name}.#{enum_label}", default: enum_label.humanize)
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,9 @@
1
+ module EnumLocalI18n
2
+ class Railtie < Rails::Railtie
3
+ initializer "enum_local_i18n.i18n" do
4
+ ActiveSupport.on_load(:active_record) do
5
+ extend EnumLocalI18n::I18n
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module EnumLocalI18n
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,4 @@
1
+ require "enum_local_i18n/version"
2
+ require "enum_local_i18n/i18n"
3
+ require "enum_local_i18n/railtie"
4
+
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+
3
+ EnumLocalI18n::Railtie.initializers.each(&:run)
4
+
5
+ class User < ActiveRecord::Base
6
+ if ActiveRecord.version < Gem::Version.new('6.1')
7
+ enum gender: [:male, :female]
8
+ elsif ActiveRecord.version < Gem::Version.new('7.0')
9
+ enum gender: [:male, :female], _default: :male
10
+ else # Rails 7.0 or higher
11
+ enum :gender, [:male, :female], default: :male
12
+ end
13
+
14
+ enum status: %i[normal disable]
15
+ end
16
+
17
+ RSpec.describe EnumLocalI18n::I18n do
18
+ describe '#enum' do
19
+ context 'With hash definitions' do
20
+ it 'responds defined _i18n method' do
21
+ expect(User.new).to respond_to("gender_i18n")
22
+ end
23
+
24
+ it 'responds defined genders_i18n method' do
25
+ expect(User).to respond_to("genders_i18n")
26
+ end
27
+ end
28
+
29
+ context 'With array definitions' do
30
+ it 'responds defined _i18n method' do
31
+ expect(User.new).to respond_to("status_i18n")
32
+ end
33
+
34
+ it 'responds defined status_i18n method' do
35
+ expect(User).to respond_to("statuses_i18n")
36
+ end
37
+ end
38
+ end
39
+
40
+ describe '#enum_key_i18n' do
41
+ describe 'user.gender' do
42
+ let(:gender) { "female" }
43
+ let(:user) { User.create(gender: gender) }
44
+
45
+
46
+ subject { user.gender_i18n }
47
+
48
+ context 'lang is zh-CN' do
49
+ before { I18n.locale = :"zh-CN" }
50
+
51
+ it { expect(I18n.locale).to eq :"zh-CN" }
52
+
53
+ context 'gender is male' do
54
+ let(:gender) { 'male' }
55
+ it { is_expected.to eq '男' }
56
+ end
57
+
58
+ context 'gender is female' do
59
+ let(:gender) { 'female' }
60
+ it { is_expected.to eq '女' }
61
+ end
62
+ end
63
+
64
+ context 'lang is en' do
65
+ before { I18n.locale = :en }
66
+
67
+ it { expect(I18n.locale).to eq :en }
68
+
69
+ context 'gender is male' do
70
+ let(:gender) { 'male' }
71
+ it { is_expected.to eq 'Male' }
72
+ end
73
+
74
+ context 'gender is female' do
75
+ let(:gender) { 'female' }
76
+ it { is_expected.to eq 'Female' }
77
+ end
78
+ end
79
+ end
80
+ end
81
+
82
+ describe '#enum_keys_i18n' do
83
+ subject { User.genders_i18n }
84
+
85
+ context 'lang is zh-CN' do
86
+ before { I18n.locale = :"zh-CN" }
87
+
88
+ it { expect(I18n.locale).to eq :"zh-CN" }
89
+ it { is_expected.to eq({"male"=>"男", "female"=>"女"}) }
90
+ end
91
+
92
+ context 'lang is en' do
93
+ before { I18n.locale = :en }
94
+
95
+ it { expect(I18n.locale).to eq :en }
96
+ it { is_expected.to eq({"male"=>"Male", "female"=>"Female"}) }
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,16 @@
1
+ en:
2
+ activerecord:
3
+ models:
4
+ user: "User"
5
+ attributes:
6
+ user:
7
+ id: "ID"
8
+ gender: "Gender"
9
+ enums:
10
+ user:
11
+ gender:
12
+ male: "Male"
13
+ female: "Female"
14
+ status:
15
+ normal: "Normal"
16
+ disable: "Disable"
@@ -0,0 +1,16 @@
1
+ zh-CN:
2
+ activerecord:
3
+ models:
4
+ user: "用户"
5
+ attributes:
6
+ user:
7
+ id: "ID"
8
+ gender: "性别"
9
+ enums:
10
+ user:
11
+ gender:
12
+ male: "男"
13
+ female: "女"
14
+ status:
15
+ normal: "正常"
16
+ disable: "禁用"
@@ -0,0 +1,15 @@
1
+ require "bundler/setup"
2
+
3
+ require "rspec"
4
+
5
+ require "rails"
6
+ require "active_record"
7
+
8
+ Bundler.require
9
+
10
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
11
+
12
+ I18n.load_path += Dir["#{File.dirname(__FILE__)}/fixtures/locales/*.yml"]
13
+ I18n.enforce_available_locales = false
14
+
15
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
@@ -0,0 +1,17 @@
1
+ ActiveRecord::Base.configurations = { "test"=> {"adapter"=>"sqlite3", "database"=>":memory:"} }
2
+ ActiveRecord::Base.establish_connection :test
3
+
4
+ version = "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
5
+ class CreateAllTables < ActiveRecord::Migration[version]
6
+ def change
7
+ create_table :users do |t|
8
+ t.integer :gender, null: false, default: 0, limit: 1
9
+ t.integer :status, null: false, default: 0, limit: 1
10
+
11
+ t.timestamps
12
+ end
13
+ end
14
+ end
15
+
16
+ ActiveRecord::Migration.verbose = false
17
+ CreateAllTables.new.change
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enum_local_i18n
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - ParenJAH
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-03-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ description: " Help I18n to work fine with ActiveRecord::Enum feature "
28
+ email:
29
+ - prostotaktos@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - Gemfile
37
+ - LICENSE
38
+ - README.md
39
+ - Rakefile
40
+ - enum_local_i18n.gemspec
41
+ - lib/enum_local_i18n.rb
42
+ - lib/enum_local_i18n/i18n.rb
43
+ - lib/enum_local_i18n/railtie.rb
44
+ - lib/enum_local_i18n/version.rb
45
+ - spec/enum_i18n_helper_spec.rb
46
+ - spec/fixtures/locales/en.yml
47
+ - spec/fixtures/locales/zh-CN.yml
48
+ - spec/spec_helper.rb
49
+ - spec/support/setup_database.rb
50
+ homepage: https://github.com/parenJah/enum_local_i18n
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ source_code_uri: https://github.com/parenJah/enum_local_i18n
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.0.9
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Internationalization of ActiveRecord::Enum
74
+ test_files:
75
+ - spec/enum_i18n_helper_spec.rb
76
+ - spec/fixtures/locales/en.yml
77
+ - spec/fixtures/locales/zh-CN.yml
78
+ - spec/spec_helper.rb
79
+ - spec/support/setup_database.rb