Giter Site home page Giter Site logo

enum_help's Introduction

EnumHelp

Gem Version Build Status

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

Breaking Changes

Version 0.0.15 changes the behaviour of namespaced modules as per this commit: standard en.yml structures for enums Foo::Bar::Baz#abc and Foo::Bar::Bat#def change from

enums:
  foo/bar/baz:
    abc:
      lorem: 'Ipsum'
  foo/bar/bat:
    def:
      lorem: 'Ipsum'

To

enums:
  foo:
    bar:
      baz:
        abc:
          lorem: 'Ipsum'
      bat:
        def:
          lorem: 'Ipsum'

For different I18n backends, adjust accordingly as namespaced modules are now referenced by . rather than /.

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

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 status' %>

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

From version 0.0.10, enum_help can automatically generate radio buttons with i18n labels.

e.g.

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

I18n local file example:

# config/locales/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 raised 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. Run test rspec
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create new Pull Request

enum_help's People

Contributors

arokettu avatar jhjguxin avatar joelbarker2011 avatar kakipo avatar kelion avatar maksimabramchuk avatar maxehmookau avatar mishina2228 avatar mrhead avatar neojin avatar shekibobo avatar thiagoa avatar tobypinder avatar zmbacker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

enum_help's Issues

i18n array with int keys

For sorting action need [["planed", 0], ["done", 1], ["loaded", 2]] with translated key.
But Model.statuses_i18n returns array with value = translated(key) and there is no integer keys to find something in database.
Maybe it's not so hard to make enums translated with integer keys?

"No" shown as "false"

When I have an enum option that is translated as "No", simple_form shows "false".
Am I wrong?

PS: workaround is adding a space after "No":

enums:
  user:
    product_already_have_km:
      already_customer: 'Sì'
      new_customerr: 'No '

Translation for enums in model concerns

I use enum in model concern because of using it in multiple models:

module Sizeable
  extend ActiveSupport::Concern

  included do
    enum pizza_size: { d22: 22, d33: 33, d38: 38 }
  end
end

How should I write translations in .yml file? I can write duplicated translations for each model used concern, but this isn't DRY

display enum field value in view

thanks for such a great gem.
i have a question, is there a way to overwrite the default behavior in view level? Because sometimes I just want to display the actual value of the enum field in the HTML page. But currently if I do

<%=task.status%>

In the html file, I see following

{:pending => "Pending", :confirmed => "Confirm" ...} 

It will be great if I can just see its status.
Thanks

Select field not generated

If I use <%= f.input :status %> (as explained in the documentation) it generates a numeric input field instead of a select. To achieve the expected behavior I have to put = f.input :state, collection: Project.statuses_i18n, label_method: :last, value_method: :first. Is this a Bug or an error in the documentation?

input as radio buttons doesn't work

When I try to use this gem with simple form and try to show values as radio buttons it shows radio buttons with Yes, No labels (there are only two options in enum for now).

class Plan
  enum payout_period: [:monthly, :at_the_end]
end
= f.input :payout_period, as: :radio_buttons

Adding gem to Rails 5 project changes belongs_to behavior

I'm not sure what is causing this, but when this gem is added to a Rails 5 project, belongs_to associations are no longer required by default.

Current workaround is to add required: true to all belongs_to relationships where you want the Rails 5 default behavior.

simple from item_wrapper_tag and item_wrapper_class not working

Wrapper

 config.wrappers :vertical_inline_radio_and_checkboxes, tag: 'fieldset', class: 'form-group', error_class: 'has-danger', 
    item_wrapper_tag: :span, item_wrapper_class: "form-check-inline", item_label_class: 'form-check-label' do |b|
    b.use :html5
    b.optional :readonly
    b.use :label, class: 'form-control-label'
    b.wrapper tag: 'div', class: 'row' do |c|
      c.use :input, class: 'form-check-input'
    end
    b.use :error, wrap_with: { tag: 'span', class: 'form-control-feedback' }
    b.use :hint,  wrap_with: { tag: 'span', class: 'form-text text-muted' }
   
  end

expected result

<fieldset class="form-group radio_buttons required user_enrollment col-md-12">
    <label class="form-control-label radio_buttons required"><abbr title="required">*</abbr>Your are</label>
    <div class="row">
       <input type="hidden" name="user[enrollment]" value="">
       <span class="radio form-check-inline">
           <label class="form-check-label" for="user_enrollment_student">
               <input class="form-check-input radio_buttons required" type="radio" value="student" name="user[enrollment]" id="user_enrollment_student">
                Student
           </label>
       </span>
       <span class="radio form-check-inline">
          <label class="form-check-label" for="user_enrollment_lecturer">
              <input class="form-check-input radio_buttons required" type="radio" value="lecturer" name="user[enrollment]" id="user_enrollment_lecturer">
               Lecturer
           </label>
       </span>
    </div>
</fieldset>

result

<fieldset class="form-group radio_buttons required user_enrollment col-md-12">
    <label class="form-control-label radio_buttons required"><abbr title="required">*</abbr>Your are</label>
    <div class="row">
       <input type="hidden" name="user[enrollment]" value="">
       <span class="radio radio radio">
           <label class="form-check-label" for="user_enrollment_student">
               <input class="form-check-input radio_buttons required" type="radio" value="student" name="user[enrollment]" id="user_enrollment_student">
                Student
           </label>
       </span>
       <span class="radio radio radio">
          <label class="form-check-label" for="user_enrollment_lecturer">
              <input class="form-check-input radio_buttons required" type="radio" value="lecturer" name="user[enrollment]" id="user_enrollment_lecturer">
               Lecturer
           </label>
       </span>
    </div>
</fieldset>

The method .references() must contain arguments

I have a column called reference of type string. It is not related to enums. It works just fine when not using enum_help gem. But by just adding enum_help gem in Gemfile it gives The method .references() must contain arguments in this line of view:

<%= f.input :reference %>

Stack trace:

activerecord (4.1.2.rc1) lib/active_record/relation/query_methods.rb:1086:in `check_if_method_has_arguments!'
activerecord (4.1.2.rc1) lib/active_record/relation/query_methods.rb:182:in `references'
activerecord (4.1.2.rc1) lib/active_record/querying.rb:10:in `references'
enum_help (0.0.6) lib/enum_help/simple_form.rb:20:in `initialize'
simple_form (3.1.0.rc1) lib/simple_form/form_builder.rb:501:in `new'
simple_form (3.1.0.rc1) lib/simple_form/form_builder.rb:501:in `find_input'
simple_form (3.1.0.rc1) lib/simple_form/form_builder.rb:112:in `input'
app/views/products/_form.html.erb:5:in `block in _app_views_products__form_html_erb___1832036717949421423_70310092763500'

Capitalize the outputted choices?

Is there a way to capitalize the outputted choices from an enum? For instance, I have:

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

This outputs the statuses that I have declared with an enum, but they are lowercase. I just want to capitalize the first letter of each choice.

Any way to do this?

Enum delegated attribute

Hello,

this gem works great!

One feature could be great, if it could work with delegated attribute. Eg. when is attribute with enum values delegated to another class simple_form doesn't show select box.

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.