Class: Katalyst::Tables::Sortable::SortableHeaderComponent

Inherits:
ViewComponent::Base
  • Object
show all
Includes:
HtmlAttributes
Defined in:
app/components/concerns/katalyst/tables/sortable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection:, cell:) ⇒ SortableHeaderComponent

Returns a new instance of SortableHeaderComponent.



32
33
34
35
36
37
# File 'app/components/concerns/katalyst/tables/sortable.rb', line 32

def initialize(collection:, cell:, **)
  super(**)

  @collection = collection
  @cell = cell
end

Instance Attribute Details

#cellObject (readonly)

Returns the value of attribute cell.



28
29
30
# File 'app/components/concerns/katalyst/tables/sortable.rb', line 28

def cell
  @cell
end

#collectionObject (readonly)

Returns the value of attribute collection.



28
29
30
# File 'app/components/concerns/katalyst/tables/sortable.rb', line 28

def collection
  @collection
end

Instance Method Details

#callObject



39
40
41
# File 'app/components/concerns/katalyst/tables/sortable.rb', line 39

def call
  link_to(content, sort_url, **html_attributes)
end

#default_html_attributesObject



60
61
62
# File 'app/components/concerns/katalyst/tables/sortable.rb', line 60

def default_html_attributes
  { data: { turbo_action: "replace" } }
end

#sort_urlObject

Generates a url for applying/toggling sort for the given column.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/components/concerns/katalyst/tables/sortable.rb', line 44

def sort_url
  # rubocop:disable Metrics/AbcSize
  # Implementation inspired by pagy's `pagy_url_for` helper.
  # Preserve any existing GET parameters
  # CAUTION: these parameters are not sanitised
  sort = column && collection.toggle_sort(column)
  params = if sort && !sort.eql?(collection.default_sort)
             request.GET.merge("sort" => sort).except("page")
           else
             request.GET.except("page", "sort")
           end
  query_string = params.empty? ? "" : "?#{Rack::Utils.build_nested_query(params)}"

  "#{request.path}#{query_string}"
end