Top Level Namespace

Defined Under Namespace

Modules: ActiveRecord

Instance Method Summary collapse

Instance Method Details

#PGEnum(**definitions) ⇒ Object

Declare an enum attribute where the values map to strings enforced by PostgreSQL’s enumerated types.

class Conversation < ActiveRecord::Base
  include PGEnum(status: %i[active archived])
end

This is merely a wrapper over traditional enum syntax so that you can define string-based enums with an array of values.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/active_record/pg_enum.rb', line 72

def PGEnum(**definitions)
  values = definitions.values.map do |value|
    if value.is_a? Array
      keys   = value.map(&:to_sym)
      values = value.map(&:to_s)

      Hash[keys.zip(values)]
    else
      value
    end
  end

  Module.new do
    define_singleton_method(:inspect) { %{ActiveRecord::PGEnum(#{definitions.keys.join(" ")})} }

    define_singleton_method :included do |klass|
      klass.enum Hash[definitions.keys.zip(values)]
    end
  end
end