Home

Awesome

SimpleEnum

Gem Version Build Status Code Climate

Unobtrusive enum-like fields for ActiveRecord and Ruby, brings enums functionality to ActiveRecord and Mongoid models (built for Rails 4+).

Since version 2.0, simple_enum is no longer compatible with Rails 3.x or Ruby 1.8, use version 1.6 instead: https://github.com/lwe/simple_enum/tree/legacy-1.x

Note: a recent search on github for enum turned out, that there are many, many similar solutions. In fact starting with Rails 4.1, there's ActiveRecord::Enum which provides some of the functionality, but is IMHO pretty limited and too strict in the defaults it provides.

ActiveRecord Quick start

Add this to a model:

class User < ActiveRecord::Base
  as_enum :gender, female: 1, male: 0
end

Then create the required gender_cd column using migrations:

class AddGenderColumnToUser < ActiveRecord::Migration
  def self.up
    add_column :users, :gender_cd, :integer
  end

  def self.down
    remove_column :users, :gender_cd
  end
end

Mongoid Quick start

Due to the dependency on ActiveModel 4.x, the Mongoid integration is only available for mongoid 4.0.0 (which is at beta1 at the moment). If you intend to use simple_enum with another version of mongoid, use version 1.6 instead.

Load mongoid support in the Gemfile:

gem 'simple_enum', '~> 2.3.0' , require: 'simple_enum/mongoid'

Add this to a model:

class User
  include Mongoid::Document
  include SimpleEnum::Mongoid

  as_enum :gender, female: 1, male: 0
end

The primary difference between AR and mongoid is, that additionaly a field is added to mongoid automatically, the field can be customized by setting field: option, or disabled by setting field: false.

Working with enums

Now it's possible to pull some neat tricks on the new column, yet the original db column (gender_cd) is still intact and not touched by anything.

jane = User.new
jane.gender = :female
jane.female?   # => true
jane.male?     # => false
jane.gender    # => :female
jane.gender_cd # => 1

Easily switch to another value using the bang methods, this does not save the record, only switch the value.

joe = User.new
joe.male!     # => :male
joe.gender    # => :male
joe.gender_cd # => 0

Accessing actual enum values is possible at the class level:

User.genders                            # => #<SimpleEnum::Enum:0x0....>
User.genders[:male]                     # => 0
User.genders.values_at(:male, :female)  # => [0, 1] (since 2.1.0)
User.females                            # => #<ActiveRecord::Relation:0x0.....> (WHERE gender_cd = 1)

By default, scope names are generated as pluralized forms of the defined enum values e.g.

class Booking < ActiveRecord::Base
  as_enum :status, %i{active cancelled pending}
end

would generate the following:

Booking.actives     # => #<ActiveRecord::Relation:0x0.....> (WHERE status_cd = 1)
Booking.cancelleds  # => #<ActiveRecord::Relation:0x0.....> (WHERE status_cd = 2)
Booking.pendings    # => #<ActiveRecord::Relation:0x0.....> (WHERE status_cd = 3)

By setting pluralize_scopes: false will not generate pluralized versions of scopes e.g.

class Booking < ActiveRecord::Base
  as_enum :status, %i{active cancelled pending}, pluralize_scopes: false
end

would generate the following:

Booking.active     # => #<ActiveRecord::Relation:0x0.....> (WHERE status_cd = 1)
Booking.cancelled  # => #<ActiveRecord::Relation:0x0.....> (WHERE status_cd = 2)
Booking.pending    # => #<ActiveRecord::Relation:0x0.....> (WHERE status_cd = 3)

Wait, there's more!

View Helpers

Require translated enum values? See SimpleEnum::ViewHelpers for more details and functions. Disclaimer: these methods are release candidate quality so expect them to change in future versions of SimpleEnum.

Extensions

simple_enum provides hooks to extend its functionality, starting with 2.3.0 the following extensions can be used:

Best practices

Do not use values named after existing, or well known method names, like new, create etc.

# BAD, conflicts with Rails ActiveRecord Methods (!)
as_enum :handle, [:new, :create, :update]

# GOOD, prefixes all methods
as_enum :handle, [:new, :create, :update], prefix: true

Searching for certain values by using the finder methods:

User.females # => returns an ActiveRecord::Relation

Contributors

License & Copyright

Copyright (c) 2011-2015 by Lukas Westermann, Licensed under MIT License (see LICENSE file)