Sha256: 1f7ec185ac41605978f51b0fb6d8d5e567ed5dbd4b5675a9c079b2dad7a0fd1a

Contents?: true

Size: 1.49 KB

Versions: 36

Compression:

Stored size: 1.49 KB

Contents

# See the Pagy documentation: https://ddnexus.github.io/pagy/extras/countless
# frozen_string_literal: true

require 'pagy/countless'

class Pagy # :nodoc:
  DEFAULT[:countless_minimal] = false

  # Paginate without the need of any count, saving one query per rendering
  module CountlessExtra
    private

    # Return Pagy object and items
    def pagy_countless(collection, vars = {})
      pagy = Countless.new(pagy_countless_get_vars(collection, vars))
      [pagy, pagy_countless_get_items(collection, pagy)]
    end

    # Sub-method called only by #pagy_countless: here for easy customization of variables by overriding
    def pagy_countless_get_vars(_collection, vars)
      pagy_set_items_from_params(vars) if defined?(ItemsExtra)
      vars[:page] ||= params[vars[:page_param] || DEFAULT[:page_param]]
      vars
    end

    # Sub-method called only by #pagy_countless: here for easy customization of record-extraction by overriding
    # You may need to override this method for collections without offset|limit
    def pagy_countless_get_items(collection, pagy)
      return collection.offset(pagy.offset).limit(pagy.items) if pagy.vars[:countless_minimal]

      fetched = collection.offset(pagy.offset).limit(pagy.items + 1).to_a # eager load items + 1
      pagy.finalize(fetched.size)                                         # finalize the pagy object
      fetched[0, pagy.items]                                              # ignore eventual extra item
    end
  end
  Backend.prepend CountlessExtra
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
pagy-6.0.0 lib/pagy/extras/countless.rb
pagy-5.10.1 lib/pagy/extras/countless.rb
pagy-5.10.0 lib/pagy/extras/countless.rb
pagy-5.9.3 lib/pagy/extras/countless.rb
pagy-5.9.2 lib/pagy/extras/countless.rb
pagy-5.9.1 lib/pagy/extras/countless.rb
pagy-5.9.0 lib/pagy/extras/countless.rb
pagy-5.8.1 lib/pagy/extras/countless.rb
pagy-5.8.0 lib/pagy/extras/countless.rb
pagy-5.7.6 lib/pagy/extras/countless.rb
pagy-5.7.5 lib/pagy/extras/countless.rb
pagy-5.7.4 lib/pagy/extras/countless.rb
pagy-5.7.3 lib/pagy/extras/countless.rb
pagy-5.7.2 lib/pagy/extras/countless.rb
pagy-5.7.1 lib/pagy/extras/countless.rb
pagy-5.7.0 lib/pagy/extras/countless.rb
pagy-5.6.10 lib/pagy/extras/countless.rb
pagy-5.6.9 lib/pagy/extras/countless.rb
pagy-5.6.8 lib/pagy/extras/countless.rb
pagy-5.6.7 lib/pagy/extras/countless.rb