lib/pagy.rb in pagy-5.8.1 vs lib/pagy.rb in pagy-5.9.0

- old
+ new

@@ -3,11 +3,11 @@ require 'pathname' # Core class class Pagy - VERSION = '5.8.1' + VERSION = '5.9.0' # Root pathname to get the path of Pagy files like templates or dictionaries def self.root @root ||= Pathname.new(__dir__).freeze end @@ -30,14 +30,14 @@ def initialize(vars) normalize_vars(vars) setup_vars(count: 0, page: 1, outset: 0) setup_items_var setup_pages_var + setup_offset_var setup_params_var raise OverflowError.new(self, :page, "in 1..#{@last}", @page) if @page > @last - @offset = (@items * (@page - 1)) + @outset @from = [@offset - @outset + 1, @count].min @to = [@offset - @outset + @items, @count].min @in = [@to - @from + 1, @count].min @prev = (@page - 1 unless @page == 1) @next = @page == @last ? (1 if @vars[:cycle]) : @page + 1 @@ -94,20 +94,25 @@ raise VariableError.new(self, name, ">= #{min}", @vars[name]) \ unless @vars[name] && instance_variable_set(:"@#{name}", @vars[name].to_i) >= min end end - # Setup and validate the items (overridden by the gearbox extra) + # Setup @items (overridden by the gearbox extra) def setup_items_var setup_vars(items: 1) end - # Setup and validates the pages (overridden by the gearbox extra) + # Setup @pages and @last (overridden by the gearbox extra) def setup_pages_var @pages = @last = [(@count.to_f / @items).ceil, 1].max end - # Setup and validates the params + # Setup @offset based on the :gearbox_items variable + def setup_offset_var + @offset = (@items * (@page - 1)) + @outset # may be already set from gear_box + end + + # Setup and validate @params def setup_params_var raise VariableError.new(self, :params, 'must be a Hash or a Proc', @params) \ unless (@params = @vars[:params]).is_a?(Hash) || @params.is_a?(Proc) end end