Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Enumerated attributes with I18n and ActiveRecord/Mongoid support
branch: master

This branch is 329 commits behind brainspec:master

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

Boolean methods:

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

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
Something went wrong with that request. Please try again.