Class: PgSqlTriggers::TablesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/pg_sql_triggers/tables_controller.rb

Instance Method Summary collapse

Methods included from PermissionsHelper

#can?, #can_apply_triggers?, #can_drop_triggers?, #can_enable_disable_triggers?, #can_execute_sql_operations?, #can_generate_triggers?, #can_view_triggers?

Methods included from ErrorHandling

#format_error_for_flash, #handle_kill_switch_error, #handle_standard_error, #rescue_pg_sql_triggers_error

Methods included from PermissionChecking

#can_apply_triggers?, #can_drop_triggers?, #can_enable_disable_triggers?, #can_execute_sql_operations?, #can_generate_triggers?, #can_view_triggers?, #check_admin_permission, #check_operator_permission, #check_viewer_permission, #current_actor, #current_user_id, #current_user_type

Methods included from KillSwitchProtection

#check_kill_switch, #current_environment, #expected_confirmation_text, #kill_switch_active?, #require_kill_switch_override

Instance Method Details

#indexObject



7
8
9
10
11
12
13
14
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
# File 'app/controllers/pg_sql_triggers/tables_controller.rb', line 7

def index
  all_tables = PgSqlTriggers::DatabaseIntrospection.new.tables_with_triggers

  # Calculate statistics
  @tables_with_trigger_count = all_tables.count { |t| t[:trigger_count].positive? }
  @tables_without_trigger_count = all_tables.count { |t| t[:trigger_count].zero? }
  @total_tables_count = all_tables.count

  # Filter based on parameter
  @filter = params[:filter] || "with_triggers"
  filtered_tables = case @filter
                    when "with_triggers"
                      all_tables.select { |t| t[:trigger_count].positive? }
                    when "without_triggers"
                      all_tables.select { |t| t[:trigger_count].zero? }
                    else # 'all'
                      all_tables
                    end

  @search_query = params[:q].to_s.strip
  if @search_query.present?
    needle = @search_query.downcase
    filtered_tables = filtered_tables.select do |row|
      row[:table_name].to_s.downcase.include?(needle)
    end
  end

  @total_tables = filtered_tables.count

  # Pagination
  @per_page = (params[:per_page] || 20).to_i
  @per_page = [@per_page, 100].min # Cap at 100
  @page = (params[:page] || 1).to_i
  @total_pages = @total_tables.positive? ? (@total_tables.to_f / @per_page).ceil : 1
  @page = @page.clamp(1, @total_pages) # Ensure page is within valid range

  offset = (@page - 1) * @per_page
  @tables_with_triggers = filtered_tables.slice(offset, @per_page) || []
end

#showObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/pg_sql_triggers/tables_controller.rb', line 47

def show
  @table_info = PgSqlTriggers::DatabaseIntrospection.new.table_triggers(params[:id])
  @columns = PgSqlTriggers::DatabaseIntrospection.new.table_columns(params[:id])

  respond_to do |format|
    format.html
    format.json do
      render json: {
        table_name: @table_info[:table_name],
        registry_triggers: @table_info[:registry_triggers].map do |t|
          {
            id: t.id,
            trigger_name: t.trigger_name,
            function_name: if t.definition.present?
                             begin
                               JSON.parse(t.definition)
                             rescue StandardError
                               {}
                             end["function_name"]
                           end,
            enabled: t.enabled,
            version: t.version,
            source: t.source
          }
        end,
        database_triggers: @table_info[:database_triggers]
      }
    end
  end
end