Module: ActiveRecord::Enum

Defined in:
lib/enum_kit/active_record_patches/enum.rb,
lib/enum_kit/active_record_patches/enum/enum_type.rb

Overview

:nodoc:

Defined Under Namespace

Classes: EnumType

Instance Method Summary collapse

Instance Method Details

#pg_enum(column_name) ⇒ Object

Define a PostgreSQL enum type.

Parameters:

  • column_name (String, Symbol)

    The name of a column representing an enum.



30
31
32
33
# File 'lib/enum_kit/active_record_patches/enum.rb', line 30

def pg_enum(column_name)
  values = pg_enum_values(column_name).map { |v| [v.to_sym, v.to_s] }
  enum(column_name => Hash[values])
end

#pg_enum_values(column_name) ⇒ Array

Retrieve the acceptable values for the enum type associated with the given column.

Parameters:

  • column_name (String, Symbol)

    The name of a column representing an enum.

Returns:

  • (Array)

    The acceptable values for the enum type associated with the column.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/enum_kit/active_record_patches/enum.rb', line 14

def pg_enum_values(column_name)
  type = columns_hash[column_name.to_s]&.sql_type

  raise "Unable to determine '#{table_name}.#{column_name}' type. Did you forget to db:migrate?" if type.blank?

  enums = connection.enums[type.to_sym]

  raise "Unable to retrieve enums for type '#{type}'. Did you forget to db:migrate?" if enums.nil?

  enums
end