Skip to content

Commit

Permalink
improved OutOfRangeError exception (storing the failed Pagy object)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Jun 18, 2018
1 parent c6d0cb3 commit 9817659
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
13 changes: 13 additions & 0 deletions docs/how-to.md
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,16 @@ def redirect_to_page_20
redirect_to url_for(page: 20), notice: %(Page ##{params[:page]} is out of range. Showing page #20 instead.)
end
```

**Notice**: The `Pagy::OutOfRangeError` exception object contain the failed Pagy object as it was during the initialization process. You can get some useful information from it. For example:

```ruby
# in a controller
rescue_from Pagy::OutOfRangeError, :with => :redirect_to_last_page
private
def redirect_to_last_page(exception)
redirect_to url_for(page: exception.pagy.last), notice: %(Page ##{exception.pagy.page} is out of range. Showing the last page instead.)
end
```
6 changes: 3 additions & 3 deletions lib/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

class Pagy ; VERSION = '0.9.1'

class OutOfRangeError < StandardError; end
class OutOfRangeError < StandardError; attr_reader :pagy; def initialize(pagy) @pagy = pagy end; end

# Root pathname to get the path of Pagy files like templates or dictionaries
def self.root; Pathname.new(__FILE__).dirname end

# default vars
VARS = { page:1, items:20, outset:0, size:[1,4,4,1], page_param: :page, params: {}, anchor: ''.freeze, link_extra: ''.freeze, item_path: 'pagy.info.item_name'.freeze }
VARS = { page:1, items:20, outset:0, size:[1,4,4,1], page_param: :page, params:{}, anchor:''.freeze, link_extra:''.freeze, item_path:'pagy.info.item_name'.freeze }

attr_reader :count, :page, :items, :vars, :pages, :last, :offset, :from, :to, :prev, :next

Expand All @@ -22,7 +22,7 @@ def initialize(vars)
or raise(ArgumentError, "expected :#{k} >= #{min}; got #{instance_variable_get(:"@#{k}").inspect}")
end
@pages = @last = [(@count.to_f / @items).ceil, 1].max # cardinal and ordinal meanings
@page >= 1 && @page <= @last or raise(OutOfRangeError, "expected :page in 1..#{@last}; got #{@page.inspect}")
@page >= 1 && @page <= @last or raise(OutOfRangeError.new(self), "expected :page in 1..#{@last}; got #{@page.inspect}")
@offset = @items * (@page - 1) + @outset # pagination offset + outset (initial offset)
@items = @count - ((@pages-1) * @items) if @page == @last # adjust items for last page
@from = @count == 0 ? 0 : @offset+1 - @outset # page begins from item
Expand Down
3 changes: 2 additions & 1 deletion test/pagy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def test_initialization
assert_raises(ArgumentError) { Pagy.new(count: 100, page: 2, size: [1, 2, 3]).series }
assert_raises(ArgumentError) { Pagy.new(count: 100, page: 2, size: [1, 2, 3, '4']).series }
assert_raises(Pagy::OutOfRangeError) { Pagy.new(count: 100, page: '11') }
assert_raises(Pagy::OutOfRangeError) { Pagy.new(count: 100, page: 12) }
e = assert_raises(Pagy::OutOfRangeError) { Pagy.new(count: 100, page: 12) }
assert_instance_of Pagy, e.pagy
end
end

Expand Down

0 comments on commit 9817659

Please sign in to comment.