lib/pagy/extras/gearbox.rb in pagy-5.8.1 vs lib/pagy/extras/gearbox.rb in pagy-5.9.0
- old
+ new
@@ -17,11 +17,11 @@
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 and @last based on the :gearbox_items variable
+ # Setup @pages and @last based on the :gearbox_items variable (not used by Pagy::Countless)
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
@@ -34,9 +34,21 @@
pages += 1
remainder -= gearbox_items[pages - 1]
end
[pages, 1].max
end)
+ end
+
+ # Setup @offset based on the :gearbox_items variable
+ def setup_offset_var
+ return super if !@vars[:gearbox_extra] || @vars[:items_extra]
+
+ gearbox_items = @vars[:gearbox_items]
+ @offset = if @page <= gearbox_items.count
+ gearbox_items[0, @page - 1].sum
+ else
+ gearbox_items.sum + (gearbox_items.last * (@page - gearbox_items.count - 1))
+ end + @outset
end
end
prepend GearboxExtra
end