# frozen_string_literal: true module Lipstick module Helpers module SortableHelper include ActionView::Helpers def sortable_link(name, param_name = nil) param_name ||= name.parameterize.underscore.downcase direction = Lipstick::Sortable.direction(params[:direction]) opposite_direction = Lipstick::Sortable.opposite_direction(params[:direction]) if params[:sort_by] == param_name sortable_active_link(name, param_name, direction, opposite_direction) else link_to name, request.query_parameters .merge(sort_by: param_name, direction: direction) end end private def sortable_active_link(name, param_name, direction, opposite_direction) link_to request.query_parameters.merge(sort_by: param_name, direction: opposite_direction) do concat("#{name} ") concat( content_tag('div', class: 'glyphicon') do content_tag('div', class: "glyphicon-chevron-#{icon(direction)}") do end end ) end end def icon(direction) direction == 'asc' ? 'up' : 'down' end end end end