activerecord-enum_translation 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6cb93380b76689c28e8185ecf8e9f42c71ea37ab4749e2a20a04b6cc6b63d29b
4
+ data.tar.gz: 1be69a77028bb7c288cf963f0e4f0ec805cb4f80be1064aa15dc5281eded0359
5
+ SHA512:
6
+ metadata.gz: d720793c9ad6a52424f43cbecfc3955e4cac384fd95869eb0c067cd322bbe41f180429d9cfed5b1ab5999b80ff7878a7d50e8c4a30fd350ced7e509caabc3617
7
+ data.tar.gz: c53707553b5cba77392b821a9f963d63b3dbc086de4d4b56fd275d519c52d78e04e488eece58109c880715241eef4625a6c7b1510c63cb6a22f845ccf434acc1
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.5
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in activerecord-enum_translation.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
8
+ gem "sqlite3"
@@ -0,0 +1,43 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ activerecord-enum_translation (0.1.0)
5
+ activerecord
6
+ activesupport
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (6.0.3.2)
12
+ activesupport (= 6.0.3.2)
13
+ activerecord (6.0.3.2)
14
+ activemodel (= 6.0.3.2)
15
+ activesupport (= 6.0.3.2)
16
+ activesupport (6.0.3.2)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ zeitwerk (~> 2.2, >= 2.2.2)
22
+ concurrent-ruby (1.1.7)
23
+ i18n (1.8.5)
24
+ concurrent-ruby (~> 1.0)
25
+ minitest (5.14.2)
26
+ rake (12.3.3)
27
+ sqlite3 (1.4.2)
28
+ thread_safe (0.3.6)
29
+ tzinfo (1.2.7)
30
+ thread_safe (~> 0.1)
31
+ zeitwerk (2.4.0)
32
+
33
+ PLATFORMS
34
+ ruby
35
+
36
+ DEPENDENCIES
37
+ activerecord-enum_translation!
38
+ minitest (~> 5.0)
39
+ rake (~> 12.0)
40
+ sqlite3
41
+
42
+ BUNDLED WITH
43
+ 2.1.4
@@ -0,0 +1,83 @@
1
+ # ActiveRecord::EnumTranslation
2
+
3
+ Provides integration between ActiveRecord::Enum and I18n.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'activerecord-enum_translation'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install activerecord-enum_translation
20
+
21
+ ## Usage
22
+
23
+ Include `ActiveRecord::EnumTranslation`:
24
+
25
+ ```ruby
26
+ class ApplicationRecord < ActiveRecord::Base
27
+ include ActiveRecord::EnumTranslation
28
+ end
29
+ ```
30
+
31
+ Define enum:
32
+
33
+ ```ruby
34
+ class User < ApplicationRecord
35
+ enum status: %i[active inactive]
36
+ end
37
+ ```
38
+
39
+ Add translations in your locale files:
40
+
41
+ ```yaml
42
+ en:
43
+ activerecord:
44
+ attributes:
45
+ user:
46
+ status:
47
+ active: Active
48
+ inactive: Inactive
49
+ ```
50
+
51
+ Get the translation by `human_enum_name_for`:
52
+
53
+ ```ruby
54
+ user = User.new(status: :active)
55
+ user.human_enum_name_for(:status)
56
+ ```
57
+
58
+ This gem also provides `human_name_reader_for`:
59
+
60
+ ```ruby
61
+ class User < ApplicationRecord
62
+ enum status: %i[active inactive]
63
+ human_enum_name_reader_for :status
64
+ end
65
+ ```
66
+
67
+ Finally you can use `human_enum_name_for_status`:
68
+
69
+ ```ruby
70
+ user = User.new(status: :active)
71
+ user.human_enum_name_for_status
72
+ ```
73
+
74
+ ## Development
75
+
76
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
77
+
78
+ 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).
79
+
80
+ ## Contributing
81
+
82
+ Bug reports and pull requests are welcome on GitHub at https://github.com/r7kamura/activerecord-enum_translation.
83
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,28 @@
1
+ require_relative 'lib/activerecord/enum_translation/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "activerecord-enum_translation"
5
+ spec.version = ActiveRecord::EnumTranslation::VERSION
6
+ spec.authors = ["Ryo Nakamura"]
7
+ spec.email = ["r7kamura@gmail.com"]
8
+
9
+ spec.summary = "Provides integration between ActiveRecord::Enum and I18n."
10
+ spec.homepage = "https://github.com/r7kamura/activerecord-enum_translation"
11
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
12
+
13
+ spec.metadata["homepage_uri"] = spec.homepage
14
+ spec.metadata["source_code_uri"] = spec.homepage
15
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/master/CHANGELOG.md"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_runtime_dependency "activerecord"
27
+ spec.add_runtime_dependency "activesupport"
28
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "activerecord/enum_translation"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,48 @@
1
+ require "active_support/concern"
2
+ require "activerecord/enum_translation/version"
3
+
4
+ module ActiveRecord
5
+ module EnumTranslation
6
+ extend ::ActiveSupport::Concern
7
+
8
+ # Translates enum identifiers into a more human format, such as `"Active"` instead of `:active`.
9
+ # @param [Symbol] enum_name
10
+ # @param [Symbol, String, nil] default
11
+ # @return [String, nil]
12
+ # @example
13
+ # User.new(status: :active).human_enum_name_for(:status) #=> "Active"
14
+ # User.new.human_enum_name_for(:status) #=> nil
15
+ # User.new.human_enum_name_for(:status, default: "Unknown") #=> "Unknown"
16
+ # User.new.human_enum_name_for(:status, default: :"common.unknown") #=> "Unknown"
17
+ def human_enum_name_for(enum_name, default: nil)
18
+ enum_value = public_send(enum_name)
19
+ defaults = []
20
+ if enum_value
21
+ defaults += self.class.lookup_ancestors.map do |ancestor|
22
+ :"activerecord.attributes.#{ancestor.model_name.i18n_key}.#{enum_name}.#{enum_value}"
23
+ end
24
+ else
25
+ defaults << nil
26
+ end
27
+ defaults << default if default
28
+ ::I18n.t(defaults.shift, default: defaults.empty? ? nil : defaults)
29
+ end
30
+
31
+ module ClassMethods
32
+ # Defines handy reader method for enum translation.
33
+ # @param [Symbol] enum_name
34
+ # @param [Symbol, String, nil] default
35
+ # @example
36
+ # class User < ApplicationRecord
37
+ # human_enum_name_reader_for :status
38
+ # end
39
+ #
40
+ # User.new(status: :active).human_enum_name_for_status #=> "Active"
41
+ def human_enum_name_reader_for(enum_name, default: nil)
42
+ define_method("human_enum_name_for_#{enum_name}") do
43
+ human_enum_name_for(enum_name, default: default)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ module EnumTranslation
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-enum_translation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryo Nakamura
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-09-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - r7kamura@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".travis.yml"
50
+ - Gemfile
51
+ - Gemfile.lock
52
+ - README.md
53
+ - Rakefile
54
+ - activerecord-enum_translation.gemspec
55
+ - bin/console
56
+ - bin/setup
57
+ - lib/activerecord/enum_translation.rb
58
+ - lib/activerecord/enum_translation/version.rb
59
+ homepage: https://github.com/r7kamura/activerecord-enum_translation
60
+ licenses: []
61
+ metadata:
62
+ homepage_uri: https://github.com/r7kamura/activerecord-enum_translation
63
+ source_code_uri: https://github.com/r7kamura/activerecord-enum_translation
64
+ changelog_uri: https://github.com/r7kamura/activerecord-enum_translation/blob/master/CHANGELOG.md
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: 2.3.0
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubygems_version: 3.0.3
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Provides integration between ActiveRecord::Enum and I18n.
84
+ test_files: []