Sha256: e4c621c4a9c29f5148f97849df0f255660bafb6e0d1e186993032a67a456e5a9

Contents?: true

Size: 1.28 KB

Versions: 8

Compression:

Stored size: 1.28 KB

Contents

# 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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
aaf-lipstick-4.10.0 lib/lipstick/helpers/sortable_helper.rb
aaf-lipstick-4.9.2 lib/lipstick/helpers/sortable_helper.rb
aaf-lipstick-4.9.1 lib/lipstick/helpers/sortable_helper.rb
aaf-lipstick-4.9.0 lib/lipstick/helpers/sortable_helper.rb
aaf-lipstick-4.8.0 lib/lipstick/helpers/sortable_helper.rb
aaf-lipstick-4.7.0 lib/lipstick/helpers/sortable_helper.rb
aaf-lipstick-4.6.0 lib/lipstick/helpers/sortable_helper.rb
aaf-lipstick-4.5.1 lib/lipstick/helpers/sortable_helper.rb