Sha256: 1d6565431a2196e44fcde24b0c9d80853439744e0860562a5d4a339a3043f6f6

Contents?: true

Size: 1.31 KB

Versions: 16

Compression:

Stored size: 1.31 KB

Contents

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

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))
      [ 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)
      pagy_set_items_from_params(vars) if defined?(UseItemsExtra)
      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

16 entries across 16 versions & 2 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/pagy-4.11.0/lib/pagy/backend.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/pagy-4.11.0/lib/pagy/backend.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/pagy-4.11.0/lib/pagy/backend.rb
pagy-4.11.0 lib/pagy/backend.rb
pagy-4.10.2 lib/pagy/backend.rb
pagy-4.10.1 lib/pagy/backend.rb
pagy-4.10.0 lib/pagy/backend.rb
pagy-4.9.0 lib/pagy/backend.rb
pagy-4.8.1 lib/pagy/backend.rb
pagy-4.8.0 lib/pagy/backend.rb
pagy-4.7.1 lib/pagy/backend.rb
pagy-4.7.0 lib/pagy/backend.rb
pagy-4.6.0 lib/pagy/backend.rb
pagy-4.5.1 lib/pagy/backend.rb
pagy-4.5.0 lib/pagy/backend.rb
pagy-4.4.0 lib/pagy/backend.rb