Better Rails Enumerations
Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
test
.gitignore
.travis.yml
Gemfile
Rakefile
Readme.md
enumerations.gemspec

Readme.md

Enumerations

Rails plugin for enumerations in ActiveRecord models.

Build Status

Installation

Inside your Gemfile add the following:

gem 'enumerations'

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

Or you can use value method for defining your enumerations:

class Status < Enumeration::Base
  value :draft,           id: 1, name: 'Draft'
  value :review_pending,  id: 2, name: 'Review pending'
  value :published,       id: 3, name: 'Published'
end

Include enumerations for integer fields in other models:

class Post < ActiveRecord::Base
  enumeration :status
  validates :status_id, presence: true
end

You can pass attributes to specify which enumeratior and which column to use:

class Post < ActiveRecord::Base
  enumeration :status,
              foreign_key: :post_status_id, # specifies which column to use
              class_name: Post::Status # specifies the class of the enumerator
  validates :post_status_id, presence: true
end

Set enumerations, find enumerations by symbol:

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

Or you can set enumerations on this way:

@post.status = Status.draft

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

Advance Usage

Except id and name you can specify other attributes to your enumerations:

class Status < Enumeration::Base
  value :draft,           id: 1, name: 'Draft'
  value :review_pending,  id: 2, name: 'Review pending', description: 'Some description...'
  value :published,       id: 3, name: 'Published'
end

Every enumeration has id, name and description methods. If you call method that is not in attribute list for enumeration, it will return nil.

Status.review_pending.description             # => 'Some description...'
Status.draft.description                      # => nil

Author

Copyright © 2010 Tomislav Car, Infinum Ltd.