enum_help 0.0.14 → 0.0.18

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 7af8f0949dd254a92a7bb439af880de5d05ad9f8
4
- data.tar.gz: 15c71d3eb00bf6fa0a2aa400129b9ae4d2246388
2
+ SHA256:
3
+ metadata.gz: 6b1fb56c71d55485d1b9c413179c4ec402c4b3ea0b3e9ee362b213122f6fdc6c
4
+ data.tar.gz: 2e105e8b000e82fc026ebf56aecbd1de97ac944b8f20294615af88efaf95c49c
5
5
  SHA512:
6
- metadata.gz: f5e119aa5242b8a35451a4e4e7f37315be76fa413fd445d0d9dd8a8b22e82eac7199bbaefefc79619b0cfdcc66b65ef4ecf24773a33a8c135eb8585b747dd729
7
- data.tar.gz: 0a6f6529c29db06ac3cb17a2627d95f7da3827c52e2b71ba0973158746d176db3726c380df5a3a4ffeb75fb7645a558ed9e3883f7f044a7818c115f816ac38f1
6
+ metadata.gz: e4f0adcc0a7829ad4d44449eb930e76eed390ea39037f34fabcd640bff615a02ce0b63f6910264f532798381b58527eabf4392b64b77957bce28faf05f494a8a
7
+ data.tar.gz: ca449b158ab5a0c9a487e95300d0ad91b8581afda1dd28373bd29fb03ceb6a1d94977c0cb0a0ab38fbff462ce34f9807fb22467833313396e493cfdb387df5e5
@@ -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
@@ -0,0 +1,3 @@
1
+ --color
2
+ --profile 3
3
+ --format documentation
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
@@ -8,6 +8,31 @@ As you know in Rails 4.1.0 , ActiveRecord supported Enum method. But it doesn't
8
8
 
9
9
  This gem can help you work fine with Enum feather, I18n and simple_form
10
10
 
