Skip to content

Commit

Permalink
Rename pagy_url_for -> pagy_page_url (avoid wrong expectations based on
Browse files Browse the repository at this point in the history
the Rails' url_for)
  • Loading branch information
ddnexus committed Jan 5, 2025
1 parent f547817 commit 7a93859
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 88 deletions.
2 changes: 1 addition & 1 deletion gem/lib/pagy/extras/bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def pagy_bootstrap_combo_nav_js(pagy, id: nil, classes: 'pagination', aria_label

%(<nav#{id} class="pagy-bootstrap combo-nav-js" #{
nav_aria_label(pagy, aria_label:)} #{
pagy_data(pagy, :cj, pagy_url_for(pagy, PAGE_TOKEN, **vars))
pagy_data(pagy, :cj, pagy_page_url(pagy, PAGE_TOKEN, **vars))
}><ul class="#{classes}">#{
bootstrap_prev_html(pagy, a)
}<li class="page-item pagy-bootstrap"><label class="page-link">#{
Expand Down
2 changes: 1 addition & 1 deletion gem/lib/pagy/extras/bulma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def pagy_bulma_combo_nav_js(pagy, id: nil, classes: 'pagy-bulma combo-nav-js pag

%(<nav#{id} class="#{classes}" #{
nav_aria_label(pagy, aria_label:)} #{
pagy_data(pagy, :cj, pagy_url_for(pagy, PAGE_TOKEN, **vars))
pagy_data(pagy, :cj, pagy_page_url(pagy, PAGE_TOKEN, **vars))
}>#{
bulma_prev_next_html(pagy, a)
}<ul class="pagination-list"><li class="pagination-link"><label>#{
Expand Down
4 changes: 2 additions & 2 deletions gem/lib/pagy/extras/calendar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def pagy_anchor(pagy, anchor_string: nil)
return super unless (counts = pagy.vars[:counts])

