enum_i18n_helper 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f9f3e01ef207fb700cb7430b8fd162d61a8560b22a3f3e16e2ddb24cc1da4246
4
+ data.tar.gz: 58ff6024baf161bbb523580146e75ff3a531136e115b005deb9ab8e8e9d10001
5
+ SHA512:
6
+ metadata.gz: f39213767680cf38f54c5e6bd7e05e6f38936e4cc7771e7e3a74f927ddce31f24820bd11edbb07275849c477b2995ccc20bb4ace9b3d863d99a132a085862ba0
7
+ data.tar.gz: a49e2631ee3a3dddccbd2ab4aea170b703da4f304cd4ed7fdb37a1457ddcd67194639f5afb7406a27b90f894aeb51162aa19cf4cfcec5b6be0c9300e69ff7895
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.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 backer
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 ADDED
@@ -0,0 +1,90 @@
1
+ # EnumI18nHelp
2
+
3
+ Help ActiveRecord::Enum feature to work fine with I18n.
4
+
5
+ As you know in Rails 4.1.0 , ActiveRecord supported Enum method. But it doesn't work fine with I18n
6
+
7
+ This gem can help you work fine with Enum and I18n
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'enum_i18n_help'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install enum_i18n_help
22
+
23
+ ## Usage
24
+
25
+
26
+ Required Rails 4.1.x
27
+
28
+ In model file:
29
+
30
+ ```ruby
31
+ class Order < ActiveRecord::Base
32
+ enum status: { "nopayment" => 0, "finished" => 1, "failed" => 2, "destroyed" => 3 }
33
+
34
+ def self.restricted_statuses
35
+ statuses.except :failed, :destroyed
36
+ end
37
+ end
38
+ ```
39
+
40
+ You can call:
41
+
42
+ ```ruby
43
+ order = Order.first
44
+ order.update_attribute :status, 0
45
+ order.status
46
+ # > nopayment
47
+ order.status_i18n # if you have an i18n file defined as following, it will return "未支付".
48
+ # > 未支付
49
+ ```
50
+
51
+ You can also fetch the translated enum collection, if you need to:
52
+
53
+ ```ruby
54
+ Order.statuses_i18n
55
+ ```
56
+
57
+ I18n local file example:
58
+
59
+ ```yaml
60
+ # config/locales/model/order.zh-cn.yml
61
+ zh-cn:
62
+ enums:
63
+ order:
64
+ status:
65
+ finished: 完成
66
+ nopayment: 未支付
67
+ failed: 失败
68
+ destroyed: 已删除
69
+ ```
70
+
71
+
72
+ ## Notice
73
+ If you want to use enum feature, field of your table can't be named with `reference`.
74
+ When it is named with 'reference' and define enum in model file, there will be raised an error as below:
75
+
76
+ NoMethodError: super: no superclass method `enum' for...
77
+
78
+
79
+ ## Thanks
80
+
81
+ Thanks for all the [contributors](https://github.com/lyohich812/enum_i18n_help/graphs/contributors).
82
+
83
+ ## Contributing
84
+
85
+ 1. Fork it ( https://github.com/lyohich812/enum_i18n_help/fork )
86
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
87
+ 3. Run test `rspec`
88
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
89
+ 5. Push to the branch (`git push origin my-new-feature`)
90
+ 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_i18n_helper/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "enum_i18n_helper"
8
+ spec.version = EnumI18nHelper::VERSION
9
+ spec.authors = ["Alex P"]
10
+ spec.email = ["youcanwinhtc@gmail.com"]
11
+ spec.summary = %q{ Extends of ActiveRecord::Enum for use internationalization }
12
+ spec.description = %q{ Help ActiveRecord::Enum feature to work fine with I18n. }
13
+ spec.homepage = "https://github.com/lyohich812/enum_i18n_helper"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport", ">= 3.0.0"
22
+ end
@@ -0,0 +1,75 @@
1
+ module EnumI18nHelper
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
+ ::EnumI18nHelper::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, ::EnumI18nHelper::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 EnumI18nHelper
2
+ class Railtie < Rails::Railtie
3
+ initializer "enum_i18n_helper.i18n" do
4
+ ActiveSupport.on_load(:active_record) do
5
+ extend EnumI18nHelper::I18n
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module EnumI18nHelper
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ require "enum_i18n_helper/version"
2
+ require "enum_i18n_helper/i18n"
3
+ require "enum_i18n_helper/railtie"
4
+
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+
3
+ EnumI18nHelper::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 EnumI18nHelper::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,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enum_i18n_helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alex P
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-02-24 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 ActiveRecord::Enum feature to work fine with I18n. "
28
+ email:
29
+ - youcanwinhtc@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - Gemfile
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - enum_i18n_helper.gemspec
41
+ - lib/enum_i18n_helper.rb
42
+ - lib/enum_i18n_helper/i18n.rb
43
+ - lib/enum_i18n_helper/railtie.rb
44
+ - lib/enum_i18n_helper/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/lyohich812/enum_i18n_helper
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.0.9
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Extends of ActiveRecord::Enum for use internationalization
73
+ test_files:
74
+ - spec/enum_i18n_helper_spec.rb
75
+ - spec/fixtures/locales/en.yml
76
+ - spec/fixtures/locales/zh-CN.yml
77
+ - spec/spec_helper.rb
78
+ - spec/support/setup_database.rb