EnumI18nHelper
Help ActiveRecord::Enum feature to work fine with I18n.
As you know in Rails 4.1.0 , ActiveRecord supported Enum method. But it doesn't work fine with I18n
This gem can help you work fine with Enum and I18n
Installation
Add this line to your application's Gemfile:
gem 'enum_i18n_helper'
And then execute:
$ bundle
Or install it yourself as:
$ gem install enum_i18n_helper
Usage
Required Rails 4.1.x
In model file:
class Order < ActiveRecord::Base
enum status: { "nopayment" => 0, "finished" => 1, "failed" => 2, "destroyed" => 3 }
def self.restricted_statuses
statuses.except :failed, :destroyed
end
end
You can call:
order = Order.first
order.update_attribute :status, 0
order.status
order.status_i18n
You can also fetch the translated enum collection, if you need to:
Order.statuses_i18n
I18n local file example:
zh-cn:
enums:
order:
status:
finished: 完成
nopayment: 未支付
failed: 失败
destroyed: 已删除
Notice
If you want to use enum feature, field of your table can't be named with reference
.
When it is named with 'reference' and define enum in model file, there will be raised an error as below:
NoMethodError: super: no superclass method `enum' for...
Thanks
Thanks for all the contributors.
Contributing
- Fork it ( https://github.com/lyohich812/enum_i18n_helper/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Run test
rspec
- Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request