Class: DbdocEngine::SchemaDiagramController

Inherits:
ApplicationController show all
Defined in:
app/controllers/dbdoc_engine/schema_diagram_controller.rb

Instance Method Summary collapse

Instance Method Details

#data ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/dbdoc_engine/schema_diagram_controller.rb', line 15

def data
  @tables = DbDesignDynamicTableQueries.active_tables_with_columns
              .includes(:db_design_dynamic_columns, :db_design_table_group)

  @foreign_keys = DbdocEngine::DbDesignDynamicColumnQueries.all_foreign_keys_with_tables
                    .includes(db_design_dynamic_table: :db_design_dynamic_columns)
  @existing_table_names = DbDesignDynamicTable.where(deleted_at: nil).pluck(:table_name).to_set

  dot_code = generate_graphviz_dot(@tables)
  svg = render_graphviz_svg(dot_code)

  selected_group_id = params[:group_id].presence if params[:group_id] != "all"
  @group_colors = {}

  @tables.each do |table|
    group_color = table.db_design_table_group&.group_color || "#cccccc"
    if selected_group_id.nil? || table.db_design_table_group_id.to_s == selected_group_id
      @group_colors[table.table_name] = group_color
    else
      @group_colors[table.table_name] = "#808080"
    end
  end

  @column_data = generate_column_data(@tables)
  @relationship_data = generate_relationship_data

  respond_to do |format|
    format.json do
      render json: {
        diagram_svg: svg,
        group_colors: @group_colors,
        column_data: @column_data,
        relationship_data: @relationship_data
      }
    end
  end
end

#index ⇒ Object



9
10
11
12
13
# File 'app/controllers/dbdoc_engine/schema_diagram_controller.rb', line 9

def index
  @groups = DbdocEngine::DbDesignTableGroupQueries.groups_with_tables_having_columns

  @foreign_keys = DbdocEngine::DbDesignDynamicColumnQueries.all_foreign_keys_with_tables
end

#lighten_color(hex, amount = 0.7) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'app/controllers/dbdoc_engine/schema_diagram_controller.rb', line 65

def lighten_color(hex, amount = 0.7)
  r = hex[1..2].to_i(16)
  g = hex[3..4].to_i(16)
  b = hex[5..6].to_i(16)
  r = [ (r + (255 - r) * amount), 255 ].min.round
  g = [ (g + (255 - g) * amount), 255 ].min.round
  b = [ (b + (255 - b) * amount), 255 ].min.round
  "#%02X%02X%02X" % [ r, g, b ]
end

#render_graphviz_svg(dot_code) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/dbdoc_engine/schema_diagram_controller.rb', line 53

def render_graphviz_svg(dot_code)
  require "open3"
  svg = nil
  Open3.popen3("dot -Tsvg") do |stdin, stdout, stderr, wait_thr|
    stdin.puts dot_code
    stdin.close
    svg = stdout.read
    raise "Graphviz error: #{stderr.read}" if wait_thr.value != 0
  end
  svg
end