enum_help 0.0.9 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +57 -33
- data/lib/enum_help/i18n.rb +44 -14
- data/lib/enum_help/simple_form.rb +22 -4
- data/lib/enum_help/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9bab58fee36f103d6dac37d93f64f56ceaa870c
|
4
|
+
data.tar.gz: e6b064455eb7a58d63f568d03000d3cb458c5ec2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 090068e7311f52bb00f9b2c0c6391144d50fefdf669aa38583b609d7c3da3de422da4e00429307463a4c45539c0518f135898f6dc2df21256c75e9c1782eba9b
|
7
|
+
data.tar.gz: 4079525deb12d50abce3806e0e5e14031ba0649c47a0c91cafd8a6828ef51914a100b6232e6d73c7c0d24513502a1f3e743c454fc873ebcf1614ad63a4156b21
|
data/README.md
CHANGED
@@ -26,72 +26,96 @@ Or install it yourself as:
|
|
26
26
|
## Usage
|
27
27
|
|
28
28
|
|
29
|
-
|
29
|
+
Required Rails 4.1.x
|
30
30
|
|
31
31
|
In model file:
|
32
32
|
|
33
|
-
|
34
|
-
|
33
|
+
```ruby
|
34
|
+
class Order < ActiveRecord::Base
|
35
|
+
enum status: { "nopayment" => 0, "finished" => 1, "failed" => 2, "destroyed" => 3 }
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
37
|
+
def self.restricted_statuses
|
38
|
+
statuses.except :failed, :destroyed
|
39
|
+
end
|
40
|
+
end
|
41
|
+
```
|
40
42
|
|
41
43
|
You can call:
|
42
44
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
45
|
+
```ruby
|
46
|
+
order = Order.first
|
47
|
+
order.update_attribute :status, 0
|
48
|
+
order.status
|
49
|
+
# > nopayment
|
50
|
+
order.status_i18n # if you have an i18n file defined as following, it will return "未支付".
|
51
|
+
# > 未支付
|
52
|
+
```
|
49
53
|
|
50
|
-
|
54
|
+
You can also fetch the translated enum collection, if you need to:
|
51
55
|
|
52
|
-
|
56
|
+
```ruby
|
57
|
+
Order.statuses_i18n
|
58
|
+
```
|
59
|
+
|
60
|
+
In `_form.html.erb` using `simple_form`:
|
61
|
+
|
62
|
+
```erb
|
63
|
+
<%= f.input :status %>
|
64
|
+
```
|
53
65
|
|
54
66
|
This will generate select field with translations automaticlly.
|
55
67
|
|
56
68
|
And if you want to generate select except some values, then you can pass a collection option.
|
57
69
|
|
58
|
-
|
70
|
+
```erb
|
71
|
+
<%= f.input :status, Order.restricted_statuses %>
|
72
|
+
```
|
59
73
|
|
74
|
+
Other arguments for `simple_form` are supported perfectly.
|
75
|
+
|
76
|
+
e.g.
|
60
77
|
|
78
|
+
```erb
|
79
|
+
<%= f.input :status, prompt: 'Please select a stauts' %>
|
61
80
|
|
62
|
-
|
81
|
+
<%= f.input :status, as: :string %>
|
82
|
+
```
|
83
|
+
|
84
|
+
From version 0.0.10, enum_help can automaticlly generate radio buttons with i18n labels.
|
63
85
|
|
64
86
|
e.g.
|
87
|
+
```erb
|
88
|
+
<%= f.input :status, as: :radio_buttons %>
|
89
|
+
```
|
90
|
+
|
65
91
|
|
66
|
-
<%= f.input :status, prompt: 'Please select a stauts' %>
|
67
|
-
|
68
|
-
<%= f.input :status, as: :string %>
|
69
92
|
|
70
93
|
|
71
94
|
I18n local file example:
|
72
95
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
96
|
+
```yaml
|
97
|
+
# config/locals/model/order.zh-cn.yml
|
98
|
+
zh-cn:
|
99
|
+
enums:
|
100
|
+
order:
|
101
|
+
status:
|
102
|
+
finished: 完成
|
103
|
+
nopayment: 未支付
|
104
|
+
failed: 失败
|
105
|
+
destroyed: 已删除
|
106
|
+
```
|
82
107
|
|
83
108
|
|
84
109
|
## Notice
|
85
|
-
If you want to use enum feature,
|
86
|
-
field of your table can't be named with reference.
|
110
|
+
If you want to use enum feature, field of your table can't be named with `reference`.
|
87
111
|
When it is named with 'reference' and define enum in model file, there will be raise an error as below:
|
88
112
|
|
89
113
|
NoMethodError: super: no superclass method `enum' for...
|
90
114
|
|
91
115
|
|
92
116
|
## Thanks
|
93
|
-
|
94
|
-
|
117
|
+
|
118
|
+
Thanks for all the [contributors](https://github.com/zmbacker/enum_help/graphs/contributors).
|
95
119
|
|
96
120
|
## Contributing
|
97
121
|
|
data/lib/enum_help/i18n.rb
CHANGED
@@ -1,25 +1,16 @@
|
|
1
1
|
module EnumHelp
|
2
|
+
|
2
3
|
module I18n
|
3
4
|
|
4
5
|
# overwrite the enum method
|
5
6
|
def enum( definitions )
|
6
|
-
klass = self
|
7
7
|
super( definitions )
|
8
|
-
definitions.each do |name,
|
9
|
-
|
10
|
-
|
11
|
-
define_method(i18n_method_name) do
|
12
|
-
enum_value = self.send(name)
|
13
|
-
if enum_value
|
14
|
-
::I18n.t("enums.#{klass.to_s.underscore}.#{name}.#{enum_value}", default: enum_value)
|
15
|
-
else
|
16
|
-
nil
|
17
|
-
end
|
18
|
-
end
|
8
|
+
definitions.each do |name, _|
|
9
|
+
Helper.define_attr_i18n_method(self, name)
|
10
|
+
Helper.define_collection_i18n_method(self, name)
|
19
11
|
end
|
20
12
|
end
|
21
13
|
|
22
|
-
|
23
14
|
def self.extended(receiver)
|
24
15
|
# receiver.class_eval do
|
25
16
|
# # alias_method_chain :enum, :enum_help
|
@@ -27,5 +18,44 @@ module EnumHelp
|
|
27
18
|
end
|
28
19
|
|
29
20
|
end
|
30
|
-
end
|
31
21
|
|
22
|
+
module Helper
|
23
|
+
|
24
|
+
def self.define_attr_i18n_method(klass, attr_name)
|
25
|
+
attr_i18n_method_name = "#{attr_name}_i18n"
|
26
|
+
|
27
|
+
klass.class_eval <<-METHOD, __FILE__, __LINE__
|
28
|
+
def #{attr_i18n_method_name}
|
29
|
+
enum_label = self.send(:#{attr_name})
|
30
|
+
if enum_label
|
31
|
+
::EnumHelp::Helper.translate_enum_label(self.class, :#{attr_name}, enum_label)
|
32
|
+
else
|
33
|
+
nil
|
34
|
+
end
|
35
|
+
end
|
36
|
+
METHOD
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.define_collection_i18n_method(klass, attr_name)
|
40
|
+
collection_method_name = "#{attr_name.to_s.pluralize}"
|
41
|
+
collection_i18n_method_name = "#{collection_method_name}_i18n"
|
42
|
+
|
43
|
+
klass.instance_eval <<-METHOD, __FILE__, __LINE__
|
44
|
+
def #{collection_i18n_method_name}(*args)
|
45
|
+
options = args.extract_options!
|
46
|
+
collection = args[0] || send(:#{collection_method_name})
|
47
|
+
collection.except! options[:except] if options[:except]
|
48
|
+
|
49
|
+
collection.map do |label, value|
|
50
|
+
[::EnumHelp::Helper.translate_enum_label(self, :#{attr_name}, label), value]
|
51
|
+
end.to_h
|
52
|
+
end
|
53
|
+
METHOD
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.translate_enum_label(klass, attr_name, enum_label)
|
57
|
+
::I18n.t("enums.#{klass.to_s.underscore}.#{attr_name}.#{enum_label}", default: enum_label)
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
@@ -6,13 +6,22 @@ module EnumHelp
|
|
6
6
|
|
7
7
|
def default_input_type_with_enum(*args, &block)
|
8
8
|
att_name = (args.first || @attribute_name).to_s
|
9
|
+
options = args.last
|
10
|
+
return :enum_radio_buttons if options.is_a?(Hash) && options[:as] == :radio_buttons &&
|
11
|
+
is_enum_attributes?( att_name )
|
9
12
|
|
10
|
-
return :enum if (
|
11
|
-
|
13
|
+
return :enum if (options.is_a?(Hash) ? options[:as] : @options[:as]).nil? &&
|
14
|
+
is_enum_attributes?( att_name )
|
12
15
|
|
13
16
|
default_input_type_without_enum(*args, &block)
|
14
17
|
end
|
15
18
|
|
19
|
+
|
20
|
+
def is_enum_attributes?( attribute_name )
|
21
|
+
object.class.respond_to?(attribute_name.pluralize) && attribute_name.pluralize != "references"
|
22
|
+
end
|
23
|
+
|
24
|
+
|
16
25
|
end
|
17
26
|
|
18
27
|
module InputExtension
|
@@ -22,7 +31,7 @@ module EnumHelp
|
|
22
31
|
enum = input_options[:collection] || @builder.options[:collection]
|
23
32
|
raise "Attribute '#{attribute_name}' has no enum class" unless enum ||= object.class.send(attribute_name.to_s.pluralize)
|
24
33
|
|
25
|
-
collect =
|
34
|
+
collect = object.class.send("#{attribute_name.to_s.pluralize}_i18n", enum.to_h).to_a
|
26
35
|
# collect.unshift [args.last[:prompt],''] if args.last.is_a?(Hash) && args.last[:prompt]
|
27
36
|
|
28
37
|
if respond_to?(:input_options)
|
@@ -45,10 +54,19 @@ class EnumHelp::SimpleForm::EnumInput < ::SimpleForm::Inputs::CollectionSelectIn
|
|
45
54
|
end
|
46
55
|
end
|
47
56
|
|
57
|
+
|
58
|
+
class EnumHelp::SimpleForm::EnumRadioButtons < ::SimpleForm::Inputs::CollectionRadioButtonsInput
|
59
|
+
include EnumHelp::SimpleForm::InputExtension
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
|
48
64
|
SimpleForm::FormBuilder.class_eval do
|
49
65
|
include EnumHelp::SimpleForm::BuilderExtension
|
50
66
|
|
51
|
-
map_type :enum,
|
67
|
+
map_type :enum, :to => EnumHelp::SimpleForm::EnumInput
|
68
|
+
map_type :enum_radio_buttons, :to => EnumHelp::SimpleForm::EnumRadioButtons
|
69
|
+
alias_method :collection_enum_radio_buttons, :collection_radio_buttons
|
52
70
|
alias_method :collection_enum, :collection_select
|
53
71
|
alias_method_chain :default_input_type, :enum
|
54
72
|
end
|
data/lib/enum_help/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lester Zhao
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-09-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|