Module: Katalyst::Tables::Collection::Sorting
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/concerns/katalyst/tables/collection/sorting.rb
Overview
Adds sorting support to a collection.
Sorting will be applied if the collection is configured with a default sorting configuration by either specifying ‘config.sorting = “column direction”` or passing `sorting: “column direction”` to the initializer.
If ‘sort` is present in params it will override the default sorting.
Defined Under Namespace
Modules: SortParams Classes: Sort
Constant Summary collapse
- DIRECTIONS =
%w[asc desc].freeze
Instance Method Summary collapse
- #default_sort? ⇒ Boolean
- #initialize(sorting: config.sorting) ⇒ Object
-
#sort=(value) ⇒ Object
Set the current sort behaviour of the collection.
-
#sort_status(column) ⇒ String
Returns the current sort behaviour of the given column, for use as a column heading class in the table view.
-
#sortable?(column = nil) ⇒ true, false
Returns true if the collection supports sorting on the given column.
-
#toggle_sort(column) ⇒ String
Calculates the sort parameter to apply when the given column is toggled.
Instance Method Details
#default_sort? ⇒ Boolean
64 65 66 |
# File 'app/models/concerns/katalyst/tables/collection/sorting.rb', line 64 def default_sort? sort == @default_sort end |
#initialize(sorting: config.sorting) ⇒ Object
58 59 60 61 62 |
# File 'app/models/concerns/katalyst/tables/collection/sorting.rb', line 58 def initialize(sorting: config.sorting, **) @default_sort = sorting.to_param if sorting.present? super(sort: @default_sort, **) # set default sort based on config end |
#sort=(value) ⇒ Object
Set the current sort behaviour of the collection.
85 86 87 |
# File 'app/models/concerns/katalyst/tables/collection/sorting.rb', line 85 def sort=(value) super(value.to_param) if @default_sort end |
#sort_status(column) ⇒ String
Returns the current sort behaviour of the given column, for use as a column heading class in the table view.
94 95 96 97 |
# File 'app/models/concerns/katalyst/tables/collection/sorting.rb', line 94 def sort_status(column) current, direction = sort.to_h.values_at(:column, :direction) direction if column.to_s == current end |
#sortable?(column = nil) ⇒ true, false
Returns true if the collection supports sorting on the given column. A column supports sorting if it is a database column or if the collection responds to ‘order_by_#column(direction)`.
74 75 76 77 78 79 80 |
# File 'app/models/concerns/katalyst/tables/collection/sorting.rb', line 74 def sortable?(column = nil) if column.nil? @default_sort.present? else items.respond_to?(:"order_by_#{column}") || items.model.has_attribute?(column.to_s) end end |
#toggle_sort(column) ⇒ String
Calculates the sort parameter to apply when the given column is toggled.
103 104 105 106 107 108 109 |
# File 'app/models/concerns/katalyst/tables/collection/sorting.rb', line 103 def toggle_sort(column) current, direction = sort.to_h.values_at(:column, :direction) return "#{column} asc" unless column.to_s == current direction == "asc" ? "#{column} desc" : "#{column} asc" end |