Skip to content

Commit

Permalink
renamed argument items > fetched; removed redundant assignation
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Dec 11, 2018
1 parent b885ddc commit e9ab4a1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
4 changes: 2 additions & 2 deletions docs/api/countless.md
Expand Up @@ -59,6 +59,6 @@ The construction of the final `Pagy::Countless` object is splitted into 2 steps:

The initial constructor takes the usual hash of variables, calculating only the requested `items` and the `offset`, useful to query the page of items.

### finalize(items)
### finalize(fetched)

The actual calculation of all the internal variables for the pagination is calculated using the `items` number argument. The method returns the finalized instance object.
The actual calculation of all the internal variables for the pagination is calculated using the number of `fetched` items. The method returns the finalized instance object.
2 changes: 1 addition & 1 deletion docs/extras/countless.md
Expand Up @@ -5,7 +5,7 @@ title: Countless

This extra uses the `Pagy::Countless` subclass in order to avoid to execute an otherwise needed count query. It is especially useful when used with large DB tables, where [Caching the count](../how-to.md#caching-the-count) may not be an option.

Its usage is practically the same as the regular `Pagy::Backend` module (see the [backend doc](../api/backend.md).
Its usage is practically the same as the regular `Pagy::Backend` module (see the [backend doc](../api/backend.md)).

The pagination resulting from this extra has some limitation as documented in the [Pagy::Countless Caveats doc](../api/countless.md#caveats).

Expand Down
19 changes: 9 additions & 10 deletions lib/pagy/countless.rb
Expand Up @@ -14,16 +14,15 @@ def initialize(vars={})
@offset = @items * (@page - 1) + @outset # pagination offset + outset (initial offset)
end

# Finalize the instance variables based on the retrieved items
def finalize(items)
items == 0 && @page > 1 and raise(OverflowError.new(self), "page #{@page} got no items")
@pages = @last = (items > @items ? @page + 1 : @page) # set the @pages and @last
@items = items if items < @items && items > 0 # adjust items for last non-empty page
@from = items == 0 ? 0 : @offset+1 - @outset # page begins from item
@to = items == 0 ? 0 : @offset + @items - @outset # page ends to item
@prev = (@page-1 unless @page == 1) # nil if no prev page
@next = (@page+1 unless @page == @last) # nil if no next page
@next = (@page == @last ? (1 if @vars[:cycle]) : @page+1) # nil if no next page
# Finalize the instance variables based on the fetched items
def finalize(fetched)
fetched == 0 && @page > 1 and raise(OverflowError.new(self), "page #{@page} got no items")
@pages = @last = (fetched > @items ? @page + 1 : @page) # set the @pages and @last
@items = fetched if fetched < @items && fetched > 0 # adjust items for last non-empty page
@from = fetched == 0 ? 0 : @offset+1 - @outset # page begins from item
@to = fetched == 0 ? 0 : @offset + @items - @outset # page ends to item
@prev = (@page-1 unless @page == 1) # nil if no prev page
@next = @page == @last ? (1 if @vars[:cycle]) : @page+1 # nil if no next page, 1 if :cycle
self
end

Expand Down

0 comments on commit e9ab4a1

Please sign in to comment.