enum_help 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +80 -0
- data/Rakefile +1 -0
- data/enum_help.gemspec +23 -0
- data/lib/enum_help/i18n.rb +27 -0
- data/lib/enum_help/railtie.rb +7 -0
- data/lib/enum_help/simple_form.rb +49 -0
- data/lib/enum_help/version.rb +3 -0
- data/lib/enum_help.rb +5 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6156968aa38d6bac599e284db6dbac69d16ae9a6
|
4
|
+
data.tar.gz: 49ba73d267e27a34ca82b2c9aab65b5da0306fa4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a17ede36b487c13c0fd18b1594908984e9585f978ab2d7f00cbaa231e0cba8f835f301b32b50c2b859e30cd5f404f5a9267e222086816a7d6b8ba60a5f72e386
|
7
|
+
data.tar.gz: 162e3c1db97fb62499f23f6cf70d1a240cceba0d3a7f406b8fc864835230e3cf91cf963a15c2c5399f3e9edbfdd573015fbc2eecad70c0fe7bab9a39d469812e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 backer
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# EnumHelp
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
Extends of ActiveRecord::Enum, which can used in simple_form and internationalization.
|
6
|
+
|
7
|
+
Make Enum field correctly generate select field.
|
8
|
+
|
9
|
+
As you know in Rails 4.1.0 , ActiveRecord supported Enum method. But it doesn't work fine with I18n and simple_form.
|
10
|
+
|
11
|
+
This gem can help you work fine with Enum feather, I18n and simple_form
|
12
|
+
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
gem 'enum_help'
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install enum_help
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
|
31
|
+
In model file:
|
32
|
+
|
33
|
+
class Order < ActiveRecord::Base
|
34
|
+
enum status: { "nopayment" => 0, "finished" => 1 }
|
35
|
+
end
|
36
|
+
|
37
|
+
You can call:
|
38
|
+
|
39
|
+
order = Order.first
|
40
|
+
order.update_attribut :status, 0
|
41
|
+
order.status
|
42
|
+
# > nopayment
|
43
|
+
order.status_i18n
|
44
|
+
# > nopayment or 未支付
|
45
|
+
|
46
|
+
In _form.html.erb using simple form:
|
47
|
+
|
48
|
+
<%= f.input :status %>
|
49
|
+
|
50
|
+
will generate select field with translations.
|
51
|
+
|
52
|
+
other arguments for simple_form are supported perfectly.
|
53
|
+
|
54
|
+
e.g.
|
55
|
+
|
56
|
+
<%= f.input :status, prompt: 'Please select a stauts' %>
|
57
|
+
|
58
|
+
<%= f.input :status, as: :string %>
|
59
|
+
|
60
|
+
|
61
|
+
I18n local file example:
|
62
|
+
|
63
|
+
# config/locals/model/order.zh-cn.yml
|
64
|
+
zh-cn:
|
65
|
+
enums:
|
66
|
+
order:
|
67
|
+
status:
|
68
|
+
finished: 完成
|
69
|
+
nopayment: 未支付
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
## Contributing
|
75
|
+
|
76
|
+
1. Fork it ( http://github.com/zmbacker/enum_help/fork )
|
77
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
78
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
79
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
80
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/enum_help.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'enum_help/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "enum_help"
|
8
|
+
spec.version = EnumHelp::VERSION
|
9
|
+
spec.authors = ["Lester Zhao"]
|
10
|
+
spec.email = ["zm.backer@gmail.com"]
|
11
|
+
spec.summary = %q{ Extends of ActiveRecord::Enum, which can used in simple_form and internationalization }
|
12
|
+
spec.description = %q{ Make Enum field correctly generate select field. }
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module EnumHelp
|
2
|
+
module I18n
|
3
|
+
|
4
|
+
# overwrite the enum method
|
5
|
+
def enum( definitions )
|
6
|
+
klass = self
|
7
|
+
super( definitions )
|
8
|
+
definitions.each do |name, values|
|
9
|
+
# def status_i18n() statuses.key self[:status] end
|
10
|
+
i18n_method_name = "#{name}_i18n".to_sym
|
11
|
+
define_method(i18n_method_name) do
|
12
|
+
enum_value = self.send(name)
|
13
|
+
::I18n.t("enums.#{klass.to_s.downcase}.#{name}.#{enum_value}", default: enum_value)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
def self.extended(receiver)
|
20
|
+
# receiver.class_eval do
|
21
|
+
# # alias_method_chain :enum, :enum_help
|
22
|
+
# end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module EnumHelp
|
2
|
+
module SimpleForm
|
3
|
+
module BuilderExtension
|
4
|
+
|
5
|
+
def default_input_type_with_enum(*args, &block)
|
6
|
+
att_name = (args.first || @attribute_name).to_s
|
7
|
+
return :enum if (args.last.is_a?(Hash) ? args.last[:as] : @options[:as]).nil? &&
|
8
|
+
object.class.respond_to?(att_name.pluralize)
|
9
|
+
|
10
|
+
default_input_type_without_enum(*args, &block)
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
module InputExtension
|
16
|
+
|
17
|
+
def initialize(*args)
|
18
|
+
super
|
19
|
+
raise "Attribute '#{attribute_name}' has no enum class" unless enum = object.class.send(attribute_name.to_s.pluralize)
|
20
|
+
collect = enum.keys
|
21
|
+
collect.unshift args.last[:prompt] if args.last.is_a?(Hash) && args.last[:prompt]
|
22
|
+
|
23
|
+
if respond_to?(:input_options)
|
24
|
+
input_options[:collection] = collect
|
25
|
+
else
|
26
|
+
@builder.options[:collection] = collect
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
class EnumHelp::SimpleForm::EnumInput < ::SimpleForm::Inputs::CollectionSelectInput
|
37
|
+
include EnumHelp::SimpleForm::InputExtension
|
38
|
+
def input_html_classes
|
39
|
+
super.push('form-control')
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
SimpleForm::FormBuilder.class_eval do
|
44
|
+
include EnumHelp::SimpleForm::BuilderExtension
|
45
|
+
|
46
|
+
map_type :enum, :to => EnumHelp::SimpleForm::EnumInput
|
47
|
+
alias_method :collection_enum, :collection_select
|
48
|
+
alias_method_chain :default_input_type, :enum
|
49
|
+
end
|
data/lib/enum_help.rb
ADDED
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enum_help
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lester Zhao
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
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
|
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: " Make Enum field correctly generate select field. "
|
42
|
+
email:
|
43
|
+
- zm.backer@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- enum_help.gemspec
|
54
|
+
- lib/enum_help.rb
|
55
|
+
- lib/enum_help/i18n.rb
|
56
|
+
- lib/enum_help/railtie.rb
|
57
|
+
- lib/enum_help/simple_form.rb
|
58
|
+
- lib/enum_help/version.rb
|
59
|
+
homepage: ''
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.2.1
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Extends of ActiveRecord::Enum, which can used in simple_form and internationalization
|
83
|
+
test_files: []
|