From 29e9180b445f9d48a3b8fb5357c108c7ba0d6881 Mon Sep 17 00:00:00 2001
From: Domizio Demichelis
Date: Sat, 31 Aug 2024 09:43:17 +0200
Subject: [PATCH] Keep offset and limit variables untouched by the overflow
extra in :empty_page mode (closes #733)
---
docs/extras/overflow.md | 24 +++++++++++++-----------
gem/lib/pagy/extras/overflow.rb | 4 ++--
test/pagy/extras/overflow_test.rb | 4 ++--
3 files changed, 17 insertions(+), 15 deletions(-)
diff --git a/docs/extras/overflow.md b/docs/extras/overflow.md
index f0a043682..04f9a74a7 100644
--- a/docs/extras/overflow.md
+++ b/docs/extras/overflow.md
@@ -70,7 +70,8 @@ pagy.last == pagy.page #=> false
pagy.last #=> 5
pagy.last == pagy.prev #=> true (the prev page is the last page relative to the overflowing page)
pagy.next #=> nil
-pagy.offset #=> 0
+pagy.limit #=> 20 (Pagy::DEFAULT[:limit])
+pagy.offset #=> 1980
pagy.from #=> 0
pagy.to #=> 0
pagy.series #=> [1, 2, 3, 4, 5] (no string, so no current page highlighted in the UI)
@@ -89,7 +90,8 @@ pagy.last == pagy.page #=> false
pagy.last #=> nil
pagy.last == pagy.prev #=> true (but nil)
pagy.next #=> nil
-pagy.offset #=> 0
+pagy.limit #=> 20 (Pagy::DEFAULT[:limit])
+pagy.offset #=> 1980
pagy.from #=> 0
pagy.to #=> 0
pagy.series #=> [] (no pages)
@@ -99,25 +101,25 @@ pagy.series #=> [] (no pages)
require 'pagy/calendar'
require 'pagy/extras/overflow'
-local_time = Time.new(2021, 10, 20, 10, 10, 10, '-09:00')
-# => 2021-10-20 10:10:10 -0900
-pagy = Pagy::Calendar::Month.new(period: [local_time, local_time + 60*60*24*130], page: 100)
+Time.zone = 'Eastern Time (US & Canada)'
+period = [Time.zone.local(2021, 10, 21, 13, 18, 23, 0), Time.zone.local(2023, 11, 13, 15, 43, 40, 0)]
+pagy = Pagy::Calendar::Month.new(period: [local_time, local_time + 60*60*24*130], page: 100)
pagy.overflow? #=> true
pagy.vars[:page] #=> 100 (requested page)
pagy.page #=> 100 (actual empty page)
pagy.last == pagy.page #=> false
-pagy.last #=> 5
+pagy.last #=> 26
pagy.last == pagy.prev #=> true (the prev page is the last page relative to the overflowing page)
pagy.next #=> nil
-pagy.from #=> 2022-03-01 00:00:00 -0900 (end time of the final unit)
-pagy.to #=> 2022-03-01 00:00:00 -0900 (same as from: if used it gets no records)
-pagy.series #=> [1, 2, 3, 4, 5] (no string, so no current page highlighted in the UI)
+pagy.from #=> Fri, 01 Dec 2023 00:00:00.000000000 EST -05:00 (end time of the final unit)
+pagy.to #=> Fri, 01 Dec 2023 00:00:00.000000000 EST -05:00 (same as from: if used it gets no records)
+pagy.series #=> [15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26] (no string, so no current page highlighted in the UI)
# small difference with order: :desc, which yield the same result of an empty page
pagy = Pagy::Calendar::Month.new(order: :desc, period: [local_time, local_time + 60*60*24*130], page: 100)
-pagy.from #=> 2021-10-01 00:00:00 -0900 (start time of initial unit)
-pagy.to #=> 2021-10-01 00:00:00 -0900 (same as from: if used it gets no records)
+pagy.from #=> Fri, 01 Oct 2021 00:00:00.000000000 EDT -04:00 (start time of initial unit)
+pagy.to #=> Fri, 01 Oct 2021 00:00:00.000000000 EDT -04:00 (same as from: if used it gets no records)
```
+++ :last_page
diff --git a/gem/lib/pagy/extras/overflow.rb b/gem/lib/pagy/extras/overflow.rb
index 4aed8dbfa..d4be9634d 100644
--- a/gem/lib/pagy/extras/overflow.rb
+++ b/gem/lib/pagy/extras/overflow.rb
@@ -27,11 +27,11 @@ def initialize(**vars)
initialize(**vars, page: @last) # re-run with the last page
@vars[:page] = requested_page # restore the requested page
when :empty_page
- @offset = @limit = @in = @from = @to = 0 # vars relative to the actual page
+ @in = @from = @to = 0 # vars relative to the actual page
if defined?(::Pagy::Calendar::Unit) \
&& is_a?(Calendar::Unit) # only for Calendar::Units 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)
+ @from = @to = edge # set both to the edge time (a >=&&< query will get no records)
end
@prev = @last # prev relative to the actual page
extend Series # special series for :empty_page
diff --git a/test/pagy/extras/overflow_test.rb b/test/pagy/extras/overflow_test.rb
index 3ca1e45a6..bd6fa4013 100644
--- a/test/pagy/extras/overflow_test.rb
+++ b/test/pagy/extras/overflow_test.rb
@@ -71,8 +71,8 @@
it 'works in :empty_page mode in Pagy' do
pagy = Pagy.new(**pagy_vars.merge(overflow: :empty_page))
_(pagy.page).must_equal 100
- _(pagy.offset).must_equal 0
- _(pagy.limit).must_equal 0
+ _(pagy.offset).must_equal 990
+ _(pagy.limit).must_equal 10
_(pagy.in).must_equal 0
_(pagy.from).must_equal 0
_(pagy.to).must_equal 0