Project

pg-enum

0.0
No commit activity in last 3 years
No release in over 3 years
Adds support for Postgres (enum)erated types to Active Record
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.15
~> 5.0
~> 12.0
~> 0.49

Runtime

>= 0.18
>= 4.2
 Project Readme

PgEnum Build Status

Add support for Postgres (enum)erated types to Active Record

Installation

Add this line to your application's Gemfile:

gem 'pg-enum'

And then execute:

$ bundle

Or install it yourself as:

$ gem install pg-enum

Usage

Create an enum type like:

# db/migrate/20170818114925_add_status_to_conversations.rb
class AddStatusToConversations < ActiveRecord::Migration[5.1]
  include PgEnum::MigrationHelpers

  def up
    create_enum :conversation_status, [:active, :archived]
    add_column :conversations, :status, :conversation_status, default: "created"
  end

  def down
    remove_column :conversations, :status
    drop_enum :conversation_status
  end
end

To add PgEnum to an Active Record model, simply include the PgEnum module.

class Conversation < ActiveRecord::Base
  include PgEnum

  pg_enum :status
end

The API is the same as ActiveRecord::Enum.

Conversation.statuses
# => { active: 'active', archived: 'archived' }

conversation = Conversation.create
conversation.status   # => "created"
conversation.created? # => true

conversation.archived!
conversation.status    # => "archived"
conversation.archived? # => true

Conversation.active.count   # => 0
Conversation.archived.count # => 1

License

The gem is available as open source under the terms of the MIT License.