Skip to content

Commit

Permalink
small performance improvement for #51 and updated doc
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Jun 23, 2018
1 parent 5863ceb commit 99081ee
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 31 deletions.
31 changes: 4 additions & 27 deletions docs/api/backend.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
8 changes: 4 additions & 4 deletions lib/pagy/backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 99081ee

Please sign in to comment.