Module: Dbviewer::SortingHelper

Included in:
ApplicationHelper
Defined in:
app/helpers/dbviewer/sorting_helper.rb

Instance Method Summary collapse

Instance Method Details

#next_sort_direction(column_name, current_order_by, current_direction) ⇒ Object

Determine the next sort direction based on the current one



14
15
16
17
18
19
20
# File 'app/helpers/dbviewer/sorting_helper.rb', line 14

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



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

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



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/helpers/dbviewer/sorting_helper.rb', line 23

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

  # Use common_params helper to build parameters
  sort_params = common_params(order_by: column_name, order_direction: sort_direction)

  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