Sha256: f2767cbde56def78cefc701c44d76627026419bce6fafcb3f0f66105ea2cbf74

Contents?: true

Size: 1.26 KB

Versions: 5

Compression:

Stored size: 1.26 KB

Contents

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

require 'pagy/countless'

class Pagy

  # used by the items extra
  COUNTLESS = true

  module Backend ; private         # the whole module is private so no problem with including it in a controller

    # Return Pagy object and items
    def pagy_countless(collection, vars={})
      pagy = Pagy::Countless.new(pagy_countless_get_vars(collection, vars))
      return 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)
      vars[:page] ||= params[ vars[:page_param] || VARS[:page_param] ]
      vars
    end

    # Sub-method called only by #pagy_countless: here for easy customization of record-extraction by overriding
    def pagy_countless_get_items(collection, pagy)
      # This should work with ActiveRecord, Sequel, Mongoid...
      items      = collection.offset(pagy.offset).limit(pagy.items + 1).to_a
      items_size = items.size
      items.pop if items_size == pagy.items + 1
      pagy.finalize(items_size)                  # finalize may adjust pagy.items, so must be used after checking the size
      items
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pagy-1.3.3 lib/pagy/extras/countless.rb
pagy-1.3.2 lib/pagy/extras/countless.rb
pagy-1.3.1 lib/pagy/extras/countless.rb
pagy-1.3 lib/pagy/extras/countless.rb
pagy-1.2.1 lib/pagy/extras/countless.rb