Enumerated attributes with I18n and ActiveRecord/Mongoid support
Ruby
Pull request Compare This branch is 372 commits behind brainspec:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
gemfiles
lib
test
.gitignore
.travis.yml
CHANGELOG.md
Gemfile
MIT-LICENSE
README.md
Rakefile
enumerize.gemspec

README.md

Enumerize TravisCI Gemnasium

Enumerated attributes with I18n and ActiveRecord/Mongoid support

Installation

Add this line to your application's Gemfile:

gem 'enumerize'

And then execute:

$ bundle

Or install it yourself as:

$ gem install enumerize

Usage

Basic:

class User
  include Enumerize

  enumerize :sex, :in => [:male, :female]
end

ActiveRecord:

class User < ActiveRecord::Base
  include Enumerize

  enumerize :sex, :in => [:male, :female]

  enumerize :role, :in => [:user, :admin], :default => :user
end

Mongoid:

class User
  include Mongoid::Document
  include Enumerize

  field :role
  enumerize :role, :in => [:user, :admin], :default => :user
end

I18n:

en:
  enumerize:
    user:
      sex:
        male: "Male"
        female: "Female"

or if you use sex attribute across several models you can use this:

en:
  enumerize:
    sex:
      male: "Male"
      female: "Female"

get attribute value:

@user.sex_text # or @user.sex.text

use it with forms:

<%= form_for @user do |f| %>
  <%= f.select :sex, User.sex.options %>
<% end %>

If you are using SimpleForm gem you don't need to specify input type (:select by default) and collection:

<%= simple_form_for @user do |f| %>
  <%= f.input :sex %>
<% end %>

and if you want it as radio buttons:

<%= simple_form_for @user do |f| %>
  <%= f.input :sex, :as => :radio_buttons %>
<% end %>

If you are using Formtastic gem you also don't need to specify input type (:select by default) and collection:

<%= semantic_form_for @user do |f| %>
  <%= f.input :sex %>
<% end %>

and if you want it as radio buttons:

<%= semantic_form_for @user do |f| %>
  <%= f.input :sex, :as => :radio %>
<% end %>

Boolean methods:

user.sex = :male
user.sex.male? #=> true
user.sex.female? #=> false

To make some attributes shared across different classes it's possible to define them in a separate module and then include it into classes:

module PersonEnumerations
  include Enumerize

  enumerize :sex, :in => %w[male female]
end

class Person
  include PersonEnumerations
end

class User
  include PersonEnumerations
end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request