From 99081ee069bf537f6ca1921dd4f4ddc626749e7d Mon Sep 17 00:00:00 2001 From: Domizio Demichelis Date: Sat, 23 Jun 2018 20:22:38 +0200 Subject: [PATCH] small performance improvement for #51 and updated doc --- docs/api/backend.md | 31 ++++--------------------------- lib/pagy/backend.rb | 8 ++++---- 2 files changed, 8 insertions(+), 31 deletions(-) diff --git a/docs/api/backend.md b/docs/api/backend.md index 734ce1714..e2d362950 100644 --- a/docs/api/backend.md +++ b/docs/api/backend.md @@ -5,11 +5,11 @@ title: Pagy::Backend This module _(see [source](https://github.com/ddnexus/pagy/blob/master/lib/pagy/backend.rb))_ provides a _generic_ pagination method (`pagy`) that should work with most ORM collection (e.g. `ActiveRecord`, `Sequel`, `Mongoid`, ... collections). -For overriding convenience, the `pagy` method calls two sub-methods that you may want to override in order to customize it for any type of collection (e.g. different ORM types, Array, elasticsearch results, etc.). +For overriding convenience, the `pagy` method calls two sub-methods that you may need to override in order to customize it for any type of collection (e.g. different ORM types, Array, elasticsearch results, etc.). -However, keep in mind that the whole module is basically providing a single functionality: getting a Pagy instance and the paginated items. You could re-write the whole module as one single and simple method specific to your need. (see [Writing your own Pagy methods](#writing-your-own-pagy-methods)) +However, keep in mind that the whole module is basically providing a single functionality: getting a Pagy instance and the paginated items. You could re-write the whole module as one single and simpler method specific to your need, eventually gaining a few IPS in the process. If you seek a bit more performance you are encouraged to [write your own Pagy methods](#writing-your-own-pagy-methods)) -**Notice**: This module is also extended with a few _specific_ extra methods that paginate array collections, if you use the [array extra](../extras/array.md). +**Notice**: This module works for ActiveRecord collections out of the box. For other types of colections you may need to override some sub-method or write your own `pagy_*` methods. For paginating Arrays you may want to use the [array extra](../extras/array.md). ## Synopsys @@ -50,17 +50,6 @@ If you need to use multiple different types of collections in the same app or ac Sub-method called only by the `pagy` method, it returns the hash of variables used to initialize the Pagy object. -Here is its source: - -```ruby -# 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), - page: params[vars[:page_param]||VARS[:page_param]] }.merge!(vars) -end -``` - Override it if you need to add or change some variable. For example you may want to add the `:item_path` or the `:item_name` to customize the `pagy_info` output, or even cache the `count`. _IMPORTANT_: `:count` and `:page` are the only 2 required Pagy core variables, so be careful not to remove them from the returned hash. @@ -104,16 +93,4 @@ def pagy_custom(collection, vars={}) end ``` -Or if you want to preserve the complete functionality of the `:page_param` variable: - -```ruby -def pagy_another_custom(collection, vars={}) - vars = { count: collection.count(:all), - page: params[vars[:page_param]||VARS[:page_param]] }.merge!(vars) - pagy = Pagy.new(vars) - # return the Pagy instance and the paginated items - return pagy, collection.offset(pagy.offset).limit(pagy.limit) -end -``` - -You can just copy and paste the above example, end edit the specific parts that you need for _any possible_ environment. +You can easily write a `pagy` method for _any possible_ environment: please read how to [Paginate Any Collection](../how-to.md#paginate-any-collection) for details. diff --git a/lib/pagy/backend.rb b/lib/pagy/backend.rb index 8f257037f..3992c43bd 100644 --- a/lib/pagy/backend.rb +++ b/lib/pagy/backend.rb @@ -16,10 +16,10 @@ def pagy(collection, vars={}) # Sub-method called only by #pagy: here for easy customization of variables by overriding def pagy_get_vars(collection, vars) - count = collection.count(:all) - count = count.count if count.respond_to?(:count) # e.g. AR .group returns an OrderdHash that responds to #count - # return the merged variables to initialize the pagy object - { count: count, page: params[vars[:page_param] || VARS[:page_param]] }.merge!(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) end # Sub-method called only by #pagy: here for easy customization of record-extraction by overriding