Sha256: bf3b2bb2bba2f2d8971816dd0d4cf983d929caa383364504d4b9ae4ecae4fddf

Contents?: true

Size: 1.22 KB

Versions: 5

Compression:

Stored size: 1.22 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)
      vars[:count] ||= (c = collection.count(:all)).is_a?(Hash) ? c.size : c
      vars[:page]  ||= params[ vars[:page_param] || VARS[:page_param] ]
      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

5 entries across 5 versions & 1 rubygems

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