Giter Site home page Giter Site logo

enum_help's Introduction

EnumHelp

Help ActiveRecord::Enum feature to work fine with I18n and simple_form.

Make Enum field correctly generate select field.

As you know in Rails 4.1.0 , ActiveRecord supported Enum method. But it doesn't work fine with I18n and simple_form.

This gem can help you work fine with Enum feather, I18n and simple_form

Installation

Add this line to your application's Gemfile:

gem 'enum_help'

And then execute:

$ bundle

Or install it yourself as:

$ gem install enum_help

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
# > nopayment
order.status_i18n # if you have an i18n file defined as following, it will return "未支付".
# > 未支付

You can also fetch the translated enum collection, if you need to:

Order.statuses_i18n

In _form.html.erb using simple_form:

<%= 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 %>

Other arguments for simple_form are supported perfectly.

e.g.

<%= 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.

<%= f.input :status, as: :radio_buttons %>

I18n local file example:

# 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. 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

Thanks for all the contributors.

Contributing

  1. Fork it ( http://github.com/zmbacker/enum_help/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

enum_help's People

Contributors

kelion avatar mrhead avatar neojin avatar shekibobo avatar thiagoa avatar voidpart avatar zmbacker avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.