Skip to content

Commit

Permalink
Fix the legacy the size in code and test
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Jul 1, 2024
1 parent 35fcc36 commit d527774
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 102 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ If you upgrade from version `< 8.0.0` see the following:
- `pagy-dev.js`: use the `pagy.min.js`with the `pagy.min.js.map`
- `pagy-module.d.ts`: use `pagy.d.ts`
- The Array type for the `:size` (e.g. `size: [1, 4, 4, 1]`) that generates the classic bar. Use the `:size` set to an integer
with the `gaps: true` variable (which are the default since 8.4.6). If a legacy bar it will remain REALLY a requirement, add `require 'pagy/extras/size` to your `pagy.rb` initalizer for smooth upgrades. (see [size extra](https://ddnexus.github.io/pagy/docs/extras/size))
with the `ends: true` variable (which are the default since 8.4.6). If a legacy bar it will remain REALLY a requirement, add
`require 'pagy/extras/size` to your `pagy.rb` initalizer for smooth upgrades. (see [size extra](https://ddnexus.github.io/pagy/docs/extras/size))
<hr>

## Version 8.6.1
Expand Down
30 changes: 15 additions & 15 deletions e2e/snapshots.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion gem/apps/repro.ru
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ __END__

<h4>pagy_nav_js</h4>
<%= pagy_nav_js(@pagy, id: 'nav-js-responsive', aria_label: 'Pages nav_js_responsove',
steps: { 0 => [1,1,1,1], 500 => [1,3,3,1], 750 => [1,5,5,1], 1000 => [2,6,6,2] }) %>
steps: { 0 => 5, 500 => 7, 750 => 9, 1000 => 11 }) %>

<h4>pagy_combo_nav_js</h4>
<%= pagy_combo_nav_js(@pagy, id: 'combo-nav-js', aria_label: 'Pages combo_nav_js') %>
Expand Down
2 changes: 1 addition & 1 deletion gem/lib/pagy/countless.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def finalize(fetched_size)
module SeriesOverride # :nodoc:
# Override the original series.
# Return nil if :countless_minimal is enabled
def series(*, **)
def series(**)
super unless @vars[:countless_minimal]
end
end
Expand Down
8 changes: 4 additions & 4 deletions gem/lib/pagy/extras/js_tools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ module PagyAddOn
# `Pagy` instance method used by the `pagy*_nav_js` helpers.
# It returns the sequels of width/series generated from the :steps hash
# Example:
# >> pagy = Pagy.new(count:1000, page: 20, steps: {0 => [1,2,2,1], 350 => [2,3,3,2], 550 => [3,4,4,3]})
# >> pagy = Pagy.new(count:1000, page: 20, steps: {0 => 5, 350 => 7, 550 => 9})
# >> pagy.sequels
# #=> { "0" => [1, :gap, 18, 19, "20", 21, 22, :gap, 50],
# "350" => [1, 2, :gap, 17, 18, 19, "20", 21, 22, 23, :gap, 49, 50],
# "550" => [1, 2, 3, :gap, 16, 17, 18, 19, "20", 21, 22, 23, 24, :gap, 48, 49, 50] }
# #=> { "0" => [18, 19, "20", 21, 22],
# "350" => [1, :gap, 19, "20", 21, :gap, 50],
# "550" => [1 :gap, 18, 19, "20", 21, 22, :gap, 50] }
# Notice: if :steps is false it will use the single {0 => @vars[:size]} size
def sequels(steps: @vars[:steps] || { 0 => @vars[:size] }, **_)
raise VariableError.new(self, :steps, 'to define the 0 width', steps) unless steps.key?(0)
Expand Down
2 changes: 1 addition & 1 deletion gem/lib/pagy/extras/overflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def finalize(items)
raise # same as without the extra
when :empty_page
@offset = @items = @from = @to = 0 # vars relative to the actual page
@vars[:size] = [] # no page in the series
@vars[:size] = 0 # no page in the series
self
else
raise VariableError.new(self, :overflow, 'to be in [:empty_page, :exception]', @vars[:overflow])
Expand Down
6 changes: 4 additions & 2 deletions test/pagy/extras/js_tools_json_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@

describe 'Calendar sequels and label_sequels' do
it 'generate the labels for the sequels' do
steps = { 0 => [1, 2, 2, 1], 600 => [1, 3, 3, 1] }
steps = { 0 => 5, 600 => 7 }
pagy = Pagy::Calendar.send(:create, :month,
period: [Time.zone.local(2021, 10, 21, 13, 18, 23, 0),
Time.zone.local(2023, 11, 13, 15, 43, 40, 0)],
steps: steps, page: 3)
steps: steps,
ends: true, # to hit the :gap condition in the calendar sequels override
page: 6)
_(pagy.sequels).must_rematch :sequels
_(pagy.label_sequels).must_rematch :label_sequels
end
Expand Down
32 changes: 13 additions & 19 deletions test/pagy/extras/js_tools_json_test.rb.rematch
Original file line number Diff line number Diff line change
@@ -1,40 +1,34 @@
---
pagy/extras/js_tools_json::#pagy_data#test_0001_uses json:
:data: data-pagy="WyJ0ZXN0X2Z1bmN0aW9uIiwic29tZS1zdHJpbmciLDEyMyx0cnVlXQ=="
pagy/extras/js_tools_json::Calendar sequels and label_sequels#test_0001_generate the labels for the sequels:
:sequels:
'0':
- 1
- 2
- '3'
- 4
- 5
- :gap
- 26
- '6'
- 7
- 8
'600':
- 1
- 2
- '3'
- 4
- :gap
- 5
- 6
- '6'
- 7
- :gap
- 26
:label_sequels:
'0':
- Oct
- Nov
- Dec
- Jan
- Feb
- :gap
- Nov
- Mar
- Apr
- May
'600':
- Oct
- Nov
- Dec
- Jan
- :gap
- Feb
- Mar
- Apr
- :gap
- Nov
pagy/extras/js_tools_json::#pagy_data#test_0001_uses json:
:data: data-pagy="WyJ0ZXN0X2Z1bmN0aW9uIiwic29tZS1zdHJpbmciLDEyMyx0cnVlXQ=="
2 changes: 1 addition & 1 deletion test/pagy/extras/js_tools_oj_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

describe 'Calendar sequels and label_sequels' do
it 'generate the labels for the sequels' do
steps = { 0 => [1, 2, 2, 1], 600 => [1, 3, 3, 1] }
steps = { 0 => 5, 600 => 7 }
pagy = Pagy::Calendar.send(:create, :month,
period: [Time.zone.local(2021, 10, 21, 13, 18, 23, 0),
Time.zone.local(2023, 11, 13, 15, 43, 40, 0)],
Expand Down
10 changes: 2 additions & 8 deletions test/pagy/extras/js_tools_oj_test.rb.rematch
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,29 @@ pagy/extras/js_tools_oj::Calendar sequels and label_sequels#test_0001_generate t
- '3'
- 4
- 5
- :gap
- 26
'600':
- 1
- 2
- '3'
- 4
- 5
- 6
- :gap
- 26
- 7
:label_sequels:
'0':
- Oct
- Nov
- Dec
- Jan
- Feb
- :gap
- Nov
'600':
- Oct
- Nov
- Dec
- Jan
- Feb
- Mar
- :gap
- Nov
- Apr
pagy/extras/js_tools_oj::#pagy_data#test_0001_uses oj:
:data_1: data-pagy="WyJ0ZXN0X2Z1bmN0aW9uIiwic29tZS1zdHJpbmciLDEyMyx0cnVlXQ=="
:data_2: data-pagy="WyJ0ZXN0X2Z1bmN0aW9uIiwic29tZS1zdHJpbmciLDEyMyx0cnVlLCJwYWdlIl0="
2 changes: 1 addition & 1 deletion test/pagy/extras/metadata_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
_(Pagy::DEFAULT[:metadata]).must_rematch :metadata
end
it 'returns the full pagy metadata' do
pagy, _records = app.send(:pagy, @collection, size: [1, 4, 4, 1], metadata: (Pagy::DEFAULT[:metadata]) + [:sequels])
pagy, _records = app.send(:pagy, @collection, metadata: (Pagy::DEFAULT[:metadata]) + [:sequels])
_(app.send(:pagy_metadata, pagy)).must_rematch :metadata
end
it 'checks for unknown metadata' do
Expand Down
82 changes: 37 additions & 45 deletions test/pagy/extras/metadata_test.rb.rematch
Original file line number Diff line number Diff line change
@@ -1,40 +1,12 @@
---
pagy/extras/metadata::#pagy_metadata for Pagy::Calendar#test_0002_returns only specific metadata for Pagy::Calendar:
pagy/extras/metadata::#pagy_metadata for Pagy#test_0004_returns only specific metadata:
:metadata:
:scaffold_url: "/foo?month_page=__pagy_page__"
:scaffold_url: "/foo?page=__pagy_page__"
:page: 3
:from: !ruby/object:ActiveSupport::TimeWithZone
utc: 2021-12-01 05:00:00.000000000 Z
zone: &1 !ruby/object:ActiveSupport::TimeZone
name: EST
time: 2021-12-01 00:00:00.000000000 Z
:to: !ruby/object:ActiveSupport::TimeWithZone
utc: 2022-01-01 05:00:00.000000000 Z
zone: *1
time: 2022-01-01 00:00:00.000000000 Z
:count: 1000
:prev: 2
:next: 4
:pages: 26
pagy/extras/metadata::#pagy_metadata for Pagy#test_0001_defines all metadata:
:metadata:
- :scaffold_url
- :first_url
- :prev_url
- :page_url
- :next_url
- :last_url
- :count
- :page
- :items
- :vars
- :pages
- :last
- :in
- :from
- :to
- :prev
- :next
- :series
:pages: 50
pagy/extras/metadata::#pagy_metadata for Pagy#test_0002_returns the full pagy metadata:
:metadata:
:scaffold_url: "/foo?page=__pagy_page__"
Expand All @@ -50,11 +22,7 @@ pagy/extras/metadata::#pagy_metadata for Pagy#test_0002_returns the full pagy me
:page: 3
:items: 20
:outset: 0
:size:
- 1
- 4
- 4
- 1
:size: 7
:ends: true
:count_args:
- :all
Expand Down Expand Up @@ -95,8 +63,6 @@ pagy/extras/metadata::#pagy_metadata for Pagy#test_0002_returns the full pagy me
- '3'
- 4
- 5
- 6
- 7
- :gap
- 50
:sequels:
Expand All @@ -106,15 +72,41 @@ pagy/extras/metadata::#pagy_metadata for Pagy#test_0002_returns the full pagy me
- '3'
- 4
- 5
- 6
- 7
- :gap
- 50
pagy/extras/metadata::#pagy_metadata for Pagy#test_0004_returns only specific metadata:
pagy/extras/metadata::#pagy_metadata for Pagy#test_0001_defines all metadata:
:metadata:
:scaffold_url: "/foo?page=__pagy_page__"
- :scaffold_url
- :first_url
- :prev_url
- :page_url
- :next_url
- :last_url
- :count
- :page
- :items
- :vars
- :pages
- :last
- :in
- :from
- :to
- :prev
- :next
- :series
pagy/extras/metadata::#pagy_metadata for Pagy::Calendar#test_0002_returns only specific metadata for Pagy::Calendar:
:metadata:
:scaffold_url: "/foo?month_page=__pagy_page__"
:page: 3
:count: 1000
:from: !ruby/object:ActiveSupport::TimeWithZone
utc: 2021-12-01 05:00:00.000000000 Z
zone: &1 !ruby/object:ActiveSupport::TimeZone
name: EST
time: 2021-12-01 00:00:00.000000000 Z
:to: !ruby/object:ActiveSupport::TimeWithZone
utc: 2022-01-01 05:00:00.000000000 Z
zone: *1
time: 2022-01-01 00:00:00.000000000 Z
:prev: 2
:next: 4
:pages: 50
:pages: 26
5 changes: 2 additions & 3 deletions test/pagy/extras/overflow_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
let(:countless_vars) { { page: 100, items: 10 } }
let(:calendar_vars) { { period: PERIOD, page: 100 } }
before do
@pagy = Pagy.new(pagy_vars)
@pagy_calendar = Pagy::Calendar::Day.new(calendar_vars)
@pagy = Pagy.new(pagy_vars)
@pagy_calendar = Pagy::Calendar::Day.new(calendar_vars)
@pagy_countless = Pagy::Countless.new(countless_vars).finalize(0)
@pagy_countless.finalize(0)
end

describe "variables" do
Expand Down
4 changes: 4 additions & 0 deletions test/pagy/extras/size_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ def series_for(page, *expected)
it 'computes an empty series' do
_(Pagy.new(@vars3.merge(count: 100, size: [])).series).must_equal []
end
it 'use super for non array size' do
_(Pagy.new(@vars3.merge(count: 100, size: 0)).series).must_equal []
_(Pagy.new(@vars3.merge(count: 103, size: 7, page: 6)).series).must_equal [1, :gap, 5, "6", 7, :gap, 11]
end
it 'raises Pagy::Variable errors for wrong size' do
_ { Pagy.new(count: 100, page: 2, size: [1, 2, 3]).series }.must_raise Pagy::VariableError
_ { Pagy.new(count: 100, page: 2, size: [1, 2, 3, '4']).series }.must_raise Pagy::VariableError
Expand Down

0 comments on commit d527774

Please sign in to comment.