Module: DashApi::Schema

Defined in:
app/services/dash_api/schema.rb

Constant Summary collapse

EXCLUDED_TABLES =
[
  'ar_internal_metadata', 
  'schema_migrations'
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#tablesObject

Returns the value of attribute tables.



9
10
11
# File 'app/services/dash_api/schema.rb', line 9

def tables
  @tables
end

Class Method Details

.db_schemaObject



25
26
27
28
29
30
31
32
33
34
# File 'app/services/dash_api/schema.rb', line 25

def self.db_schema 
  @tables = table_names
  schema = {
    tables: @tables
  }
  @tables.each do |table_name|
    schema[table_name] = table_schema(table_name)
  end      
  schema   
end

.foreign_key?(column_name) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'app/services/dash_api/schema.rb', line 50

def self.foreign_key?(column_name)      
  column_name[-2..-1]&.downcase === 'id' && column_name.downcase != 'id'
end

.foreign_table(column_name) ⇒ Object



54
55
56
57
# File 'app/services/dash_api/schema.rb', line 54

def self.foreign_table(column_name)
  table_prefix = column_name[0...-3]      
  @tables.find{|t| t === table_prefix || t === table_prefix.pluralize }
end

.render_column(column) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/services/dash_api/schema.rb', line 36

def self.render_column(column)      
  {
    friendly_name: column.human_name,
    name: column.name,
    type: column.type,
    array: column.array,
    default: column.default,
    limit: column..limit,
    precision: column..precision,
    foreign_key: foreign_key?(column.name),
    foreign_table: foreign_table(column.name) 
  }
end

.table_namesObject



11
12
13
14
# File 'app/services/dash_api/schema.rb', line 11

def self.table_names
  @tables = ActiveRecord::Base.connection.tables
  @tables.filter!{|t| !EXCLUDED_TABLES.include?(t)}.sort!
end

.table_schema(table_name) ⇒ Object



16
17
18
19
20
21
22
23
# File 'app/services/dash_api/schema.rb', line 16

def self.table_schema(table_name) 
  @tables = table_names 
  dash_table = DashTable.modelize(table_name)
  dash_table.reset_column_information      
  dash_table.columns.map{ |column| 
    render_column(column)
  } 
end