Module: Karafka::Web::Ui::Helpers::SortingHelper

Included in:
Base
Defined in:
lib/karafka/web/ui/helpers/sorting_helper.rb

Overview

Helpers for rendering sortable column links in data tables

Instance Method Summary collapse

Instance Method Details

Returns html link for sorting with arrow when attribute sort enabled.

Parameters:

  • name (String)

    link value

  • attribute (Symbol, nil) (defaults to: nil)

    sorting attribute or nil if we provide only symbol name

  • rev (Boolean) (defaults to: false)

    when set to true, arrows will be in the reverse position. This is used when the description in the link is reverse to data we sort. For example we have order on when processes were started and we display "x hours" ago but we sort on their age, meaning that it looks like it is the other way around. This flag allows us to reverse just he arrow making it look consistent with the presented data order

Returns:

  • (String)

    html link for sorting with arrow when attribute sort enabled



40
41
42
43
44
45
46
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
# File 'lib/karafka/web/ui/helpers/sorting_helper.rb', line 40

def sort_link(name, attribute = nil, rev: false)
  unless attribute
    attribute = name

    if SORT_NAMES[attribute]
      name = SORT_NAMES[attribute]
    else
      name = attribute.to_s.tr("_", " ").tr("?", "")
      # Always capitalize the name
      name = name.split.map(&:capitalize).join(" ")
    end
  end

  arrow_both = "⇕"
  arrow_down = "▾"
  arrow_up = "▴"

  desc = "#{attribute} desc"
  asc = "#{attribute} asc"
  path = current_path(sort: desc)
  full_name = "#{name} #{arrow_both}"

  if params.current_sort == desc
    path = current_path(sort: asc)
    full_name = "#{name} #{rev ? arrow_up : arrow_down}"
  end

  if params.current_sort == asc
    path = current_path(sort: desc)
    full_name = "#{name} #{rev ? arrow_down : arrow_up}"
  end

  "<a class=\"sort\" href=\"#{path}\">#{full_name}</a>"
end