Sha256: b2e192de6ef8852081920fbbe50df717a1b0f0b0b5cb674f2494a8d6dff384c9

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module Katalyst
  module Tables
    module Frontend
      module Helper # :nodoc:
        extend ActiveSupport::Concern

        def initialize(**options)
          super()

          options(**options)
        end

        # Add HTML options to the current component.
        def options(html: {}, **options)
          @html_options = options.slice(:id, :aria, :class, :data).merge(html)
          @html_options.stringify_keys!
        end

        # Generates a url for applying/toggling sort for the given column.
        #
        # @param sort [String, nil] sort parameter to apply, or nil to remove sorting
        # @return [String] URL for toggling column sorting
        def sort_url_for(sort: nil)
          # Implementation inspired by pagy's `pagy_url_for` helper.
          # Preserve any existing GET parameters
          # CAUTION: these parameters are not sanitised
          params = if sort
                     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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
katalyst-tables-2.0.0 lib/katalyst/tables/frontend/helper.rb