Class: Annotato::EnumFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/annotato/enum_formatter.rb

Overview

Formats PostgreSQL native enum type definitions for columns whose sql_type refers to a custom DB enum (e.g. access_link_category). Queries pg_enum directly so the output reflects the actual database definition, not the ActiveRecord-level enum mapping already visible in the model source.

Class Method Summary collapse

Class Method Details

.format(conn, columns) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/annotato/enum_formatter.rb', line 9

def self.format(conn, columns)
  # Collect columns backed by a native PG enum type.
  # A native enum type is one that exists in pg_type with typtype = 'e'.
  enum_columns = columns.select { |col| pg_enum_type?(conn, col.sql_type) }
  return [] if enum_columns.empty?

  enum_columns.map do |col|
    labels = pg_enum_labels(conn, col.sql_type)
    lines = ["#  #{col.name} (#{col.sql_type}): ["]
    lines += labels.map.with_index { |label, i|
      comma = i == labels.size - 1 ? "" : ","
      "#    #{label}#{comma}"
    }
    lines << "#  ]"
    lines.join("\n")
  end
end