Skip to content

Commit

Permalink
out_of_range extra improvements (#68)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Grosser <michael@grosser.it>
  • Loading branch information
ddnexus and grosser committed Jul 13, 2018
1 parent f487a42 commit 5958dc5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -101,6 +101,10 @@ An alternative UI that combines the pagination feature with the navigation info

Allow the client to request a custom number of items per page with a ready to use selector UI. _(see [more...](http://ddnexus.github.io/pagy/extras/items))_

### Out Of Range Extra

Allow for easy handling of out of range pages _(see [more...](http://ddnexus.github.io/pagy/extras/out_of_range))_

### Responsive Extra

On resize, the number of page links will adapt in real-time to the available window or container width. _(see [more...](http://ddnexus.github.io/pagy/extras/responsive))_
Expand Down
47 changes: 29 additions & 18 deletions lib/pagy/extras/out_of_range.rb
Expand Up @@ -4,26 +4,37 @@ class Pagy

def out_of_range?; @out_of_range end

alias :create :initialize

def initialize(vars)
create(vars)
rescue OutOfRangeError => e
raise e if @vars[:out_of_range_mode] == :exception
@out_of_range = true
if @vars[:out_of_range_mode] == :last_page
@page = @last # set as last page
elsif @vars[:out_of_range_mode] == :empty_page
@offset = @items = @from = @to = 0 # vars relative to the actual page
@prev = @last # the prev is the last page
define_singleton_method(:series) do |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
module OutOfRange

def initialize(vars)
super
rescue OutOfRangeError => e
@out_of_range = true # adds the out_of_range flag
raise e if @vars[:out_of_range_mode] == :exception
if @vars[:out_of_range_mode] == :last_page
@page = @last # set as the last page
elsif @vars[:out_of_range_mode] == :empty_page
@offset = @items = @from = @to = 0 # vars relative to the actual page
@prev = @last
extend(Series)
else raise ArgumentError, "Unknown mode #{@vars[:out_of_range_mode]}"
end
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

prepend OutOfRange

end
8 changes: 7 additions & 1 deletion test/pagy/extras/out_of_range_test.rb
Expand Up @@ -29,7 +29,7 @@

describe "#initialize" do

it 'initializes with out of range page' do
it 'works in :last_page mode' do
pagy.must_be_instance_of Pagy
pagy.page.must_equal pagy.last
pagy.vars[:page].must_equal 100
Expand All @@ -49,15 +49,21 @@
pagy.prev.must_equal pagy.last
end

it 'raises ArgumentError' do
proc { Pagy.new(vars.merge(out_of_range_mode: :unknown)) }.must_raise ArgumentError
end

end

describe "#series singleton for :empty_page mode" do

it 'computes series for last page' do
pagy = Pagy.new(vars.merge(out_of_range_mode: :empty_page))
series = pagy.series
series.must_equal [1, 2, 3, :gap, 9, 10, 11]
pagy.page.must_equal 100
end

end

end

0 comments on commit 5958dc5

Please sign in to comment.