Sha256: 0a5a0a6c8c4918d67915e58e0e168ac432760de0b3bf34f2c4b69fe60aeab4c7

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

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

class Pagy
  # Defines a few generic methods to paginate an ORM 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         # the whole module is private so no problem with including it in a controller

    # Return Pagy object and items
    def pagy(collection, vars={})
      pagy = Pagy.new(pagy_get_vars(collection, vars))
      return pagy, pagy_get_items(collection, pagy)
    end

    # Sub-method called only by #pagy: here for easy customization of variables by overriding
    def pagy_get_vars(collection, vars)
      # Return the merged variables to initialize the Pagy object
      { count: collection.count(:all),        # works with AR, but may not work with other ORMs
        page:  params[vars[:page_param]||VARS[:page_param]] }.merge!(vars)
    end

    # Sub-method called only by #pagy: here for easy customization of record-extraction by overriding
    def pagy_get_items(collection, pagy)
      # This should work with ActiveRecord, Sequel, Mongoid...
      collection.offset(pagy.offset).limit(pagy.items)
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pagy-0.10.0 lib/pagy/backend.rb
pagy-0.9.2 lib/pagy/backend.rb
pagy-0.9.1 lib/pagy/backend.rb
pagy-0.9.0 lib/pagy/backend.rb