11
+ ## Breaking Changes
12
+
13
+ Version 0.0.15 changes the behaviour of namespaced modules as per [this commit](https://github.com/zmbacker/enum_help/commit/fd1c09bcc5402b97bbf4d35313ce84cdffbe47d3): standard en.yml structures for enums `Foo::Bar::Baz#abc` and `Foo::Bar::Bat#def` change from
14
+
15
+ enums:
16
+ foo/bar/baz:
17
+ abc:
18
+ lorem: 'Ipsum'
19
+ foo/bar/bat:
20
+ def:
21
+ lorem: 'Ipsum'
22
+
23
+ To
24
+
25
+ enums:
26
+ foo:
27
+ bar:
28
+ baz:
29
+ abc:
30
+ lorem: 'Ipsum'
31
+ bat:
32
+ def:
33
+ lorem: 'Ipsum'
34
+
35
+ For different I18n backends, adjust accordingly as namespaced modules are now referenced by `.` rather than `/`.
11
36
 
12
37
  ## Installation
13
38
 
@@ -33,7 +58,7 @@ In model file:
33
58
  ```ruby
34
59
  class Order < ActiveRecord::Base
35
60
  enum status: { "nopayment" => 0, "finished" => 1, "failed" => 2, "destroyed" => 3 }
36
-
61
+
37
62
  def self.restricted_statuses
38
63
  statuses.except :failed, :destroyed
39
64
  end
@@ -63,7 +88,7 @@ In `_form.html.erb` using `simple_form`:
63
88
  <%= f.input :status %>
64
89
  ```
65
90
 
66
- This will generate select field with translations automaticlly.
91
+ This will generate select field with translations automatically.
67
92
 
68
93
  And if you want to generate select except some values, then you can pass a collection option.
69
94
 
@@ -76,12 +101,12 @@ Other arguments for `simple_form` are supported perfectly.
76
101
  e.g.
77
102
 
78
103
  ```erb
79
- <%= f.input :status, prompt: 'Please select a stauts' %>
104
+ <%= f.input :status, prompt: 'Please select a status' %>
80
105
 
81
106
  <%= f.input :status, as: :string %>
82
107
  ```
83
108
 
84
- From version 0.0.10, enum_help can automaticlly generate radio buttons with i18n labels.
109
+ From version 0.0.10, enum_help can automatically generate radio buttons with i18n labels.
85
110
 
86
111
  e.g.
87
112
  ```erb
@@ -94,7 +119,7 @@ e.g.
94
119
  I18n local file example:
95
120
 
96
121
  ```yaml
97
- # config/locals/model/order.zh-cn.yml
122
+ # config/locales/model/order.zh-cn.yml
98
123
  zh-cn:
99
124
  enums:
100
125
  order:
@@ -108,7 +133,7 @@ zh-cn:
108
133
 
109
134
  ## Notice
110
135
  If you want to use enum feature, field of your table can't be named with `reference`.
111
- When it is named with 'reference' and define enum in model file, there will be raise an error as below:
136
+ When it is named with 'reference' and define enum in model file, there will be raised an error as below:
112
137
 
113
138
  NoMethodError: super: no superclass method `enum' for...
114
139
 
@@ -121,6 +146,7 @@ Thanks for all the [contributors](https://github.com/zmbacker/enum_help/graphs/c
121
146
 
122
147
  1. Fork it ( http://github.com/zmbacker/enum_help/fork )
123
148
  2. Create your feature branch (`git checkout -b my-new-feature`)
124
- 3. Commit your changes (`git commit -am 'Add some feature'`)
125
- 4. Push to the branch (`git push origin my-new-feature`)
126
- 5. Create new Pull Request
149
+ 3. Run test `rspec`
150
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
151
+ 5. Push to the branch (`git push origin my-new-feature`)
152
+ 6. Create new Pull Request
data/Rakefile CHANGED
@@ -1 +1,2 @@
1
1
  require "bundler/gem_tasks"
2
+ task :default => :spec
data/enum_help.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
2
+ lib = File.expand_path("../lib", __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'enum_help/version'
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.add_development_dependency "bundler", "~> 1.5"
22
- spec.add_development_dependency "rake"
21
+ spec.add_dependency "activesupport", ">= 3.0.0"
23
22
  end
@@ -2,12 +2,26 @@ module EnumHelp
2
2
 
3
3
  module I18n
4
4
 
5
- # overwrite the enum method
6
- def enum( definitions )
7
- super( definitions )
8
- definitions.each do |name, _|
9
- Helper.define_attr_i18n_method(self, name)
10
- Helper.define_collection_i18n_method(self, name)
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
+
10
+ definitions = options.slice!(:_prefix, :_suffix, :_scopes, :_default)
11
+ definitions.each do |name, _|
12
+ Helper.define_attr_i18n_method(self, name)
13
+ Helper.define_collection_i18n_method(self, name)
14
+ end
15
+ end
16
+ end
17
+ if ActiveRecord::VERSION::MAJOR < 7
18
+ # overwrite the enum method
19
+ def enum( definitions )
20
+ super( definitions )
21
+ definitions.each do |name, _|
22
+ Helper.define_attr_i18n_method(self, name)
23
+ Helper.define_collection_i18n_method(self, name)
24
+ end
11
25
  end
12
26
  end
13
27
 
@@ -28,7 +42,7 @@ module EnumHelp
28
42
  def #{attr_i18n_method_name}
29
43
  enum_label = self.send(:#{attr_name})
30
44
  if enum_label
31
- ::EnumHelp::Helper.translate_enum_label(self.class, :#{attr_name}, enum_label)
45
+ ::EnumHelp::Helper.translate_enum_label('#{klass}', :#{attr_name}, enum_label)
32
46
  else
33
47
  nil
34
48
  end
@@ -43,7 +57,7 @@ module EnumHelp
43
57
  klass.instance_eval <<-METHOD, __FILE__, __LINE__
44
58
  def #{collection_i18n_method_name}
45
59
  collection_array = #{collection_method_name}.collect do |label, _|
46
- [label, ::EnumHelp::Helper.translate_enum_label(self, :#{attr_name}, label)]
60
+ [label, ::EnumHelp::Helper.translate_enum_label('#{klass}', :#{attr_name}, label)]
47
61
  end
48
62
  Hash[collection_array].with_indifferent_access
49
63
  end
@@ -51,7 +65,7 @@ module EnumHelp
51
65
  end
52
66
 
53
67
  def self.translate_enum_label(klass, attr_name, enum_label)
54
- ::I18n.t("enums.#{klass.to_s.underscore}.#{attr_name}.#{enum_label}", default: enum_label)
68
+ ::I18n.t("enums.#{klass.to_s.underscore.gsub('/', '.')}.#{attr_name}.#{enum_label}", default: enum_label.humanize)
55
69
  end
56
70
 
57
71
  end
@@ -1,7 +1,9 @@
1
1
  module EnumHelp
2
2
  class Railtie < Rails::Railtie
3
3
  initializer "enum_help.i18n" do
4
- ActiveRecord::Base.send :extend, EnumHelp::I18n
4
+ ActiveSupport.on_load(:active_record) do
5
+ extend EnumHelp::I18n
6
+ end
5
7
  end
6
8
  end
7
- end
9
+ end
@@ -4,7 +4,7 @@ module EnumHelp
4
4
  module SimpleForm
5
5
  module BuilderExtension
6
6
 
7
- def default_input_type_with_enum(*args, &block)
7
+ def default_input_type(*args, &block)
8
8
  att_name = (args.first || @attribute_name).to_s
9
9
  options = args.last
10
10
  return :enum_radio_buttons if options.is_a?(Hash) && options[:as] == :radio_buttons &&
@@ -13,7 +13,7 @@ module EnumHelp
13
13
  return :enum if (options.is_a?(Hash) ? options[:as] : @options[:as]).nil? &&
14
14
  is_enum_attributes?( att_name )
15
15
 
16
- default_input_type_without_enum(*args, &block)
16
+ super
17
17
  end
18
18
 
19
19
 
@@ -71,11 +71,10 @@ end
71
71
 
72
72
 
73
73
  SimpleForm::FormBuilder.class_eval do
74
- include EnumHelp::SimpleForm::BuilderExtension
74
+ prepend EnumHelp::SimpleForm::BuilderExtension
75
75
 
76
76
  map_type :enum, :to => EnumHelp::SimpleForm::EnumInput
77
77
  map_type :enum_radio_buttons, :to => EnumHelp::SimpleForm::EnumRadioButtons
78
78
  alias_method :collection_enum_radio_buttons, :collection_radio_buttons
79
79
  alias_method :collection_enum, :collection_select
80
- alias_method_chain :default_input_type, :enum
81
80
  end
@@ -1,3 +1,3 @@
1
1
  module EnumHelp
2
- VERSION = "0.0.14"
2
+ VERSION = "0.0.18"
3
3
  end
@@ -0,0 +1,97 @@
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
+ else
9
+ enum gender: [:male, :female], _default: :male
10
+ end
11
+
12
+ enum status: %i[normal disable]
13
+ end
14
+
15
+ RSpec.describe EnumHelp::I18n do
16
+ describe '#enum' do
17
+ context 'With hash definitions' do
18
+ it 'responds defined _i18n method' do
19
+ expect(User.new).to respond_to("gender_i18n")
20
+ end
21
+
22
+ it 'responds defined genders_i18n method' do
23
+ expect(User).to respond_to("genders_i18n")
24
+ end
25
+ end
26
+
27
+ context 'With array definitions' do
28
+ it 'responds defined _i18n method' do
29
+ expect(User.new).to respond_to("status_i18n")
30
+ end
31
+
32
+ it 'responds defined status_i18n method' do
33
+ expect(User).to respond_to("statuses_i18n")
34
+ end
35
+ end
36
+ end
37
+
38
+ describe '#enum_key_i18n' do
39
+ describe 'user.gender' do
40
+ let(:gender) { "female" }
41
+ let(:user) { User.create(gender: gender) }
42
+
43
+
44
+ subject { user.gender_i18n }
45
+
46
+ context 'lang is zh-CN' do
47
+ before { I18n.locale = :"zh-CN" }
48
+
49
+ it { expect(I18n.locale).to eq :"zh-CN" }
50
+
51
+ context 'gender is male' do
52
+ let(:gender) { 'male' }
53
+ it { is_expected.to eq '男' }
54
+ end
55
+
56
+ context 'gender is female' do
57
+ let(:gender) { 'female' }
58
+ it { is_expected.to eq '女' }
59
+ end
60
+ end
61
+
62
+ context 'lang is en' do
63
+ before { I18n.locale = :en }
64
+
65
+ it { expect(I18n.locale).to eq :en }
66
+
67
+ context 'gender is male' do
68
+ let(:gender) { 'male' }
69
+ it { is_expected.to eq 'Male' }
70
+ end
71
+
72
+ context 'gender is female' do
73
+ let(:gender) { 'female' }
74
+ it { is_expected.to eq 'Female' }
75
+ end
76
+ end
77
+ end
78
+ end
79
+
80
+ describe '#enum_keys_i18n' do
81
+ subject { User.genders_i18n }
82
+
83
+ context 'lang is zh-CN' do
84
+ before { I18n.locale = :"zh-CN" }
85
+
86
+ it { expect(I18n.locale).to eq :"zh-CN" }
87
+ it { is_expected.to eq({"male"=>"男", "female"=>"女"}) }
88
+ end
89
+
90
+ context 'lang is en' do
91
+ before { I18n.locale = :en }
92
+
93
+ it { expect(I18n.locale).to eq :en }
94
+ it { is_expected.to eq({"male"=>"Male", "female"=>"Female"}) }
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,16 @@
1
+ zh-CN:
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 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.14
4
+ version: 0.0.18
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: 2015-03-07 00:00:00.000000000 Z
11
+ date: 2022-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
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.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: '0'
34
- type: :development
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: '0'
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
- homepage: ''
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
- rubyforge_project:
80
- rubygems_version: 2.2.2
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