Sha256: e68fc150dd60f790dd0be7f040419d340a1128002f47ff7c1149ac4e7180d33a

Contents?: true

Size: 1.63 KB

Versions: 4

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module Katalyst
  module Tables
    module Collection
      # Adds sorting support to a collection.
      #
      # Sorting will be applied if the collection is configured with a default
      # sorting configuration by either specifying
      # `config.sorting = "column direction"` or passing
      # `sorting: "column direction"` to the initializer.
      #
      # If `sort` is present in params it will override the default sorting.
      module Sorting
        extend ActiveSupport::Concern

        included do
          config_accessor :sorting
          attr_accessor :sorting

          attribute :sort, :string
        end

        def initialize(sorting: config.sorting, **options)
          @sorting = SortForm.parse(sorting) if sorting

          super(sort: sorting, **options) # set default sort based on config
        end

        def sort=(value)
          return unless @sorting

          # update internal proxy
          @sorting = SortForm.parse(value, default: attribute_was(:sort))

          # update attribute based on normalized value
          super(@sorting.to_param)
        end

        class Sort # :nodoc:
          include Backend

          def initialize(app)
            @app = app
          end

          def call(collection)
            @collection                            = @app.call(collection)
            @collection.sorting, @collection.items = @collection.sorting.apply(@collection.items) if @collection.sorting
            @collection
          end

          # pagy shim
          def params
            @collection.attributes
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
katalyst-tables-2.6.0 app/models/concerns/katalyst/tables/collection/sorting.rb
katalyst-tables-2.6.0.beta app/models/concerns/katalyst/tables/collection/sorting.rb
katalyst-tables-2.5.0 app/models/concerns/katalyst/tables/collection/sorting.rb
katalyst-tables-2.4.0 app/models/concerns/katalyst/tables/collection/sorting.rb