Fetching contributors…
Cannot retrieve contributors at this time
33 lines (25 sloc) 1.44 KB
# See Pagy::Backend API documentation:
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 =, vars))
return pagy, pagy_get_items(collection, pagy)
# Sub-method called only by #pagy: here for easy customization of variables by overriding
def pagy_get_vars(collection, vars)
count = collection.count(:all) # work with AR collections: other ORMs may need to change this
count = count.count if count.is_a?(Hash) # fix for the AR grouping count inconsistency (Hash instead of Integer)
# Return the merged variables to initialize the Pagy object
{ count: count, page: params[vars[:page_param]||VARS[:page_param]] }.merge!(vars)
# 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...