Sha256: 9cd691a2cda5c30450ffd09b21cd5b8c0682298933fe8abf81e4eaa6f68ad899

Contents?: true

Size: 1.13 KB

Versions: 25

Compression:

Stored size: 1.13 KB

Contents

module Trestle
  module SortHelper
    def sort_link(text, field, options={})
      sort_link = SortLink.new(field, persistent_params, options)
      link_to text, sort_link.params, class: sort_link.classes
    end

    class SortLink
      attr_reader :field

      def initialize(field, params, options)
        @field, @params, @options = field, params, options
      end

      def active?
        @params[:sort] == field.to_s ||
          (@options[:default] && !@params.key?(:sort))
      end

      def params
        @params.merge(sort: field, order: order)
      end

      def order
        if active?
          reverse_order(current_order)
        else
          default_order
        end
      end

      def current_order
        @params[:order] || default_order
      end

      def default_order
        @options.fetch(:default_order, "asc").to_s
      end

      def classes
        classes = ["sort"]

        if active?
          classes << "sort-#{current_order}"
          classes << "active"
        end

        classes
      end

      def reverse_order(order)
        order == "asc" ? "desc" : "asc"
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
trestle-0.10.0 app/helpers/trestle/sort_helper.rb
trestle-0.10.0.pre2 app/helpers/trestle/sort_helper.rb
trestle-0.10.0.pre app/helpers/trestle/sort_helper.rb
trestle-0.9.8 app/helpers/trestle/sort_helper.rb
trestle-0.9.7 app/helpers/trestle/sort_helper.rb
trestle-0.9.6 app/helpers/trestle/sort_helper.rb
trestle-0.9.5 app/helpers/trestle/sort_helper.rb
trestle-0.9.4 app/helpers/trestle/sort_helper.rb
trestle-0.9.3 app/helpers/trestle/sort_helper.rb
trestle-0.9.2 app/helpers/trestle/sort_helper.rb
trestle-0.9.1 app/helpers/trestle/sort_helper.rb
trestle-0.9.0 app/helpers/trestle/sort_helper.rb
trestle-0.8.13 app/helpers/trestle/sort_helper.rb
trestle-0.8.12 app/helpers/trestle/sort_helper.rb
trestle-0.8.11 app/helpers/trestle/sort_helper.rb
trestle-0.8.10 app/helpers/trestle/sort_helper.rb
trestle-0.8.9 app/helpers/trestle/sort_helper.rb
trestle-0.8.8 app/helpers/trestle/sort_helper.rb
trestle-0.8.7 app/helpers/trestle/sort_helper.rb
trestle-0.8.6 app/helpers/trestle/sort_helper.rb