Skip to content

Commit

Permalink
comments and docs updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Mar 17, 2019
1 parent 9f057e6 commit 6fa1d03
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 23 deletions.
2 changes: 1 addition & 1 deletion docs/api/backend.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ For overriding convenience, the `pagy` method calls two sub-methods that you may

**Notice**: 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).

Check also the [array extra](../extras/array.md) and [searchkick extra](../extras/searchkick.md) for specific backend customizations.
Check also the [array](../extras/array.md), [searchkick](../extras/searchkick.md) and [elasticsearch_rails](../extras/elasticsearch_rails.md) extras for specific backend customizations.

## Synopsis

Expand Down
2 changes: 1 addition & 1 deletion docs/extras/bootstrap.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Configure [javascript](../extras.md#javascript) if you use `pagy_bootstrap_respo

## Methods

This extra adds 3 nav helpers to the `Pagy::Frontend` module. You can customize them by overriding it directly in your own view helper.
This extra adds 3 nav helpers to the `Pagy::Frontend` module. You can customize them by overriding them directly in your own view helper.

### pagy_bootstrap_nav(pagy)

Expand Down
2 changes: 1 addition & 1 deletion docs/extras/foundation.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Configure [javascript](../extras.md#javascript) if you use `pagy_foundation_resp

## Methods

This extra adds 3 nav helpers to the `Pagy::Frontend` module. You can customize it by overriding it directly in your own view helper.
This extra adds 3 nav helpers to the `Pagy::Frontend` module. You can customize them by overriding them directly in your own view helper.

### pagy_foundation_nav(pagy)

Expand Down
11 changes: 3 additions & 8 deletions docs/extras/overflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,12 @@ Pagy::VARS[:overflow] = :exception

As usual, depending on the scope of the customization, you have a couple of options to set the variables:

As a global default:

```ruby
# globally
Pagy::VARS[:overflow] = :empty_page
```

For a single instance (overriding the global default):

```ruby
pagy(scope, overflow: :empty_page)
Pagy.new(count:100, overflow: :empty_page)
# or for a single instance
@pagy, @records = pagy(scope, overflow: :empty_page)
```

## Modes
Expand Down
89 changes: 80 additions & 9 deletions docs/extras/support.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,79 @@ If you don't need the navbar you can just set the `:size` variable to an empty v

You can also use the `pagy_prev_link` and `pagy_next_link` helpers provided by this extra, mostly useful if you also use the `countless` extra.

Here is an example:

`incremental` action:
```ruby
def incremental
@pagy, @records = pagy(Product.all, link_extra: 'data-remote="true"')
end
```

`incremental.html.erb`

```erb
<div id="content">
<table id="records_table">
<tr>
<th> Name
<th> Description
</tr>
<%= render partial: 'page_items' %>
</table>
<%= render partial: 'next_link' %>
</div>
```

`_page_items.html.erb`

```erb
<% @records.each do |record| %>
<tr>
<td> <%= record.name %> </td>
<td> <%= record.description %> </td>
</tr>
<% end %>
```

`_next_link.html.erb`

```erb
<%== pagy_next_link(@pagy, 'More...', 'id="next_link"') %>
```

`incremental.js.erb`
```erb
$('#records_table').append("<%= j(render 'page_items')%>");
$('#next-link').html("<%= j(render 'next_link') %>");
```

### Auto-incremental

Automatic incremental pagination (sometimes referred as infinite-scroll pagination) is a UI-less pagination that loads the next page at the end of the listing with an AJAX call.

A simple example about how to implement can use the same example for the [Incremental](#navlessincremental) above with just a couple of changes:

Hide the link in `_next_link.html.slim` by adding a style tag:

```erb
<%== pagy_next_link(@pagy, 'More...', 'id="next_link" style="display: none;"') %>
```

Then the following javascript would take care of clicking the link when the page get loaded/resized/scrolled , so to keep the page filled with results, one page at a time:

```js
var loadNextPage = function(){
var wBottom = $(window).scrollTop() + $(window).height();
var elBottom = $('#records_table').offset().top + $('#records_table').height();
if (wBottom > elBottom){ $('#next_link')[0].click() }
};

window.addEventListener('load', loadNextPage);
window.addEventListener('resize', loadNextPage);
window.addEventListener('scroll', loadNextPage);
```

### Circular/Infinite

This type of pagination sets the `next` page to `1` when the current page is the last page, allowing an infinite loop through the pages.
Expand All @@ -40,21 +113,17 @@ For example, it is often used to show a few suggestions of "similar products" in
For example:

```ruby
@pagy, @suggestions = Pagy.new(count: 25, items: 5, cycle: true)
@pagy, @suggestions = pagy(Product.all, count: 25, items: 5, cycle: true)
```

Passing a forced `:count` of 25 will generate 5 pages of 5 items each that will always have a next page. Regardless the actual collection count, you will show the first 25 items of the collection, looping in stripes of 5 items each.

You may want to combine it with something like:

```erb
<%== pagy_next_link(@pagy, 'More...')
<%== pagy_next_link(@pagy, 'More...') %>
```

### Auto-scroll

Pagy supports auto-scroll pagination by providing the `pagy_apply_init_tag` helper, which initializes your custom pagination elements by calling your custom defined javascript function at page load.

### Custom UIs

You can use the `pagy_prev_url` and `pagy_next_url` to build your own links or buttons. Besides, with the `pagy_apply_init_tag` and `pagy_serialized` helpers you can also setup you own custom javascript components.
Expand All @@ -69,13 +138,13 @@ Returns the url for the previous page. Useful to build minimalistic UIs that don

Returns the url for the next page. Useful to build minimalistic UIs that don't use nav bar links (e.g. `countless` extra).

### pagy_prev_link(pagy, text=pagy_t('pagy.nav.prev'), link_extra='')
### pagy_prev_link(pagy, text=pagy_t('pagy.nav.prev'), link_extra="")

Returns the link for the next page. It is the same prev link string which is part of the `pagy_nav` helper.

Useful to build minimalistic helpers UIs that don't use nav bar links (e.g. `countless` extra).

### pagy_next_link(pagy, text=pagy_t('pagy.nav.next'), link_extra='')
### pagy_next_link(pagy, text=pagy_t('pagy.nav.next'), link_extra="")

Returns the link for the next page. It is the same next link string which is part of the `pagy_nav` helper.

Expand All @@ -89,6 +158,8 @@ Returns a hash with all the instance variables, series, prev_url and next_url of

This is a multi-purpose helper that generates a JSON tag that will be loaded and exececuted by the `Pagy.init` javascript function at document load (see [Javascript](../extras.md#javascript)).

**Notice**: This method is useful to create your custom reusable component. For simpler need you can just use some generic javascript.

The method requires a pagy object, a javascript function name and accepts an optional payload (default to the hash returned by `pagy_serialized` method) that will get passed to the function. For example:

```ryby
Expand All @@ -109,4 +180,4 @@ PagyInit.myInitFunction = function(payload){
}
```

You can use it to initialize your custom pagination elements. For auto-scroll, you may want to update some client side variable with the `pagy_next_url` at each page load.
You can use it to initialize your custom pagination elements. For auto-incremental pagination, you may want to update some client side variable with the `pagy_next_url` at each page load.
2 changes: 1 addition & 1 deletion docs/how-to.md
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ When the count caching is not an option, you may want to use the [countless extr

## Adding HTTP headers

Pagy implements the [RFC-8288](https://tools.ietf.org/html/rfc8288) compliant http response headers (and other helpers) useful for API pagination: no need of other dependencies. See the [headers extra](http://ddnexus.github.io/pagy/extras/headers) documentation and examples.
Pagy implements the [RFC-8288](https://tools.ietf.org/html/rfc8288) compliant http response headers (and other helpers) useful for API pagination: no need to use other dependencies. See the [headers extra](http://ddnexus.github.io/pagy/extras/headers) documentation and examples.

## Using the pagy_info helper

Expand Down
2 changes: 1 addition & 1 deletion lib/config/pagy.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# encoding: utf-8
# frozen_string_literal: true

# Pagy initializer file
# Pagy initializer file (v2.1.2)
# Customize only what you really need and notice that Pagy works also without any of the following lines.
# Should you just cherry pick part of this file, please maintain the require-order of the extras

Expand Down
2 changes: 1 addition & 1 deletion lib/pagy/frontend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Frontend

include Helpers

# We use EMPTY + 'whatever' that is almost as fast as +'whatever' but is also 1.9 compatible
# EMPTY + 'string' is almost as fast as +'string' but is also 1.9 compatible
EMPTY = ''

# Generic pagination: it returns the html with the series of links to the pages
Expand Down

0 comments on commit 6fa1d03

Please sign in to comment.