Sha256: 5e471d44c31a62ef7bb257856c8ffaf1427007d2af74989cd44a4f65ed62534b

Contents?: true

Size: 1.64 KB

Versions: 17

Compression:

Stored size: 1.64 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 = Backend::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 = Backend::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

17 entries across 17 versions & 1 rubygems

Version Path
katalyst-tables-2.3.1 app/models/concerns/katalyst/tables/collection/sorting.rb
katalyst-tables-2.3.0 app/models/concerns/katalyst/tables/collection/sorting.rb
katalyst-tables-2.2.12 app/models/concerns/katalyst/tables/collection/sorting.rb
katalyst-tables-2.2.11 app/models/concerns/katalyst/tables/collection/sorting.rb
katalyst-tables-2.2.10 app/models/concerns/katalyst/tables/collection/sorting.rb
katalyst-tables-2.2.9 app/models/concerns/katalyst/tables/collection/sorting.rb
katalyst-tables-2.2.8 app/models/concerns/katalyst/tables/collection/sorting.rb
katalyst-tables-2.2.7 app/models/concerns/katalyst/tables/collection/sorting.rb
katalyst-tables-2.2.6 app/models/concerns/katalyst/tables/collection/sorting.rb
katalyst-tables-2.2.5 app/models/concerns/katalyst/tables/collection/sorting.rb
katalyst-tables-2.2.4 app/models/concerns/katalyst/tables/collection/sorting.rb
katalyst-tables-2.2.3 app/models/concerns/katalyst/tables/collection/sorting.rb
katalyst-tables-2.2.2 app/models/concerns/katalyst/tables/collection/sorting.rb
katalyst-tables-2.2.1 app/models/concerns/katalyst/tables/collection/sorting.rb
katalyst-tables-2.2.0 app/models/concerns/katalyst/tables/collection/sorting.rb
katalyst-tables-2.1.3 app/models/concerns/katalyst/tables/collection/sorting.rb
katalyst-tables-2.1.2 app/models/concerns/katalyst/tables/collection/sorting.rb