Enumerated values stored as integers accessed with symbols for ActiveRecord
Ruby
Permalink
Failed to load latest commit information.
lib
test Update for Rails 4 compatibility Dec 19, 2013
.gitignore
.ruby-version
.travis.yml Bump Ruby version Dec 19, 2013
Gemfile
Guardfile
LICENSE.txt
README.md
Rakefile
attr_enum_accessor.gemspec

README.md

Enumerated Attribute Accessor

Build Status Gem Version

Store enumerated attributes in your database as integers while using type checked symbols in code.

RubyDoc Documentation

Usage

Create integer columsn with _value suffix to store enumerated attribute values.

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :email, null: false
      t.string :password_digest
      t.integer :role_value, null: false, default: 0
      t.timestamps
    end
  end
end

Create a hash in your model to map symbols to enumerated attribute values.

class User < ActiveRecord::Base
  ROLE = {
    user:  0,
    admin: 1,
  }

  include AttrEnumAccessor
  attr_enum_accessor :role, with: ROLE
end

Access your enumerated values using symbols.

u = User.new
u.role = :admin
puts u.role_value

# throws exception
u.role = :unknown_role

puts User.where_role(:admin).count

Installation

Add this line to your application's Gemfile:

gem 'attr_enum_accessor'

And then execute:

$ bundle

Contributing

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