Class: DbdocAdmin::DbDesignDynamicTablesController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/dbdoc_engine/dbdoc_admin/db_design_dynamic_tables_controller.rb

Instance Method Summary collapse

Instance Method Details

#check_column_dependency ⇒ Object

Checks if a column is referenced in other tables



95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/dbdoc_engine/dbdoc_admin/db_design_dynamic_tables_controller.rb', line 95

def check_column_dependency
  table = DbdocEngine::DbDesignDynamicTableQueries.find_table_with_deleted(params[:table_id])
  is_referenced = DbdocEngine::DbDesignDynamicTableQueries.check_column_is_referenced(
    table.table_name,
    params[:column_name],
    only_active: true
  )

  render json: { is_referenced: is_referenced }
end

#columns ⇒ Object

Fetches columns of a table in JSON format



107
108
109
110
# File 'app/controllers/dbdoc_engine/dbdoc_admin/db_design_dynamic_tables_controller.rb', line 107

def columns
  table = DbdocEngine::DbDesignDynamicTableQueries.find_by_table_name(params[:table_name])
  render json: table ? table.db_design_dynamic_columns.select(:id, :column_name) : []
end

#create ⇒ Object

Creates a new dynamic table using the service layer



46
47
48
# File 'app/controllers/dbdoc_engine/dbdoc_admin/db_design_dynamic_tables_controller.rb', line 46

def create
  DbDesignDynamicTableHandlerService.new(self, table_params).create
end

#deleted_tables ⇒ Object

Displays deleted tables with filters and pagination



17
18
19
20
21
# File 'app/controllers/dbdoc_engine/dbdoc_admin/db_design_dynamic_tables_controller.rb', line 17

def deleted_tables
  @tables = DbdocEngine::DbDesignDynamicTableQueries.all_deleted_tables_with_columns_and_groups
  load_filtered_tables(params[:page])
  @total_table_count = DbdocEngine::DbDesignDynamicTableQueries.count_all_deleted_tables
end

#destroy ⇒ Object

Deletes a table (soft delete)



61
62
63
64
# File 'app/controllers/dbdoc_engine/dbdoc_admin/db_design_dynamic_tables_controller.rb', line 61

def destroy
  handle_table_action(:soft_delete, @table.table_name, I18n.t("dbdoc.table_deletion"),
                      dbdoc_admin_db_design_dynamic_tables_path)
end

#edit ⇒ Object

Edits an existing dynamic table



51
52
53
# File 'app/controllers/dbdoc_engine/dbdoc_admin/db_design_dynamic_tables_controller.rb', line 51

def edit
  @other_tables = DbdocEngine::DbDesignDynamicTableQueries.find_all_except(@table.id)
end

#export_all_to_excel ⇒ Object

Exports all tables data to Excel



118
119
120
# File 'app/controllers/dbdoc_engine/dbdoc_admin/db_design_dynamic_tables_controller.rb', line 118

def export_all_to_excel
  DbDesignDynamicTableExportService.new(params, self).export_all
end

#export_to_excel ⇒ Object

Exports table data to Excel



113
114
115
# File 'app/controllers/dbdoc_engine/dbdoc_admin/db_design_dynamic_tables_controller.rb', line 113

def export_to_excel
  DbDesignDynamicTableExportService.new(params, self).export_single
end

#index ⇒ Object

Displays all dynamic tables with filters and pagination



10
11
12
13
14
# File 'app/controllers/dbdoc_engine/dbdoc_admin/db_design_dynamic_tables_controller.rb', line 10

def index
  @tables = DbdocEngine::DbDesignDynamicTableQueries.all_tables_ordered_by_group
  load_filtered_tables(params[:page])
  @total_table_count = DbdocEngine::DbDesignDynamicTableQueries.count_all_tables
end

#new ⇒ Object

Initializes a new table with associated columns



39
40
41
42
43
# File 'app/controllers/dbdoc_engine/dbdoc_admin/db_design_dynamic_tables_controller.rb', line 39

def new
  @table = DbDesignDynamicTable.new
  @table.db_design_dynamic_columns.build
  load_other_tables
end

#permanent_destroy ⇒ Object

Permanently deletes a table



67
68
69
70
# File 'app/controllers/dbdoc_engine/dbdoc_admin/db_design_dynamic_tables_controller.rb', line 67

def permanent_destroy
  handle_table_action(:permanent_destroy, @table.table_name, I18n.t("dbdoc.table_permanent_deletion"),
                      deleted_tables_dbdoc_admin_db_design_dynamic_tables_path)
end

#render_column_fields ⇒ Object

Renders additional column fields dynamically



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/dbdoc_engine/dbdoc_admin/db_design_dynamic_tables_controller.rb', line 79

def render_column_fields
  @index = params[:index] || Time.now.to_i
  @column = DbDesignDynamicColumn.new
  current_table_id = params[:table_id]
  @other_tables = if current_table_id
                    DbdocEngine::DbDesignDynamicTableQueries.find_all_except(current_table_id)
  else
                    DbdocEngine::DbDesignDynamicTableQueries.all_tables_with_columns_and_groups
  end

  render partial: "column_fields", locals: { f: fields_for_column(@column, @index),
                                            table: @table,
                                            other_tables: @other_tables }
end

#restore ⇒ Object

Restores a soft-deleted table



73
74
75
76
# File 'app/controllers/dbdoc_engine/dbdoc_admin/db_design_dynamic_tables_controller.rb', line 73

def restore
  handle_table_action(:restore, @table.table_name, I18n.t("dbdoc.table_restore"),
                      deleted_tables_dbdoc_admin_db_design_dynamic_tables_path)
end

#show_table_info ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/dbdoc_engine/dbdoc_admin/db_design_dynamic_tables_controller.rb', line 23

def show_table_info
  @table = DbdocEngine::DbDesignDynamicTableQueries.find_table_with_columns_and_group(params[:id])
  @total_columns_count = @table.db_design_dynamic_columns.size
  @columns = @table.db_design_dynamic_columns.order(is_primary_key: :desc, column_name: :asc).page(params[:page]).per(10)
  table_name = @table.table_name
  @recent_changelogs = DbdocEngine::DbDesignChangelog.where(
    (DbdocEngine::DbDesignChangelog.arel_table[:entity_type].eq("table")
      .and(DbdocEngine::DbDesignChangelog.arel_table[:entity_name].eq(table_name)))
    .or(
      DbdocEngine::DbDesignChangelog.arel_table[:entity_type].eq("column")
      .and(DbdocEngine::DbDesignChangelog.arel_table[:entity_name].matches("#{table_name}.%"))
    )
  ).order(change_timestamp: :desc).limit(10)
end

#update ⇒ Object

Updates an existing dynamic table using the service layer



56
57
58
# File 'app/controllers/dbdoc_engine/dbdoc_admin/db_design_dynamic_tables_controller.rb', line 56

def update
  DbDesignDynamicTableHandlerService.new(self, table_params, @table).update
end