Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
test
.gitignore
Rakefile
Readme.md
enumerations.gemspec

Readme.md

Enumerations

Rails plugin for enumerations in ActiveRecord models.

Install

If you are using Bundler (and you should be), just add it as a gem.

gem 'enumerations'

If not, then install it as a plugin.

rails plugin install git://github.com/infinum/enumerations.git

Usage

Create a model for your enumerations

class Status < Enumeration::Base
  values :draft => {:id => 1, :name => 'Draft'},
         :review_pending => {:id => 2, :name => 'Review pending'},
         :published => {:id => 3, :name => 'Published'}  
end

Include enumerations for integer fields in other models

class Post < ActiveRecord::Base
  enumeration :status
  validates_presence_of :body, :title, :status_id
end

Set enumerations, find enumerations by symbol

@post = Post.first
@post.status = Status.find(:draft)
@post.save

Find enumerations by id

@post.status = Status.find(2) # => Review pending
@post.save

Compare enumerations

@post.status == :published                  # => true
@post.status == 3                           # => true
@post.status == Status.find(:published)     # => true
@post.status.published?                     # => true

Get all enumerations

Status.all

Use in forms

%p
  = f.label :status_id
  %br
  = f.collection_select :status_id, Status.all, :id, :name

TODO

  • support for object values (not just symbols and strings)

Author

Copyright (c) 2010 Tomislav Car, Infinum

Something went wrong with that request. Please try again.