saucy/app/helpers/members_helper.rb

26 lines
609 B
Ruby
Raw Normal View History

2022-06-25 12:48:46 +00:00
module MembersHelper
def link_to_current_with_sort text, default_sort
current_sort = stringify(sort_params)
2023-09-24 10:28:58 +00:00
current_params = request.params.except(:action, :controller)
2022-06-25 12:48:46 +00:00
if default_sort == current_sort
2023-09-24 10:28:58 +00:00
current_params.merge!(sort: invert_sort_order(current_sort))
2022-06-25 12:48:46 +00:00
else
2023-09-24 10:28:58 +00:00
current_params.merge!(sort: default_sort)
2022-06-25 12:48:46 +00:00
end
2023-09-24 10:28:58 +00:00
link_to text, members_path(current_params)
2022-06-25 12:48:46 +00:00
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