enumize 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +100 -0
- data/Rakefile +29 -0
- data/lib/enumize/model.rb +53 -0
- data/lib/enumize/railtie.rb +9 -0
- data/lib/enumize/version.rb +5 -0
- data/lib/enumize.rb +8 -0
- metadata +78 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a6c6268b2691c51def763ed7f8261e98c410f3820609c5c5659cb5490e17bddc
|
4
|
+
data.tar.gz: b01b5cacf299a7fad1876cce66ebdb5eb05df437eb0448bc1a5d63b82af0c9ca
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0d23b660a5f65e1e31030bf7a69cad761458e115e832a3ab979ca2d70538df3bc8e57186fda9aea269927fe157f3667591913af72a95698121ef174e1a7a5168
|
7
|
+
data.tar.gz: fe206a7ac51f8e01fdccd422820278425d5fcf0973084212c6d3b0cd4e2f518886cff038f8485ccaf2a9f8d7ec148f33a6288bdfeb700e709ffa8576775b4e45
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2019 Jason Lee
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
# Enumize
|
2
|
+
|
3
|
+
Extend ActiveRecord::Enum for add more helpful methods, from [rails/rails#36503](https://github.com/rails/rails/pull/36503).
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
```rb
|
8
|
+
class Book
|
9
|
+
enum status: %i[draft published archived]
|
10
|
+
end
|
11
|
+
```
|
12
|
+
|
13
|
+
Now we have `status_name`, `status_color`, `status_value` and `Book.status_options` methods:
|
14
|
+
|
15
|
+
- `#{attribute}_name` - return I18n name for display.
|
16
|
+
- `#{attribute}_color` - return color for enum value from I18n file.
|
17
|
+
- `#{attribute}_value` - return raw value for enum, Now we don't need use `Book.statuses[@book.status]`.
|
18
|
+
- `Book.#{attribute}_options` - return a array list for select tag options, `<%= f.select :status, Book.status_options %>`.
|
19
|
+
|
20
|
+
Write I18n:
|
21
|
+
|
22
|
+
```yml
|
23
|
+
en:
|
24
|
+
activerecord:
|
25
|
+
enums:
|
26
|
+
book:
|
27
|
+
status:
|
28
|
+
draft: Drafting
|
29
|
+
published: Published
|
30
|
+
archived: Archived
|
31
|
+
status_color:
|
32
|
+
draft: "#999999"
|
33
|
+
published: "green"
|
34
|
+
archived: "red"
|
35
|
+
|
36
|
+
zh-CN:
|
37
|
+
activerecord:
|
38
|
+
enums:
|
39
|
+
book:
|
40
|
+
status:
|
41
|
+
draft: "草稿"
|
42
|
+
published: "已发布"
|
43
|
+
archived: "归档"
|
44
|
+
```
|
45
|
+
|
46
|
+
use the methods:
|
47
|
+
|
48
|
+
```rb
|
49
|
+
irb> @book = Book.new(status: :draft)
|
50
|
+
irb> @book.status_value
|
51
|
+
0
|
52
|
+
irb> @book.status_name
|
53
|
+
"草稿"
|
54
|
+
irb> @book.status_color
|
55
|
+
"#999999"
|
56
|
+
```
|
57
|
+
|
58
|
+
For `_options` methods for `select` helper:
|
59
|
+
|
60
|
+
```erb
|
61
|
+
<% form_for(@book) do |f| %>
|
62
|
+
<%= f.text_field :name %>
|
63
|
+
<%= f.select :status, Book.status_options %>
|
64
|
+
<%= f.submit %>
|
65
|
+
<% end >
|
66
|
+
```
|
67
|
+
|
68
|
+
## Installation
|
69
|
+
|
70
|
+
Add this line to your application's Gemfile:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
gem 'enumize'
|
74
|
+
```
|
75
|
+
|
76
|
+
And then execute:
|
77
|
+
```bash
|
78
|
+
$ bundle
|
79
|
+
```
|
80
|
+
|
81
|
+
## Configure the I18n fallbacks for `color` method
|
82
|
+
|
83
|
+
You can write color config in `en.yml` once for all locales, so follow the I18n fallback, the others locale will
|
84
|
+
use color value from `en`:
|
85
|
+
|
86
|
+
config/application.rb
|
87
|
+
|
88
|
+
```rb
|
89
|
+
module Dummy
|
90
|
+
class Application < Rails::Application
|
91
|
+
# Add to tell I18n fallback translate to en
|
92
|
+
config.i18n.available_locales = ["en", "zh-CN"]
|
93
|
+
config.i18n.fallbacks = true
|
94
|
+
end
|
95
|
+
end
|
96
|
+
````
|
97
|
+
|
98
|
+
## License
|
99
|
+
|
100
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
begin
|
4
|
+
require "bundler/setup"
|
5
|
+
rescue LoadError
|
6
|
+
puts "You must `gem install bundler` and `bundle install` to run rake tasks"
|
7
|
+
end
|
8
|
+
|
9
|
+
require "rdoc/task"
|
10
|
+
|
11
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
12
|
+
rdoc.rdoc_dir = "rdoc"
|
13
|
+
rdoc.title = "Enumize"
|
14
|
+
rdoc.options << "--line-numbers"
|
15
|
+
rdoc.rdoc_files.include("README.md")
|
16
|
+
rdoc.rdoc_files.include("lib/**/*.rb")
|
17
|
+
end
|
18
|
+
|
19
|
+
require "bundler/gem_tasks"
|
20
|
+
|
21
|
+
require "rake/testtask"
|
22
|
+
|
23
|
+
Rake::TestTask.new(:test) do |t|
|
24
|
+
t.libs << "test"
|
25
|
+
t.pattern = "test/**/*_test.rb"
|
26
|
+
t.verbose = false
|
27
|
+
end
|
28
|
+
|
29
|
+
task default: :test
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Enumize
|
4
|
+
module Model
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
class_methods do
|
8
|
+
def enum(opts)
|
9
|
+
super(opts)
|
10
|
+
|
11
|
+
klass = self
|
12
|
+
singular_model_name = klass.name.singularize.underscore
|
13
|
+
locale_prefix = "activerecord.enums.#{singular_model_name}"
|
14
|
+
|
15
|
+
opts.each do |name, values|
|
16
|
+
# def status_name; I18n.t("activerecord.enums.book.status.#{status}"); end
|
17
|
+
detect_enum_conflict!(name, "#{name}_name")
|
18
|
+
define_method("#{name}_name") do
|
19
|
+
return "" if self.send(name).nil?
|
20
|
+
I18n.t("#{locale_prefix}.#{name}.#{self.send(name)}")
|
21
|
+
rescue I18n::MissingTranslationData
|
22
|
+
self[field].titleize
|
23
|
+
end
|
24
|
+
|
25
|
+
# def status_color; I18n.t("activerecord.enums.book.status_color.#{status}"); end
|
26
|
+
detect_enum_conflict!(name, "#{name}_color")
|
27
|
+
define_method("#{name}_color") do
|
28
|
+
return "black" if self.send(name).nil?
|
29
|
+
I18n.t("#{locale_prefix}.#{name}_color.#{self.send(name)}", raise: true)
|
30
|
+
rescue I18n::MissingTranslationData
|
31
|
+
return "black"
|
32
|
+
end
|
33
|
+
|
34
|
+
# def status_value; Book.statuses[self.status]; end
|
35
|
+
detect_enum_conflict!(name, "#{name}_value")
|
36
|
+
define_method("#{name}_value") do
|
37
|
+
return nil if self.send(name).nil?
|
38
|
+
klass.send(name.to_s.pluralize)[self[name]]
|
39
|
+
end
|
40
|
+
|
41
|
+
# Book.state_options # => [["Draft", "draft"], ["Published", "published"], ["Archived", "archived"]]
|
42
|
+
detect_enum_conflict!(name, "#{name}_options", true)
|
43
|
+
define_singleton_method("#{name}_options") do
|
44
|
+
self.send(name.to_s.pluralize).map do |k, _|
|
45
|
+
label = self.new(name => k).send("#{name}_name")
|
46
|
+
[label, k]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/enumize.rb
ADDED
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enumize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jason Lee
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-06-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 5.2.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mysql2
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Extend ActiveRecord::Enum for add more helpful methods
|
42
|
+
email:
|
43
|
+
- huacnlee@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- lib/enumize.rb
|
52
|
+
- lib/enumize/model.rb
|
53
|
+
- lib/enumize/railtie.rb
|
54
|
+
- lib/enumize/version.rb
|
55
|
+
homepage: https://github.com/huacnlee/enumize
|
56
|
+
licenses:
|
57
|
+
- MIT
|
58
|
+
metadata: {}
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubygems_version: 3.0.3
|
75
|
+
signing_key:
|
76
|
+
specification_version: 4
|
77
|
+
summary: Extend ActiveRecord::Enum for add more helpful methods.
|
78
|
+
test_files: []
|