Module: Pagy::GearboxExtra

Included in:
Pagy
Defined in:
lib/pagy/extras/gearbox.rb

Overview

Automatically change the number of items per page depending on the page number accepts an array as the :gearbox_items variable, that will determine the items for the first pages

Instance Method Summary collapse

Instance Method Details

#setup_items_varObject

Setup @items based on the :gearbox_items variable

Raises:



12
13
14
15
16
17
18
19
20
# File 'lib/pagy/extras/gearbox.rb', line 12

def setup_items_var
  return super if !@vars[:gearbox_extra] || @vars[:items_extra]

  gearbox_items = @vars[:gearbox_items]
  raise VariableError.new(self, :gearbox_items, 'to be an Array of positives', gearbox_items) \
        unless gearbox_items.is_a?(Array) && gearbox_items.all? { |num| num.positive? rescue false } # rubocop:disable Style/RescueModifier

  @items = gearbox_items[@page - 1] || gearbox_items.last
end

#setup_pages_varObject

Setup @pages and @last based on the :gearbox_items variable



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pagy/extras/gearbox.rb', line 23

def setup_pages_var
  return super if !@vars[:gearbox_extra] || @vars[:items_extra]

  gearbox_items = @vars[:gearbox_items]
  # This algorithm is thousands of times faster than the one in the geared_pagination gem
  @pages = @last = (if @count > (sum = gearbox_items.sum)
                      [((@count - sum).to_f / gearbox_items.last).ceil, 1].max + gearbox_items.count
                    else
                      pages    = 0
                      reminder = @count
                      while reminder.positive?
                        pages    += 1
                        reminder -= gearbox_items[pages - 1]
                      end
                      [pages, 1].max
                    end)
end