Skip to content

Commit

Permalink
simpler overflow module structure (backward compatible)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Feb 22, 2019
1 parent 9c1212b commit 532743d
Showing 1 changed file with 35 additions and 34 deletions.
69 changes: 35 additions & 34 deletions lib/pagy/extras/overflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,50 @@ class Pagy

VARS[:overflow] = :empty_page

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

module Overflow

def initialize(vars)
super
rescue OverflowError
@overflow = true # add the overflow flag
case @vars[:overflow]
when :exception
raise # same as without the extra
when :last_page
initial_page = @vars[:page] # save the very initial page (even after re-run)
super(vars.merge!(page: @last)) # re-run with the last page
@vars[:page] = initial_page # restore the inital page
when :empty_page
@offset = @items = @from = @to = 0 # vars relative to the actual page
@prev = @last # prev relative to the actual page
extend(Series) # special series for :empty_page
else
raise ArgumentError, "expected :overflow variable in [:last_page, :empty_page, :exception]; got #{@vars[:overflow].inspect}"
end
alias :initialize_without_overflow :initialize
def initialize_with_overflow(vars)
@overflow ||= false # don't override if :last_page re-run the method after an overflow
initialize_without_overflow(vars)
rescue OverflowError
@overflow = true # add the overflow flag
case @vars[:overflow]
when :exception
raise # same as without the extra
when :last_page
initial_page = @vars[:page] # save the very initial page (even after re-run)
initialize(vars.merge!(page: @last)) # re-run with the last page
@vars[:page] = initial_page # restore the inital page
when :empty_page
@offset = @items = @from = @to = 0 # vars relative to the actual page
@prev = @last # prev relative to the actual page
extend(Series) # special series for :empty_page
else
raise ArgumentError, "expected :overflow variable in [:last_page, :empty_page, :exception]; got #{@vars[:overflow].inspect}"
end
end
alias :initialize :initialize_with_overflow

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
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 Overflow


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

def finalize(items)
super
alias :finalize_without_overflow :finalize
def finalize_with_overflow(items)
@overflow = false
finalize_without_overflow(items)
rescue OverflowError
@overflow = true # add the overflow flag
case @vars[:overflow]
Expand All @@ -61,9 +62,9 @@ def finalize(items)
raise ArgumentError, "expected :overflow variable in [:empty_page, :exception]; got #{@vars[:overflow].inspect}"
end
end
alias :finalize :finalize_with_overflow

end
Countless.prepend CountlessOverflow
end

end

0 comments on commit 532743d

Please sign in to comment.