No commit activity in last 3 years
No release in over 3 years
Helper to derive an ActiveRecord enum from existing PostgreSQL database constraint.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.11
~> 10.0
~> 3.0

Runtime

 Project Readme

EnumFromPgConstraint

Derive a Rails enum from an existing PostgreSQL constraint.

Usage:

Given a migration to create table Thing with a PostgreSQL constraint:

class CreateThing < ActiveRecord::Migration
  def up
    create_table :things do |t|
      t.string :name
      t.string :status, null: false, default: 'pending'
      t.timestamps null: false
    end

    execute <<-SQL.strip_heredoc
      ALTER TABLE things
      ADD CONSTRAINT allowed_statuses
      CHECK (status IN (
        'pending', -- thing not yet attempted
        'success', -- thing succeeded
        'failure'  -- thing failed
      ));
    SQL
  end

  def down
    drop_table :things
  end
end

Add the constraint to your Model:

class Thing < ActiveRecord::Base
  include EnumFromPgConstraint

  enum_from_pg_constraint :status
end
$ rails c
> Thing.statuses
=> {"pending"=>"pending", "success"=>"success", "failure"=>"failure"}

> Thing.create(name: 'Hello World!')
> Thing.pending
=> [#<Thing:0x007fec93d6b5f0
  id: 1,
  name: 'Hello World!'
  status: "pending",
  created_at: Sun, 31 Jan 2016 18:53:25 PST -08:00,
  updated_at: Sun, 31 Jan 2016 18:53:25 PST -08:00>]

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/somlor/enum_from_pg_constraint. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

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