README.md in enum_help-0.0.9 vs README.md in enum_help-0.0.10

- old
+ new

@@ -24,75 +24,99 @@ $ gem install enum_help ## Usage -required Rails 4.1.x +Required Rails 4.1.x In model file: - class Order < ActiveRecord::Base - enum status: { "nopayment" => 0, "finished" => 1, "failed" => 2, "destroyed" => 3 } +```ruby +class Order < ActiveRecord::Base + enum status: { "nopayment" => 0, "finished" => 1, "failed" => 2, "destroyed" => 3 } - def self.restricted_statuses - statuses.except :failed, :destroyed - end - end + def self.restricted_statuses + statuses.except :failed, :destroyed + end +end +``` You can call: - order = Order.first - order.update_attribute :status, 0 - order.status - # > nopayment - order.status_i18n # if you have an i18n file defined as following, it will return "未支付". - # > 未支付 +```ruby +order = Order.first +order.update_attribute :status, 0 +order.status +# > nopayment +order.status_i18n # if you have an i18n file defined as following, it will return "未支付". +# > 未支付 +``` -In _form.html.erb using simple_form: +You can also fetch the translated enum collection, if you need to: - <%= f.input :status %> +```ruby +Order.statuses_i18n +``` +In `_form.html.erb` using `simple_form`: + +```erb +<%= f.input :status %> +``` + This will generate select field with translations automaticlly. And if you want to generate select except some values, then you can pass a collection option. - <%= f.input :status Order.restricted_statuses %> +```erb +<%= f.input :status, Order.restricted_statuses %> +``` +Other arguments for `simple_form` are supported perfectly. +e.g. -Other arguments for simple_form are supported perfectly. +```erb +<%= f.input :status, prompt: 'Please select a stauts' %> +<%= f.input :status, as: :string %> +``` + +From version 0.0.10, enum_help can automaticlly generate radio buttons with i18n labels. + e.g. +```erb +<%= f.input :status, as: :radio_buttons %> +``` - <%= f.input :status, prompt: 'Please select a stauts' %> - - <%= f.input :status, as: :string %> + I18n local file example: - # config/locals/model/order.zh-cn.yml - zh-cn: - enums: - order: - status: - finished: 完成 - nopayment: 未支付 - failed: 失败 - destroyed: 已删除 +```yaml +# config/locals/model/order.zh-cn.yml +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. +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 raise an error as below: NoMethodError: super: no superclass method `enum' for... ## Thanks -* [mrhead](https://github.com/mrhead) -* [Jin Lee](https://github.com/neojin) + +Thanks for all the [contributors](https://github.com/zmbacker/enum_help/graphs/contributors). ## Contributing 1. Fork it ( http://github.com/zmbacker/enum_help/fork ) 2. Create your feature branch (`git checkout -b my-new-feature`)