anchor_string &&= %( #{anchor_string})
left, right = %(<a#{anchor_string} href="#{pagy_url_for(pagy, PAGE_TOKEN)}").split(PAGE_TOKEN, 2)
left, right = %(<a#{anchor_string} href="#{pagy_page_url(pagy, PAGE_TOKEN)}").split(PAGE_TOKEN, 2)
# lambda used by all the helpers
lambda do |page, text = pagy.label_for(page), classes: nil, aria_label: nil|
count = counts[page - 1]
Expand All @@ -70,7 +70,7 @@ def pagy_anchor(pagy, anchor_string: nil)
module UrlHelperAddOn
# Return the url for the calendar page at time
def pagy_calendar_url_at(calendar, time, **)
pagy_url_for(calendar.send(:calendar_at, time, **), 1, **)
pagy_page_url(calendar.send(:calendar_at, time, **), 1, **)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions gem/lib/pagy/extras/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ def pagy_headers(pagy)
def pagy_link_header(pagy)
{ 'link' => [].tap do |link|
if defined?(::Pagy::Keyset) && pagy.is_a?(Keyset)
link << %(<#{pagy_url_for(pagy, nil, absolute: true)}>; rel="first")
link << %(<#{pagy_url_for(pagy, pagy.next, absolute: true)}>; rel="next") if pagy.next
link << %(<#{pagy_page_url(pagy, nil, absolute: true)}>; rel="first")
link << %(<#{pagy_page_url(pagy, pagy.next, absolute: true)}>; rel="next") if pagy.next
else
p = PAGE_TOKEN
url_str = pagy_url_for(pagy, PAGE_TOKEN, absolute: true)
url_str = pagy_page_url(pagy, PAGE_TOKEN, absolute: true)
link << %(<#{url_str.sub(p, '1')}>; rel="first")
link << %(<#{url_str.sub(p, pagy.prev.to_s)}>; rel="prev") if pagy.prev
link << %(<#{url_str.sub(p, pagy.next.to_s)}>; rel="next") if pagy.next
Expand Down
12 changes: 6 additions & 6 deletions gem/lib/pagy/extras/jsonapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ module BackendOverride
# Return the jsonapi links
def pagy_jsonapi_links(pagy, **)
if defined?(::Pagy::Keyset) && pagy.is_a?(Keyset)
{ first: pagy_url_for(pagy, nil, **),
{ first: pagy_page_url(pagy, nil, **),
last: nil,
prev: nil,
next: pagy.next ? pagy_url_for(pagy, pagy.next, **) : nil }
next: pagy.next ? pagy_page_url(pagy, pagy.next, **) : nil }
else
{ first: pagy_url_for(pagy, 1, **),
last: pagy_url_for(pagy, pagy.last, **),
prev: pagy.prev ? pagy_url_for(pagy, pagy.prev, **) : nil,
next: pagy.next ? pagy_url_for(pagy, pagy.next, **) : nil }
{ first: pagy_page_url(pagy, 1, **),
last: pagy_page_url(pagy, pagy.last, **),
prev: pagy.prev ? pagy_page_url(pagy, pagy.prev, **) : nil,
next: pagy.next ? pagy_page_url(pagy, pagy.next, **) : nil }
end
end

Expand Down
4 changes: 2 additions & 2 deletions gem/lib/pagy/extras/keyset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ def pagy_keyset(set, **vars)

# Return the URL string for the first page
def pagy_keyset_first_url(pagy, **vars)
pagy_url_for(pagy, nil, **vars)
pagy_page_url(pagy, nil, **vars)
end

# Return the URL string for the next page or nil
def pagy_keyset_next_url(pagy, **vars)
pagy_url_for(pagy, pagy.next, **vars) if pagy.next
pagy_page_url(pagy, pagy.next, **vars) if pagy.next
end
end
Backend.prepend KeysetExtra
Expand Down
2 changes: 1 addition & 1 deletion gem/lib/pagy/extras/limit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def pagy_limit_selector_js(pagy, id: nil, item_name: nil)
vars = pagy.vars
limit = vars[:limit]
vars[:limit] = LIMIT_TOKEN # limit token replaced in the javascript
url_token = pagy_url_for(pagy, PAGE_TOKEN)
url_token = pagy_page_url(pagy, PAGE_TOKEN)
vars[:limit] = limit # restore the limit

limit_input = %(<input name="limit" type="number" min="1" max="#{vars[:limit_max]}" value="#{
Expand Down
2 changes: 1 addition & 1 deletion gem/lib/pagy/extras/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module MetadataExtra

# Return the metadata hash
def pagy_metadata(pagy, absolute: nil)
url_template = pagy_url_for(pagy, PAGE_TOKEN, absolute:)
url_template = pagy_page_url(pagy, PAGE_TOKEN, absolute:)
{}.tap do |metadata|
keys = if defined?(::Pagy::Calendar::Unit) && pagy.is_a?(Calendar::Unit)
pagy.vars[:metadata] - %i[count limit]
Expand Down
10 changes: 5 additions & 5 deletions gem/lib/pagy/extras/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def pagy_combo_nav_js(pagy, id: nil, aria_label: nil, **vars)

%(<nav#{id} class="pagy combo-nav-js" #{
nav_aria_label(pagy, aria_label:)} #{
pagy_data(pagy, :cj, pagy_url_for(pagy, PAGE_TOKEN, **vars))}>#{
pagy_data(pagy, :cj, pagy_page_url(pagy, PAGE_TOKEN, **vars))}>#{
prev_a(pagy, a)
}<label>#{
pagy_t('pagy.combo_nav_js', page_input:, pages:)
Expand All @@ -46,12 +46,12 @@ def pagy_combo_nav_js(pagy, id: nil, aria_label: nil, **vars)

# Return the previous page URL string or nil
def pagy_prev_url(pagy, **vars)
pagy_url_for(pagy, pagy.prev, **vars) if pagy.prev
pagy_page_url(pagy, pagy.prev, **vars) if pagy.prev
end

# Return the next page URL string or nil
def pagy_next_url(pagy, **vars)
pagy_url_for(pagy, pagy.next, **vars) if pagy.next
pagy_page_url(pagy, pagy.next, **vars) if pagy.next
end

# Return the enabled/disabled previous page anchor tag
Expand All @@ -68,12 +68,12 @@ def pagy_next_a(pagy, text: pagy_t('pagy.next'), aria_label: pagy_t('pagy.aria_l

# Conditionally return the previous page link tag
def pagy_prev_link(pagy, **vars)
%(<link href="#{pagy_url_for(pagy, pagy.prev, **vars)}"/>) if pagy.prev
%(<link href="#{pagy_page_url(pagy, pagy.prev, **vars)}"/>) if pagy.prev
end

# Conditionally return the next page link tag
def pagy_next_link(pagy, **vars)
%(<link href="#{pagy_url_for(pagy, pagy.next, **vars)}"/>) if pagy.next
%(<link href="#{pagy_page_url(pagy, pagy.next, **vars)}"/>) if pagy.next
end
end
Frontend.prepend PagyExtra
Expand Down
2 changes: 1 addition & 1 deletion gem/lib/pagy/frontend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Frontend
# Benchmarked on a 20 link nav: it is ~22x faster and uses ~18x less memory than rails' link_to
def pagy_anchor(pagy, anchor_string: nil, **vars)
anchor_string &&= %( #{anchor_string})
left, right = %(<a#{anchor_string} href="#{pagy_url_for(pagy, PAGE_TOKEN, **vars)}").split(PAGE_TOKEN, 2)
left, right = %(<a#{anchor_string} href="#{pagy_page_url(pagy, PAGE_TOKEN, **vars)}").split(PAGE_TOKEN, 2)
# lambda used by all the helpers
lambda do |page, text = pagy.label_for(page), classes: nil, aria_label: nil|
classes = %( class="#{classes}") if classes
Expand Down
2 changes: 1 addition & 1 deletion gem/lib/pagy/url_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def escape(str)
# Return the URL for the page, relying on the params method and Rack by default.
# It supports all Rack-based frameworks (Sinatra, Padrino, Rails, ...).
# For non-rack environments you can use the standalone extra
def pagy_url_for(pagy, page, absolute: false, fragment: nil, **_)
def pagy_page_url(pagy, page, absolute: false, fragment: nil, **_)
vars = pagy.vars
query_params = vars[:url] ? {} : request.GET.clone(freeze: false)
query_params.merge!(vars[:params].transform_keys(&:to_s)) if vars[:params].is_a?(Hash)
Expand Down
18 changes: 9 additions & 9 deletions test/pagy/extras/jsonapi_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,35 @@
it 'uses the :jsonapi with page:nil' do
app = MockApp.new(params: { page: nil })
pagy, _records = app.send(:pagy, @collection, limit_extra: false)
_(app.send(:pagy_url_for, pagy, 1)).must_rematch :url_1
_(app.send(:pagy_page_url, pagy, 1)).must_rematch :url_1
pagy, _records = app.send(:pagy, @collection)
_(app.send(:pagy_url_for, pagy, 1)).must_rematch :url_2
_(app.send(:pagy_page_url, pagy, 1)).must_rematch :url_2
end
it 'uses the :jsonapi with page:3' do
app = MockApp.new(params: { page: { page: 3 } })
pagy, _records = app.send(:pagy, @collection, limit_extra: false)
_(app.send(:pagy_url_for, pagy, 2)).must_rematch :url_1
_(app.send(:pagy_page_url, pagy, 2)).must_rematch :url_1
pagy, _records = app.send(:pagy, @collection)
_(app.send(:pagy_url_for, pagy, 2)).must_rematch :url_2
_(app.send(:pagy_page_url, pagy, 2)).must_rematch :url_2
end
end
describe 'Skip JsonApi' do
it 'skips the :jsonapi with page:nil' do
Pagy::DEFAULT[:jsonapi] = false
app = MockApp.new(params: { page: nil })
pagy, _records = app.send(:pagy, @collection, limit_extra: false)
_(app.send(:pagy_url_for, pagy, 1)).must_equal '/foo?page=1'
_(app.send(:pagy_page_url, pagy, 1)).must_equal '/foo?page=1'
pagy, _records = app.send(:pagy, @collection)
_(app.send(:pagy_url_for, pagy, 1)).must_equal '/foo?page=1&limit=20'
_(app.send(:pagy_page_url, pagy, 1)).must_equal '/foo?page=1&limit=20'
Pagy::DEFAULT[:jsonapi] = true
end
it 'skips the :jsonapi with page:3' do
Pagy::DEFAULT[:jsonapi] = false
app = MockApp.new(params: { page: 3 })
pagy, _records = app.send(:pagy, @collection, limit_extra: false)
_(app.send(:pagy_url_for, pagy, 2)).must_equal '/foo?page=2'
_(app.send(:pagy_page_url, pagy, 2)).must_equal '/foo?page=2'
pagy, _records = app.send(:pagy, @collection)
_(app.send(:pagy_url_for, pagy, 2)).must_equal '/foo?page=2&limit=20'
_(app.send(:pagy_page_url, pagy, 2)).must_equal '/foo?page=2&limit=20'
Pagy::DEFAULT[:jsonapi] = true
end
end
Expand All @@ -69,7 +69,7 @@
it 'sets custom named params' do
app = MockApp.new(params: { page: { number: 3, size: 10 } })
pagy, _records = app.send(:pagy, @collection, page_sym: :number, limit_sym: :size)
_(app.send(:pagy_url_for, pagy, 4)).must_rematch :url
_(app.send(:pagy_page_url, pagy, 4)).must_rematch :url
end
end
describe '#pagy_jsonapi_links' do
Expand Down
12 changes: 6 additions & 6 deletions test/pagy/extras/limit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,26 +113,26 @@ def test_limit_vars_params(limit, vars, params)
end

describe 'view_methods' do
describe '#pagy_url_for' do
describe '#pagy_page_url' do
it 'renders basic url' do
pagy = Pagy.new count: 1000, page: 3
_(app.pagy_url_for(pagy, 5)).must_equal '/foo?page=5&limit=20'
_(app.pagy_page_url(pagy, 5)).must_equal '/foo?page=5&limit=20'
end
it 'renders basic url and limit var' do
pagy = Pagy.new count: 1000, page: 3, limit: 50
_(app.pagy_url_for(pagy, 5)).must_equal '/foo?page=5&limit=50'
_(app.pagy_page_url(pagy, 5)).must_equal '/foo?page=5&limit=50'
end
it 'renders url with limit_sym' do
pagy = Pagy.new count: 1000, page: 3, limit_sym: :custom
_(app.pagy_url_for(pagy, 5)).must_equal '/foo?page=5&custom=20'
_(app.pagy_page_url(pagy, 5)).must_equal '/foo?page=5&custom=20'
end
it 'renders url with fragment' do
pagy = Pagy.new count: 1000, page: 3
_(app.pagy_url_for(pagy, 6, fragment: '#fragment')).must_equal '/foo?page=6&limit=20#fragment'
_(app.pagy_page_url(pagy, 6, fragment: '#fragment')).must_equal '/foo?page=6&limit=20#fragment'
end
it 'renders url with params and fragment' do
pagy = Pagy.new count: 1000, page: 3, params: { a: 3, b: 4 }, limit: 40
_(app.pagy_url_for(pagy, 5, fragment: '#fragment')).must_equal "/foo?page=5&a=3&b=4&limit=40#fragment"
_(app.pagy_page_url(pagy, 5, fragment: '#fragment')).must_equal "/foo?page=5&a=3&b=4&limit=40#fragment"
end
end
it 'renders limit selector' do
Expand Down
14 changes: 7 additions & 7 deletions test/pagy/extras/standalone_limit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
describe 'pagy/extras/standalone_limit' do
let(:app) { MockApp.new }

describe '#pagy_url_for' do
describe '#pagy_page_url' do
it 'renders url with params and fragment' do
pagy = Pagy.new(count: 1000, page: 3, params: { a: 3, b: 4 })
_(app.pagy_url_for(pagy, 5, fragment: '#fragment')).must_equal "/foo?page=5&a=3&b=4&limit=20#fragment"
_(app.pagy_url_for(pagy, 5, absolute: true, fragment: '#fragment')).must_equal "http://example.com:3000/foo?page=5&a=3&b=4&limit=20#fragment"
_(app.pagy_page_url(pagy, 5, fragment: '#fragment')).must_equal "/foo?page=5&a=3&b=4&limit=20#fragment"
_(app.pagy_page_url(pagy, 5, absolute: true, fragment: '#fragment')).must_equal "http://example.com:3000/foo?page=5&a=3&b=4&limit=20#fragment"
pagy = Pagy.new(count: 1000, page: 3, params: { a: [1, 2, 3] }, url: 'http://www.pagy-standalone.com/subdir')
_(app.pagy_url_for(pagy, 5, fragment: '#fragment')).must_equal "http://www.pagy-standalone.com/subdir?a%5B%5D=1&a%5B%5D=2&a%5B%5D=3&page=5&limit=20#fragment"
_(app.pagy_url_for(pagy, 5, absolute: true, fragment: '#fragment')).must_equal "http://www.pagy-standalone.com/subdir?a%5B%5D=1&a%5B%5D=2&a%5B%5D=3&page=5&limit=20#fragment"
_(app.pagy_page_url(pagy, 5, fragment: '#fragment')).must_equal "http://www.pagy-standalone.com/subdir?a%5B%5D=1&a%5B%5D=2&a%5B%5D=3&page=5&limit=20#fragment"
_(app.pagy_page_url(pagy, 5, absolute: true, fragment: '#fragment')).must_equal "http://www.pagy-standalone.com/subdir?a%5B%5D=1&a%5B%5D=2&a%5B%5D=3&page=5&limit=20#fragment"
pagy = Pagy.new(count: 1000, page: 3, params: { a: nil }, url: '')
_(app.pagy_url_for(pagy, 5, fragment: '#fragment')).must_equal "?a&page=5&limit=20#fragment"
_(app.pagy_url_for(pagy, 5, absolute: true, fragment: '#fragment')).must_equal "?a&page=5&limit=20#fragment"
_(app.pagy_page_url(pagy, 5, fragment: '#fragment')).must_equal "?a&page=5&limit=20#fragment"
_(app.pagy_page_url(pagy, 5, absolute: true, fragment: '#fragment')).must_equal "?a&page=5&limit=20#fragment"
end
end
end
Loading

0 comments on commit 7a93859

Please sign in to comment.