Sha256: 640cda5fbbfd732cf4693ebab8193440d892434a6b31bb1159eb077278ff75bf

Contents?: true

Size: 1.55 KB

Versions: 14

Compression:

Stored size: 1.55 KB

Contents

# See Pagy::Backend API documentation: https://ddnexus.github.io/pagy/docs/api/backend
# frozen_string_literal: true

class Pagy
  # Define a few generic methods to paginate a collection out of the box,
  # or any collection by overriding pagy_get_items and/or pagy_get_vars in your controller
  # See also the extras if you need specialized methods to paginate Arrays or other collections
  module Backend
    private

    # Return Pagy object and paginated results
    def pagy(collection, **vars)
      vars[:count] ||= pagy_get_count(collection, vars)
      vars[:limit] ||= pagy_get_limit(vars)
      vars[:page]  ||= pagy_get_page(vars)
      pagy = Pagy.new(**vars)
      [pagy, pagy_get_items(collection, pagy)]
    end

    # Get the count from the collection
    def pagy_get_count(collection, vars)
      count_args = vars[:count_args] || DEFAULT[:count_args]
      (count     = collection.count(*count_args)).is_a?(Hash) ? count.size : count
    end

    # Sub-method called only by #pagy: here for easy customization of fetching by overriding
    # You may need to override this method for collections without offset|limit
    def pagy_get_items(collection, pagy)
      collection.offset(pagy.offset).limit(pagy.limit)
    end

    # Override for limit extra
    def pagy_get_limit(_vars)
      DEFAULT[:limit]
    end

    # Get the page integer from the params
    # Overridable by the jsonapi extra
    def pagy_get_page(vars, force_integer: true)
      page = params[vars[:page_param] || DEFAULT[:page_param]]
      force_integer ? (page || 1).to_i : page
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
pagy-9.3.1 lib/pagy/backend.rb
pagy-9.3.0 lib/pagy/backend.rb
pagy-9.2.2 lib/pagy/backend.rb
pagy-9.2.1 lib/pagy/backend.rb
pagy-9.2.0 lib/pagy/backend.rb
pagy-9.1.1 lib/pagy/backend.rb
pagy-9.1.0 lib/pagy/backend.rb
pagy-9.0.9 lib/pagy/backend.rb
pagy-9.0.8 lib/pagy/backend.rb
pagy-9.0.7 lib/pagy/backend.rb
pagy-9.0.6 lib/pagy/backend.rb
pagy-9.0.5 lib/pagy/backend.rb
pagy-9.0.4 lib/pagy/backend.rb
pagy-9.0.3 lib/pagy/backend.rb