module EnumTable::SchemaDumper

Constants

COLUMN_ATTRIBUTES

Public Instance Methods

ignore_tables_with_enum_table() click to toggle source
# File lib/enum_table/schema_dumper.rb, line 24
def ignore_tables_with_enum_table
  ignore_tables_without_enum_table + @connection.enum_tables << 'enum_tables'
end
tables_with_enum_table(stream) click to toggle source
# File lib/enum_table/schema_dumper.rb, line 10
def tables_with_enum_table(stream)
  tables_without_enum_table(stream)
  table_names = @connection.enum_tables
  table_names.each do |table_name|
    stream.puts "  create_enum_table #{table_name.inspect}, force: true do |t|"
    enum_table_column(stream, table_name, 'value', SchemaStatements::DEFAULT_VALUE_ATTRIBUTES)
    @connection.execute("SELECT id, value FROM #{@connection.quote_table_name table_name} ORDER BY id").each do |row|
      stream.puts "    t.add #{row[1].to_s.inspect}, #{row[0]}"
    end
    stream.puts "  end"
    stream.puts
  end
end

Private Instance Methods

enum_table_column(stream, table_name, column_name, defaults) click to toggle source
# File lib/enum_table/schema_dumper.rb, line 30
def enum_table_column(stream, table_name, column_name, defaults)
  column = @connection.columns(table_name).find { |c| c.name == column_name }
  custom_attributes = {}
  COLUMN_ATTRIBUTES.each do |attribute|
    value = column.send(attribute)
    value == defaults[attribute] or
      custom_attributes[attribute] = value
  end
  if custom_attributes.present?
    formatted = custom_attributes.map { |k, v| "#{k}: #{v.inspect}" }.join(', ')
    stream.puts "    t.#{column_name} #{formatted}"
  end
end