extract Instructeurs::FacetTableHeaderComponent wrapping sort logic

This commit is contained in:
mfo 2024-07-22 09:37:15 +02:00
parent 31255b69cc
commit 743ff8c8a9
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
4 changed files with 51 additions and 19 deletions

View file

@ -0,0 +1,40 @@
class Instructeurs::ColumnTableHeaderComponent < ApplicationComponent
attr_reader :procedure_presentation, :facet
# maybe extract a FacetSorter class?
def initialize(procedure_presentation:, facet:)
@procedure_presentation = procedure_presentation
@facet = facet
end
def sorted_by_current_facet?
procedure_presentation.sort['table'] == facet.table &&
procedure_presentation.sort['column'] == facet.column
end
def sorted_ascending?
current_sort_order == 'asc'
end
def sorted_descending?
current_sort_order == 'desc'
end
def aria_sort
if sorted_by_current_facet?
if sorted_ascending?
{ "aria-sort": "ascending" }
elsif sorted_descending?
{ "aria-sort": "descending" }
end
else
{}
end
end
private
def current_sort_order
procedure_presentation.sort['order']
end
end

View file

@ -0,0 +1,9 @@
%th{ aria_sort, scope: "col", class: facet.classname }
= link_to update_sort_instructeur_procedure_path(@procedure_presentation.procedure, table: facet.table, column: facet.column, order: @procedure_presentation.opposite_order_for(facet.table, facet.column)) do
- if sorted_by_current_facet?
- if sorted_ascending?
#{facet.label} ↑
- else
#{facet.label} ↓
- else
#{facet.label}

View file

@ -146,23 +146,6 @@ class ProcedurePresentation < ApplicationRecord
slice(:filters, :sort, :displayed_fields)
end
def sorted_by?(facet)
sort['table'] == facet.table &&
sort['column'] == facet.column
end
def aria_sort(order, field)
if sorted_by?(field)
if order == 'asc'
{ "aria-sort": "ascending" }
elsif order == 'desc'
{ "aria-sort": "descending" }
end
else
{}
end
end
private
def sorted_ids(dossiers, count)

View file

@ -98,8 +98,8 @@
%th.text-center
%input{ type: "checkbox", disabled: @disable_checkbox_all, checked: @disable_checkbox_all, data: { action: "batch-operation#onCheckAll" }, id: dom_id(BatchOperation.new, :checkbox_all), aria: { label: t('views.instructeurs.dossiers.select_all') } }
- @procedure_presentation.displayed_fields_for_headers.each do |field|
= render partial: "header_field", locals: { field: field }
- @procedure_presentation.displayed_fields_for_headers.each do |facet|
= render Instructeurs::ColumnTableHeaderComponent.new(procedure_presentation: @procedure_presentation, facet:)
%th.follow-col
Actions