Module: ActiveRecord::PGEnum::PostgreSQLAdapter

Defined in:
lib/active_record/pg_enum/postgresql_adapter.rb

Instance Method Summary collapse

Instance Method Details

#enum_typesObject

Helper method used by the monkeypatch internals. Provides a hash of ENUM types as they exist currently.

Example:

{ "foo_type" => ["foo", "bar", "baz"] }


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_record/pg_enum/postgresql_adapter.rb', line 15

def enum_types
  res = exec_query("    SELECT t.typname AS enum_name, array_agg(e.enumlabel ORDER BY e.enumsortorder) AS enum_value\n    FROM pg_type t\n    JOIN pg_enum e ON t.oid = e.enumtypid\n    JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n    WHERE n.nspname = ANY (current_schemas(false))\n    GROUP BY enum_name;\n  SQL\n\n  coltype = res.column_types[\"enum_value\"]\n  deserialize = if coltype.respond_to?(:deserialize)\n    proc { |values| coltype.deserialize(values) }\n  elsif coltype.respond_to?(:type_cast_from_database)\n    proc { |values| coltype.type_cast_from_database(values) }\n  else\n    proc { |values| coltype.type_cast(values) }\n  end\n\n  res.rows\n    .map { |name, values| [name, values] }\n    .sort { |a, b| a.first <=> b.first }\n    .each_with_object({}) { |(name, values), memo| memo[name] = deserialize.call(values) }\nend\n".strip_heredoc, "SCHEMA")