Skip to content

Commit

Permalink
added countless support to the overflow extra
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Nov 19, 2018
1 parent 7b25165 commit f272faf
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 21 deletions.
43 changes: 38 additions & 5 deletions docs/extras/overflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Overflow
---
# Overflow Extra

This extra allows for easy handling of overflowing pages. It internally rescues from the `Pagy::OverflowError` offering a few different ready to use modes, quite useful for UIs and/or APIs.
This extra allows for easy handling of overflowing pages. It internally rescues from the `Pagy::OverflowError` offering a few different ready to use modes, quite useful for UIs and/or APIs. It works with `Pagy` or `Pagy::Countless` instances.

## Synopsys

Expand All @@ -14,8 +14,16 @@ In the `pagy.rb` initializer:
```ruby
require 'pagy/extras/overflow'

# default :last_page (other options :empty_page and :exception )
# default :empty_page (other options :last_page and :exception )
Pagy::VARS[:overflow] = :last_page

# OR
require 'pagy/countless'
require 'pagy/extras/overflow'

# default :empty_page (other option :exception )
Pagy::VARS[:overflow] = :exception

```

## Files
Expand All @@ -26,7 +34,7 @@ This extra is composed of the [overflow.rb](https://github.com/ddnexus/pagy/blob

| Variable | Description | Default |
| ------------| -------------------------------------------------------- | ------------ |
| `:overflow` | one of `:last_page`, `:empty_page` or `:exception` modes | `:last_page` |
| `:overflow` | one of `:last_page`, `:empty_page` or `:exception` modes | `:empty_page` |

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

Expand All @@ -49,7 +57,9 @@ These are the modes accepted by the `:overflow` variable:

### :last_page

This is the default mode. It is useful in apps with an UI, in order to avoid to redirect to the last page.
**Notice**: Not available for `Pagy::Countless` instances.

It is useful in apps with an UI, in order to avoid to redirect to the last page.

Regardless the overflowing page requested, Pagy will set the page to the last page and paginate exactly as if the last page has been requested. For example:

Expand All @@ -65,7 +75,9 @@ pagy.last == pagy.page #=> true

### :empty_page

This mode will paginate the actual requested page, which - being overflowing - is empty. It is useful with APIs, where the client expects an empty set of results in order to stop requesting further pages. For example:
This is the default mode; it will paginate the actual requested page, which - being overflowing - is empty. It is useful with APIs, where the client expects an empty set of results in order to stop requesting further pages.

Example for `Pagy` instance:

```ruby
pagy = Pagy.new(count: 100, page: 100, overflow: :empty_page)
Expand All @@ -83,6 +95,27 @@ pagy.to #=> 0
pagy.series #=> [1, 2, 3, 4, 5] (no string, so no current page highlighted in the UI)
```

Example for `Pagy::Countless` instance:

```ruby
require 'pagy/countless'
require 'pagy/extras/overflow'

pagy = Pagy::Countless.new(count: 100, page: 100, overflow: :empty_page).finalize(0)

pagy.overflow? #=> true
pagy.vars[:page] #=> 100 (requested page)
pagy.page #=> 100 (actual empty page)
pagy.last == pagy.page #=> false
pagy.last #=> nil
pagy.last == pagy.prev #=> true (but nil)
pagy.next #=> nil
pagy.offset #=> 0
pagy.from #=> 0
pagy.to #=> 0
pagy.series #=> [] (no pages)
```

### :exception

This mode raises the `Pagy::OverflowError` as usual, so you can rescue from and do what is needed. It is useful when you need to use your own custom mode even in presence of this extra (which would not raise any error).
Expand Down
43 changes: 32 additions & 11 deletions lib/pagy/extras/overflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

class Pagy

VARS[:overflow] = :last_page
VARS[:overflow] = :empty_page

def overflow?; @overflow end
def overflow?; !!@overflow end

module Overflow

Expand All @@ -29,20 +29,41 @@ def initialize(vars)
end
end

module Series
def series(size=@vars[:size])
@page = @last # series for last page
super(size).tap do |s| # call original series
s[s.index(@page.to_s)] = @page # string to integer (i.e. no current page)
@page = @vars[:page] # restore the actual page
end
end
end

end
prepend Overflow

module Series
# support for Pagy::Countless
if defined?(Pagy::Countless)
module CountlessOverflow

def series(size=@vars[:size])
@page = @last # series for last page
super(size).tap do |s| # call original series
s[s.index(@page.to_s)] = @page # string to integer (i.e. no current page)
@page = @vars[:page] # restore the actual page
def finalize(items)
super
rescue OverflowError
@overflow = true # add the overflow flag
case @vars[:overflow]
when :exception
raise # same as without the extra
when :empty_page
@offset = @items = @from = @to = 0 # vars relative to the actual page
@vars[:size] = [] # no page in the series
self
else
raise ArgumentError, "expected :overflow variable in [:empty_page, :exception]; got #{@vars[:overflow].inspect}"
end
end
end

end
Countless.prepend CountlessOverflow
end

prepend Overflow

end
25 changes: 20 additions & 5 deletions test/pagy/extras/overflow_test.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
require 'pagy'
require_relative '../../test_helper'
require 'pagy/extras/overflow'

SingleCov.covered!
SingleCov.covered!(uncovered: 1) # the condition for countless

describe Pagy do

let(:vars) {{ page: 100, count: 103, items: 10, size: [3, 2, 2, 3] }}
let(:pagy) {Pagy.new(vars)}
let(:countless_vars) {{ page: 100, items: 10 }}

before do
@pagy = Pagy.new(vars)
@pagy_countless = Pagy::Countless.new(countless_vars)
@pagy_countless.finalize(0)
end

describe "variables" do

it 'has vars defaults' do
Pagy::VARS[:overflow].must_equal :last_page
Pagy::VARS[:overflow].must_equal :empty_page # default for countless
end

end

describe "#overflow?" do

it 'must be overflow?' do
pagy.must_be :overflow?
@pagy.must_be :overflow?
@pagy_countless.must_be :overflow?
Pagy.new(vars.merge(page:2)).wont_be :overflow?
end

Expand All @@ -30,6 +36,7 @@
describe "#initialize" do

it 'works in :last_page mode' do
pagy = Pagy.new(vars.merge(overflow: :last_page))
pagy.must_be_instance_of Pagy
pagy.page.must_equal pagy.last
pagy.vars[:page].must_equal 100
Expand All @@ -42,6 +49,7 @@

it 'raises OverflowError in :exception mode' do
proc { Pagy.new(vars.merge(overflow: :exception)) }.must_raise Pagy::OverflowError
proc { Pagy::Countless.new(countless_vars.merge(overflow: :exception)).finalize(0) }.must_raise Pagy::OverflowError
end

it 'works in :empty_page mode' do
Expand All @@ -56,6 +64,7 @@

it 'raises ArgumentError' do
proc { Pagy.new(vars.merge(overflow: :unknown)) }.must_raise ArgumentError
proc { Pagy::Countless.new(countless_vars.merge(overflow: :unknown)).finalize(0) }.must_raise ArgumentError
end

end
Expand All @@ -69,6 +78,12 @@
pagy.page.must_equal 100
end

it 'computes empty series' do
series = @pagy_countless.series
series.must_equal []
@pagy_countless.page.must_equal 100
end

end

end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
require 'pagy'
require 'pagy/countless'
require 'rack'
require_relative 'test_helper/array'
require_relative 'test_helper/frontend'
Expand Down

0 comments on commit f272faf

Please sign in to comment.