Sha256: 868a9890ba71592589a4e28504d6d318651db7a7cfa74e3d96e69b3670f7743c

Contents?: true

Size: 1.25 KB

Versions: 17

Compression:

Stored size: 1.25 KB

Contents

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

require_relative '../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 records
    def pagy_countless(collection, **vars)
      vars[:limit] ||= pagy_get_limit(vars)
      vars[:page]  ||= pagy_get_page(vars)
      pagy = Countless.new(**vars)
      [pagy, pagy_countless_get_items(collection, pagy)]
    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.limit) if pagy.vars[:countless_minimal]

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

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
pagy-9.3.4 lib/pagy/extras/countless.rb
pagy-9.3.3 lib/pagy/extras/countless.rb
pagy-9.3.2 lib/pagy/extras/countless.rb
pagy-9.3.1 lib/pagy/extras/countless.rb
pagy-9.3.0 lib/pagy/extras/countless.rb
pagy-9.2.2 lib/pagy/extras/countless.rb
pagy-9.2.1 lib/pagy/extras/countless.rb
pagy-9.2.0 lib/pagy/extras/countless.rb
pagy-9.1.1 lib/pagy/extras/countless.rb
pagy-9.1.0 lib/pagy/extras/countless.rb
pagy-9.0.9 lib/pagy/extras/countless.rb
pagy-9.0.8 lib/pagy/extras/countless.rb
pagy-9.0.7 lib/pagy/extras/countless.rb
pagy-9.0.6 lib/pagy/extras/countless.rb
pagy-9.0.5 lib/pagy/extras/countless.rb
pagy-9.0.4 lib/pagy/extras/countless.rb
pagy-9.0.3 lib/pagy/extras/countless.rb