# 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(glyphicon(direction)) end end def glyphicon(direction) content_tag('div', class: 'glyphicon') do content_tag('div', nil, class: "glyphicon-chevron-#{icon(direction)}") end end def icon(direction) direction == 'asc' ? 'up' : 'down' end end end end