Sha256: a9514a435507d6fc386f6971a6a86544f305b9038997f787efc68e012b9de823

Contents?: true

Size: 1.11 KB

Versions: 24

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

module Katalyst
  module Tables
    # Extension to add sorting support to a collection.
    # Assumes collection and sorting are available in the current scope.
    module Sortable
      extend ActiveSupport::Concern

      # Returns true when the given attribute is sortable.
      def sortable?(attribute)
        sorting&.supports?(collection, attribute)
      end

      # Generates a url for applying/toggling sort for the given column.
      def sort_url(attribute) # rubocop:disable Metrics/AbcSize
        # Implementation inspired by pagy's `pagy_url_for` helper.
        # Preserve any existing GET parameters
        # CAUTION: these parameters are not sanitised
        sort = attribute && sorting.toggle(attribute)
        params = if sort && !sort.eql?(sorting.default)
                   request.GET.merge("sort" => sort).except("page")
                 else
                   request.GET.except("page", "sort")
                 end
        query_string = params.empty? ? "" : "?#{Rack::Utils.build_nested_query(params)}"

        "#{request.path}#{query_string}"
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
katalyst-tables-3.0.0.beta1 app/components/concerns/katalyst/tables/sortable.rb
katalyst-tables-2.6.0 app/components/concerns/katalyst/tables/sortable.rb
katalyst-tables-2.6.0.beta app/components/concerns/katalyst/tables/sortable.rb
katalyst-tables-2.5.0 app/components/concerns/katalyst/tables/sortable.rb
katalyst-tables-2.4.0 app/components/concerns/katalyst/tables/sortable.rb
katalyst-tables-2.3.1 app/components/concerns/katalyst/tables/sortable.rb
katalyst-tables-2.3.0 app/components/concerns/katalyst/tables/sortable.rb
katalyst-tables-2.2.12 app/components/concerns/katalyst/tables/sortable.rb
katalyst-tables-2.2.11 app/components/concerns/katalyst/tables/sortable.rb
katalyst-tables-2.2.10 app/components/concerns/katalyst/tables/sortable.rb
katalyst-tables-2.2.9 app/components/concerns/katalyst/tables/sortable.rb
katalyst-tables-2.2.8 app/components/concerns/katalyst/tables/sortable.rb
katalyst-tables-2.2.7 app/components/concerns/katalyst/tables/sortable.rb
katalyst-tables-2.2.6 app/components/concerns/katalyst/tables/sortable.rb
katalyst-tables-2.2.5 app/components/concerns/katalyst/tables/sortable.rb
katalyst-tables-2.2.4 app/components/concerns/katalyst/tables/sortable.rb
katalyst-tables-2.2.3 app/components/concerns/katalyst/tables/sortable.rb
katalyst-tables-2.2.2 app/components/concerns/katalyst/tables/sortable.rb
katalyst-tables-2.2.1 app/components/concerns/katalyst/tables/sortable.rb
katalyst-tables-2.2.0 app/components/concerns/katalyst/tables/sortable.rb