Sha256: 79f227be12c3f6e797229862b1cf544fed5ac344125feb11efd831b7d043b96c

Contents?: true

Size: 1.92 KB

Versions: 14

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

module Katalyst
  module Tables
    module Collection
      # Adds pagination support for a collection.
      #
      # Pagination will be applied if the collection is configured to paginate
      # by either specifying `config.paginate = true` or passing
      # `paginate: true` to the initializer.
      #
      # If the value given to `paginate` is a hash, it will be passed to the
      # `pagy` gem as options.
      #
      # If `page` is present in params it will be passed to pagy.
      module Pagination
        extend ActiveSupport::Concern

        included do
          attr_accessor :pagination

          attribute :page, :integer, default: 1, filter: false

          config_accessor :paginate
        end

        def initialize(paginate: config.paginate, **)
          super(**)

          @paginate = paginate.freeze
        end

        def paginate?
          !!@paginate
        end

        def paginate_options
          opts = @paginate.is_a?(Hash) ? @paginate : {}
          opts = opts.dup
          opts[:anchor_string] ||= anchor_string
          opts
        end

        def anchor_string
          "data-turbo-action=\"replace\""
        end

        class Paginate # :nodoc:
          # Pagy is not a required gem unless you're using pagination
          # Expect to see NoMethodError failures if pagy is not available
          "Pagy::Backend".safe_constantize&.tap { |pagy| include(pagy) }

          def initialize(app)
            @app = app
          end

          def call(collection)
            @collection = @app.call(collection)
            if collection.paginate?
              @collection.pagination, @collection.items = pagy(@collection.items, collection.paginate_options.dup)
            end
            @collection
          end

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

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
katalyst-tables-3.5.1 app/models/concerns/katalyst/tables/collection/pagination.rb
katalyst-tables-3.5.0 app/models/concerns/katalyst/tables/collection/pagination.rb
katalyst-tables-3.4.6 app/models/concerns/katalyst/tables/collection/pagination.rb
katalyst-tables-3.4.5 app/models/concerns/katalyst/tables/collection/pagination.rb
katalyst-tables-3.4.4 app/models/concerns/katalyst/tables/collection/pagination.rb
katalyst-tables-3.4.3 app/models/concerns/katalyst/tables/collection/pagination.rb
katalyst-tables-3.4.2 app/models/concerns/katalyst/tables/collection/pagination.rb
katalyst-tables-3.4.1 app/models/concerns/katalyst/tables/collection/pagination.rb
katalyst-tables-3.4.0 app/models/concerns/katalyst/tables/collection/pagination.rb
katalyst-tables-3.3.4 app/models/concerns/katalyst/tables/collection/pagination.rb
katalyst-tables-3.3.3 app/models/concerns/katalyst/tables/collection/pagination.rb
katalyst-tables-3.3.2 app/models/concerns/katalyst/tables/collection/pagination.rb
katalyst-tables-3.3.1 app/models/concerns/katalyst/tables/collection/pagination.rb
katalyst-tables-3.3.0 app/models/concerns/katalyst/tables/collection/pagination.rb