saucy/app/helpers/members_helper.rb

26 lines
609 B
Ruby

module MembersHelper
def link_to_current_with_sort text, default_sort
current_sort = stringify(sort_params)
current_params = request.params.except(:action, :controller)
if default_sort == current_sort
current_params.merge!(sort: invert_sort_order(current_sort))
else
current_params.merge!(sort: default_sort)
end
link_to text, members_path(current_params)
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