Module: Pagy::OverflowExtra::PagyOverride

Included in:
Pagy, Calendar
Defined in:
lib/pagy/extras/overflow.rb

Overview

Support for Pagy class

Defined Under Namespace

Modules: Series

Instance Method Summary collapse

Instance Method Details

#initialize(vars) ⇒ Object

Add rescue clause for different behaviors



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pagy/extras/overflow.rb', line 17

def initialize(vars)
  @overflow ||= false                            # still true if :last_page re-run the method after an overflow
  super
rescue OverflowError
  @overflow = true                               # add the overflow flag
  case @vars[:overflow]
  when :exception
    raise                                        # same as without the extra
  when :last_page
    requested_page = @vars[:page]                # save the requested page (even after re-run)
    initialize vars.merge!(page: @last)          # re-run with the last page
    @vars[:page] = requested_page                # restore the requested page
  when :empty_page
    @offset = @items = @from = @to = 0           # vars relative to the actual page
    if defined?(Calendar) && is_a?(Calendar)     # only for Calendar instances
      edge = @order == :asc ? @final : @initial  # get the edge of the overflow side (neat, but any time would do)
      @from = @to = edge                         # set both to the edge utc time (a >=&&< query will get no records)
    end
    @prev = @last                                # prev relative to the actual page
    extend Series                                # special series for :empty_page
  else
    raise VariableError.new(self, :overflow, 'to be in [:last_page, :empty_page, :exception]', @vars[:overflow])
  end
end

#overflow?Boolean

Is the requested page overflowing?

Returns:

  • (Boolean)


12
13
14
# File 'lib/pagy/extras/overflow.rb', line 12

def overflow?
  @overflow
end