saucy/app/helpers/members_helper.rb
2022-06-25 13:48:46 +01:00

25 lines
543 B
Ruby

module MembersHelper
def link_to_current_with_sort text, default_sort
current_sort = stringify(sort_params)
pp [default_sort, current_sort]
if default_sort == current_sort
link_to text, members_path(sort: invert_sort_order(current_sort))
else
link_to text, members_path(sort: default_sort)
end
end
private
def stringify(sort)
"#{sort.keys.first.to_s}.#{sort.values.first.to_s}"
end
def invert_sort_order(sort)
sort.sub(/\.(asc|desc)$/) { |x| x == '.asc' ? '.desc' : '.asc' }
end
end