enum_help 0.0.16 → 0.0.19
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 +5 -5
- data/.github/workflows/ci.yml +44 -0
- data/.rspec +3 -0
- data/Gemfile +14 -0
- data/README.md +13 -9
- data/Rakefile +1 -0
- data/enum_help.gemspec +4 -5
- data/lib/enum_help/i18n.rb +23 -6
- data/lib/enum_help/railtie.rb +4 -2
- data/lib/enum_help/version.rb +1 -1
- data/spec/enum_i18n_help_spec.rb +99 -0
- data/spec/fixtures/locales/en.yml +16 -0
- data/spec/fixtures/locales/zh-CN.yml +16 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/support/setup_database.rb +17 -0
- metadata +24 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 31095c38410d72dc36b68e0eb2fe538a2bb09246fd2195e80d3441e05fb6a697
|
4
|
+
data.tar.gz: 2c62a01900ee464b3de4cca4ebc17de9e8e53a5b3f3e211acbf224f52cc80c49
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5626546f63adea2e0742ee1adf325f4ee9cf6d6673068d7b239d88f907a5b270e0c02e1b1207896bb5e635b9e4d892b204a6c4b18af14ec8c8bdda848fef136
|
7
|
+
data.tar.gz: ebaec0dbcb3423c2ce8c720f385df4091e713a6e06e1516985bba3bbf079f68921ee3dfd99520dde9f4e3d0e26039eb196becf7197d89eaf77016d7b0eeba894
|
@@ -0,0 +1,44 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on:
|
4
|
+
- push
|
5
|
+
- pull_request
|
6
|
+
|
7
|
+
jobs:
|
8
|
+
test:
|
9
|
+
runs-on: ubuntu-latest
|
10
|
+
strategy:
|
11
|
+
fail-fast: false
|
12
|
+
matrix:
|
13
|
+
ruby-version:
|
14
|
+
- head
|
15
|
+
- 3.1
|
16
|
+
- "3.0"
|
17
|
+
- 2.7
|
18
|
+
- 2.6
|
19
|
+
rails-version:
|
20
|
+
- "edge"
|
21
|
+
- "~> 7.0.1"
|
22
|
+
- "~> 6.1.4"
|
23
|
+
- "~> 6.0.4"
|
24
|
+
- "~> 5.2.6"
|
25
|
+
exclude:
|
26
|
+
- ruby-version: head
|
27
|
+
rails-version: "~> 5.2.6"
|
28
|
+
- ruby-version: 3.1
|
29
|
+
rails-version: "~> 5.2.6"
|
30
|
+
- ruby-version: "3.0"
|
31
|
+
rails-version: "~> 5.2.6"
|
32
|
+
- ruby-version: 2.6
|
33
|
+
rails-version: "edge"
|
34
|
+
- ruby-version: 2.6
|
35
|
+
rails-version: "~> 7.0.1"
|
36
|
+
env:
|
37
|
+
RAILS_VERSION: "${{ matrix.rails-version }}"
|
38
|
+
steps:
|
39
|
+
- uses: actions/checkout@v2
|
40
|
+
- uses: ruby/setup-ruby@v1
|
41
|
+
with:
|
42
|
+
ruby-version: "${{ matrix.ruby-version }}"
|
43
|
+
bundler-cache: true
|
44
|
+
- run: bundle exec rspec
|
data/.rspec
ADDED
data/Gemfile
CHANGED
@@ -2,3 +2,17 @@ source 'https://rubygems.org'
|
|
2
2
|
|
3
3
|
# Specify your gem's dependencies in enum_help.gemspec
|
4
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/README.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# EnumHelp
|
2
2
|
|
3
|
+
[](https://rubygems.org/gems/enum_help)
|
4
|
+
[](https://github.com/zmbacker/enum_help/actions/workflows/ci.yml)
|
5
|
+
|
3
6
|
Help ActiveRecord::Enum feature to work fine with I18n and simple_form.
|
4
7
|
|
5
8
|
Make Enum field correctly generate select field.
|
@@ -17,7 +20,7 @@ Version 0.0.15 changes the behaviour of namespaced modules as per [this commit](
|
|
17
20
|
abc:
|
18
21
|
lorem: 'Ipsum'
|
19
22
|
foo/bar/bat:
|
20
|
-
|
23
|
+
def:
|
21
24
|
lorem: 'Ipsum'
|
22
25
|
|
23
26
|
To
|
@@ -29,7 +32,7 @@ To
|
|
29
32
|
abc:
|
30
33
|
lorem: 'Ipsum'
|
31
34
|
bat:
|
32
|
-
|
35
|
+
def:
|
33
36
|
lorem: 'Ipsum'
|
34
37
|
|
35
38
|
For different I18n backends, adjust accordingly as namespaced modules are now referenced by `.` rather than `/`.
|
@@ -58,7 +61,7 @@ In model file:
|
|
58
61
|
```ruby
|
59
62
|
class Order < ActiveRecord::Base
|
60
63
|
enum status: { "nopayment" => 0, "finished" => 1, "failed" => 2, "destroyed" => 3 }
|
61
|
-
|
64
|
+
|
62
65
|
def self.restricted_statuses
|
63
66
|
statuses.except :failed, :destroyed
|
64
67
|
end
|
@@ -88,7 +91,7 @@ In `_form.html.erb` using `simple_form`:
|
|
88
91
|
<%= f.input :status %>
|
89
92
|
```
|
90
93
|
|
91
|
-
This will generate select field with translations
|
94
|
+
This will generate select field with translations automatically.
|
92
95
|
|
93
96
|
And if you want to generate select except some values, then you can pass a collection option.
|
94
97
|
|
@@ -106,7 +109,7 @@ e.g.
|
|
106
109
|
<%= f.input :status, as: :string %>
|
107
110
|
```
|
108
111
|
|
109
|
-
From version 0.0.10, enum_help can
|
112
|
+
From version 0.0.10, enum_help can automatically generate radio buttons with i18n labels.
|
110
113
|
|
111
114
|
e.g.
|
112
115
|
```erb
|
@@ -133,7 +136,7 @@ zh-cn:
|
|
133
136
|
|
134
137
|
## Notice
|
135
138
|
If you want to use enum feature, field of your table can't be named with `reference`.
|
136
|
-
When it is named with 'reference' and define enum in model file, there will be
|
139
|
+
When it is named with 'reference' and define enum in model file, there will be raised an error as below:
|
137
140
|
|
138
141
|
NoMethodError: super: no superclass method `enum' for...
|
139
142
|
|
@@ -146,6 +149,7 @@ Thanks for all the [contributors](https://github.com/zmbacker/enum_help/graphs/c
|
|
146
149
|
|
147
150
|
1. Fork it ( http://github.com/zmbacker/enum_help/fork )
|
148
151
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
149
|
-
3.
|
150
|
-
4.
|
151
|
-
5.
|
152
|
+
3. Run test `rspec`
|
153
|
+
4. Commit your changes (`git commit -am 'Add some feature'`)
|
154
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
155
|
+
6. Create new Pull Request
|
data/Rakefile
CHANGED
data/enum_help.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# coding: utf-8
|
2
|
-
lib = File.expand_path(
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
4
|
+
require "enum_help/version"
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "enum_help"
|
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["zm.backer@gmail.com"]
|
11
11
|
spec.summary = %q{ Extends of ActiveRecord::Enum, which can used in simple_form and internationalization }
|
12
12
|
spec.description = %q{ Help ActiveRecord::Enum feature to work fine with I18n and simple_form. }
|
13
|
-
spec.homepage = ""
|
13
|
+
spec.homepage = "https://github.com/zmbacker/enum_help"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
@@ -18,6 +18,5 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.
|
22
|
-
spec.add_development_dependency "rake"
|
21
|
+
spec.add_dependency "activesupport", ">= 3.0.0"
|
23
22
|
end
|
data/lib/enum_help/i18n.rb
CHANGED
@@ -2,12 +2,29 @@ module EnumHelp
|
|
2
2
|
|
3
3
|
module I18n
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
11
28
|
end
|
12
29
|
end
|
13
30
|
|
data/lib/enum_help/railtie.rb
CHANGED
data/lib/enum_help/version.rb
CHANGED
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
EnumHelp::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 EnumHelp::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
|
data/spec/spec_helper.rb
ADDED
@@ -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
CHANGED
@@ -1,43 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enum_help
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.19
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lester Zhao
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-04-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '1.5'
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '1.5'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: rake
|
14
|
+
name: activesupport
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
30
16
|
requirements:
|
31
17
|
- - ">="
|
32
18
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
34
|
-
type: :
|
19
|
+
version: 3.0.0
|
20
|
+
type: :runtime
|
35
21
|
prerelease: false
|
36
22
|
version_requirements: !ruby/object:Gem::Requirement
|
37
23
|
requirements:
|
38
24
|
- - ">="
|
39
25
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
26
|
+
version: 3.0.0
|
41
27
|
description: " Help ActiveRecord::Enum feature to work fine with I18n and simple_form.
|
42
28
|
\ "
|
43
29
|
email:
|
@@ -46,7 +32,9 @@ executables: []
|
|
46
32
|
extensions: []
|
47
33
|
extra_rdoc_files: []
|
48
34
|
files:
|
35
|
+
- ".github/workflows/ci.yml"
|
49
36
|
- ".gitignore"
|
37
|
+
- ".rspec"
|
50
38
|
- Gemfile
|
51
39
|
- LICENSE.txt
|
52
40
|
- README.md
|
@@ -57,11 +45,16 @@ files:
|
|
57
45
|
- lib/enum_help/railtie.rb
|
58
46
|
- lib/enum_help/simple_form.rb
|
59
47
|
- lib/enum_help/version.rb
|
60
|
-
|
48
|
+
- spec/enum_i18n_help_spec.rb
|
49
|
+
- spec/fixtures/locales/en.yml
|
50
|
+
- spec/fixtures/locales/zh-CN.yml
|
51
|
+
- spec/spec_helper.rb
|
52
|
+
- spec/support/setup_database.rb
|
53
|
+
homepage: https://github.com/zmbacker/enum_help
|
61
54
|
licenses:
|
62
55
|
- MIT
|
63
56
|
metadata: {}
|
64
|
-
post_install_message:
|
57
|
+
post_install_message:
|
65
58
|
rdoc_options: []
|
66
59
|
require_paths:
|
67
60
|
- lib
|
@@ -76,9 +69,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
69
|
- !ruby/object:Gem::Version
|
77
70
|
version: '0'
|
78
71
|
requirements: []
|
79
|
-
|
80
|
-
|
81
|
-
signing_key:
|
72
|
+
rubygems_version: 3.1.6
|
73
|
+
signing_key:
|
82
74
|
specification_version: 4
|
83
75
|
summary: Extends of ActiveRecord::Enum, which can used in simple_form and internationalization
|
84
|
-
test_files:
|
76
|
+
test_files:
|
77
|
+
- spec/enum_i18n_help_spec.rb
|
78
|
+
- spec/fixtures/locales/en.yml
|
79
|
+
- spec/fixtures/locales/zh-CN.yml
|
80
|
+
- spec/spec_helper.rb
|
81
|
+
- spec/support/setup_database.rb
|