Module: Dbviewer::ApplicationHelper

Defined in:
app/helpers/dbviewer/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#active_nav_class(controller_name, action_name = nil) ⇒ Object

Helper method to determine if current controller and action match



101
102
103
104
105
106
107
108
109
110
# File 'app/helpers/dbviewer/application_helper.rb', line 101

def active_nav_class(controller_name, action_name = nil)
  current_controller = params[:controller].split("/").last
  active = current_controller == controller_name

  if action_name.present?
    active = active && params[:action] == action_name
  end

  active ? "active" : ""
end

#code_block_bg_classObject

Helper method for code blocks background that adapts to dark mode



62
63
64
# File 'app/helpers/dbviewer/application_helper.rb', line 62

def code_block_bg_class
  "sql-code-block"
end

#column_type_icon(column_type) ⇒ Object

Get appropriate icon for column data type



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/helpers/dbviewer/application_helper.rb', line 81

def column_type_icon(column_type)
  case column_type.to_s.downcase
  when /int/, /serial/, /number/, /decimal/, /float/, /double/
    "bi-123"
  when /char/, /text/, /string/, /uuid/
    "bi-fonts"
  when /date/, /time/
    "bi-calendar"
  when /bool/
    "bi-toggle-on"
  when /json/, /jsonb/
    "bi-braces"
  when /array/
    "bi-list-ol"
  else
    "bi-file-earmark"
  end
end

#current_table?(table_name) ⇒ Boolean

Determine if the current table should be active in the sidebar

Returns:

  • (Boolean)


67
68
69
# File 'app/helpers/dbviewer/application_helper.rb', line 67

def current_table?(table_name)
  @table_name.present? && @table_name == table_name
end

#dashboard_nav_classObject

Helper for highlighting dashboard link



113
114
115
# File 'app/helpers/dbviewer/application_helper.rb', line 113

def dashboard_nav_class
  active_nav_class("home")
end

#erd_nav_classObject

Helper for highlighting ERD link



123
124
125
# File 'app/helpers/dbviewer/application_helper.rb', line 123

def erd_nav_class
  active_nav_class("entity_relationship_diagrams")
end

#format_cell_value(value) ⇒ Object



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
# File 'app/helpers/dbviewer/application_helper.rb', line 17

def format_cell_value(value)
  return "NULL" if value.nil?
  return value.to_s.truncate(100) unless value.is_a?(String)

  case value
  when /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/
    # ISO 8601 datetime
    begin
      Time.parse(value).strftime("%Y-%m-%d %H:%M:%S")
    rescue
      value.to_s.truncate(100)
    end
  when /\A\d{4}-\d{2}-\d{2}\z/
    # Date
    value
  when /\A{.+}\z/, /\A\[.+\]\z/
    # JSON
    begin
      JSON.pretty_generate(JSON.parse(value)).truncate(100)
    rescue
      value.to_s.truncate(100)
    end
  else
    value.to_s.truncate(100)
  end
end

#format_table_name(table_name) ⇒ Object

Format table name for display - truncate if too long



72
73
74
75
76
77
78
# File 'app/helpers/dbviewer/application_helper.rb', line 72

def format_table_name(table_name)
  if table_name.length > 20
    "#{table_name.first(17)}..."
  else
    table_name
  end
end

#get_database_managerObject

Helper to access the database manager



13
14
15
# File 'app/helpers/dbviewer/application_helper.rb', line 13

def get_database_manager
  @database_manager ||= ::Dbviewer::DatabaseManager.new
end

#has_timestamp_column?(table_name) ⇒ Boolean

Check if a table has a created_at column

Returns:

  • (Boolean)


4
5
6
7
8
9
10
# File 'app/helpers/dbviewer/application_helper.rb', line 4

def has_timestamp_column?(table_name)
  return false unless table_name.present?

  # Get the columns for the table directly using DatabaseManager
  columns = get_database_manager.table_columns(table_name)
  columns.any? { |col| col[:name] == "created_at" && [ :datetime, :timestamp ].include?(col[:type]) }
end

#logs_nav_classObject

Helper for highlighting SQL Logs link



128
129
130
# File 'app/helpers/dbviewer/application_helper.rb', line 128

def logs_nav_class
  active_nav_class("logs")
end

#next_sort_direction(column_name, current_order_by, current_direction) ⇒ Object

Determine the next sort direction based on the current one



143
144
145
146
147
148
149
# File 'app/helpers/dbviewer/application_helper.rb', line 143

def next_sort_direction(column_name, current_order_by, current_direction)
  if column_name == current_order_by && current_direction == "ASC"
    "DESC"
  else
    "ASC"
  end
end

#sort_icon(column_name, current_order_by, current_direction) ⇒ Object

Returns a sort icon based on the current sort direction



133
134
135
136
137
138
139
140
# File 'app/helpers/dbviewer/application_helper.rb', line 133

def sort_icon(column_name, current_order_by, current_direction)
  if column_name == current_order_by
    direction = current_direction == "ASC" ? "up" : "down"
    "<i class='bi bi-sort-#{direction}'></i>".html_safe
  else
    "<i class='bi bi-filter invisible sort-icon'></i>".html_safe
  end
end

#sortable_column_header(column_name, current_order_by, current_direction, table_name, current_page, per_page, column_filters) ⇒ Object

Generate a sortable column header link



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/helpers/dbviewer/application_helper.rb', line 152

def sortable_column_header(column_name, current_order_by, current_direction, table_name, current_page, per_page, column_filters)
  is_sorted = column_name == current_order_by
  sort_direction = next_sort_direction(column_name, current_order_by, current_direction)

  aria_sort = if is_sorted
    current_direction.downcase == "asc" ? "ascending" : "descending"
  else
    "none"
  end

  # Build parameters for the sort link
  sort_params = {
    order_by: column_name,
    order_direction: sort_direction,
    page: current_page,
    per_page: per_page,
    column_filters: column_filters
  }

  # Add creation filter parameters if they're in the controller
  if defined?(@creation_filter_start) && @creation_filter_start.present?
    sort_params[:creation_filter_start] = @creation_filter_start
  end

  if defined?(@creation_filter_end) && @creation_filter_end.present?
    sort_params[:creation_filter_end] = @creation_filter_end
  end

  link_to table_path(table_name, sort_params),
    class: "d-flex align-items-center text-decoration-none text-reset column-sort-link",
    title: "Sort by #{column_name} (#{sort_direction.downcase})",
    "aria-sort": aria_sort,
    role: "button",
    tabindex: "0" do
      (:span, column_name, class: "column-name") +
      (:span, sort_icon(column_name, current_order_by, current_direction), class: "sort-icon-container")
  end
end

#stat_card_bg_classObject

Returns the appropriate background class for stat cards that adapts to dark mode



57
58
59
# File 'app/helpers/dbviewer/application_helper.rb', line 57

def stat_card_bg_class
  "stat-card-bg"
end

#tables_nav_classObject

Helper for highlighting tables link



118
119
120
# File 'app/helpers/dbviewer/application_helper.rb', line 118

def tables_nav_class
  active_nav_class("tables")
end

#theme_toggle_iconObject

Returns the theme toggle icon based on the current theme



47
48
49
# File 'app/helpers/dbviewer/application_helper.rb', line 47

def theme_toggle_icon
  '<i class="bi bi-moon"></i><i class="bi bi-sun"></i>'.html_safe
end

#theme_toggle_labelObject

Returns the aria label for the theme toggle button



52
53
54
# File 'app/helpers/dbviewer/application_helper.rb', line 52

def theme_toggle_label
  "Toggle dark mode"
end