Module: Arql::Commands::Table

Defined in:
lib/arql/commands/table.rb

Class Method Summary collapse

Class Method Details

.get_table_name(name) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/arql/commands/table.rb', line 6

def get_table_name(name)
  name = name.to_s
  return name if name =~ /^[a-z]/
  if Object.const_defined?(name)
    klass = Object.const_get(name)
    return klass.table_name if klass < ActiveRecord::Base
  end
  name
end

.table_info(table_name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/arql/commands/table.rb', line 22

def table_info(table_name)
  t = []
  t << ['PK', 'Name', 'SQL Type', 'Ruby Type', 'Limit', 'Precision', 'Scale', 'Default', 'Nullable', 'Comment']
  t << nil
  connection = ::ActiveRecord::Base.connection
  connection.columns(table_name).each do |column|
    pk = if [connection.primary_key(table_name)].flatten.include?(column.name)
           'Y'
         else
           ''
         end
    t << [pk, column.name, column.sql_type,
          column..type, column..limit || '',
          column..precision || '', column..scale || '', column.default || '',
          column.null, column.comment || '']
  end
  t
end

.table_info_table(table_name) ⇒ Object



16
17
18
19
20
# File 'lib/arql/commands/table.rb', line 16

def table_info_table(table_name)
  Terminal::Table.new do |t|
    table_info(table_name).each { |row| t << (row || :separator) }
  end
end