Skip to content

Commit

Permalink
Update json_api extra to work with pagy keyset
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Jul 10, 2024
1 parent 9f56ecd commit ca99d9a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
19 changes: 13 additions & 6 deletions gem/lib/pagy/extras/jsonapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@ module BackendOverride

# Return the jsonapi links
def pagy_jsonapi_links(pagy, **opts)
{ first: pagy_url_for(pagy, 1, **opts),
last: pagy_url_for(pagy, pagy.last, **opts),
prev: pagy.prev ? pagy_url_for(pagy, pagy.prev, **opts) : nil,
next: pagy.next ? pagy_url_for(pagy, pagy.next, **opts) : nil }
if defined?(Pagy::Keyset) && pagy.instance_of?(Pagy::Keyset)
{ first: pagy_url_for(pagy, nil, **opts),
last: nil,
prev: nil,
next: pagy.next ? pagy_url_for(pagy, pagy.next, **opts) : nil }
else
{ first: pagy_url_for(pagy, 1, **opts),
last: pagy_url_for(pagy, pagy.last, **opts),
prev: pagy.prev ? pagy_url_for(pagy, pagy.prev, **opts) : nil,
next: pagy.next ? pagy_url_for(pagy, pagy.next, **opts) : nil }
end
end

# Should skip the jsonapi
Expand All @@ -40,9 +47,9 @@ def pagy_skip_jsonapi?(vars)
# Override the Backend method
def pagy_get_page(vars)
return super if pagy_skip_jsonapi?(vars)
return 1 if params[:page].nil?
return if params[:page].nil?

[params[:page][vars[:page_param] || DEFAULT[:page_param]].to_i, 1].max
params[:page][vars[:page_param] || DEFAULT[:page_param]]
end
end
Backend.prepend BackendOverride
Expand Down
26 changes: 25 additions & 1 deletion test/pagy/extras/jsonapi_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
require_relative '../../test_helper'
require 'pagy/extras/items'
require 'pagy/extras/jsonapi'
require 'pagy/extras/keyset'

require_relative '../../mock_helpers/collection'
require_relative '../../files/models/pets'
require_relative '../../mock_helpers/app'

describe 'pagy/extras/jsonapi' do
Expand Down Expand Up @@ -75,7 +77,7 @@
app = MockApp.new(params: { page: { number: 3, size: 10 } })
pagy, _records = app.send(:pagy, @collection, page_param: :number, items_param: :size)
result = app.send(:pagy_jsonapi_links, pagy)
_(result.keys).must_equal %i[first last prev next] # not sure it's a requirementS
_(result.keys).must_equal %i[first last prev next]
_(result).must_rematch :result
end
it 'sets the prev value to null when the link is unavailable' do
Expand All @@ -91,4 +93,26 @@
_(result[:next]).must_be_nil
end
end
describe '#pagy_jsonapi_links (keyset)' do
it 'returns the ordered links' do
app = MockApp.new(params: { page: { cursor: 'eyJpZCI6MTB9', size: 10 } })
pagy, _records = app.send(:pagy_keyset,
Pet.order(:id),
page_param: :cursor,
items_param: :size)
result = app.send(:pagy_jsonapi_links, pagy)
_(result.keys).must_equal %i[first last prev next]
_(result).must_rematch :keyset_result
end

it 'sets the next value to null when the link is unavailable' do
app = MockApp.new(params: { page: { size: 50 } })
pagy, _records = app.send(:pagy_keyset,
Pet.order(:id),
page_param: :cursor,
items_param: :size)
result = app.send(:pagy_jsonapi_links, pagy)
_(result[:next]).must_be_nil
end
end
end
6 changes: 6 additions & 0 deletions test/pagy/extras/jsonapi_test.rb.rematch
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ pagy/extras/jsonapi::#pagy_jsonapi_links#test_0001_returns the ordered links:
:last: "/foo?page%5Bnumber%5D=100&page%5Bsize%5D=10"
:prev: "/foo?page%5Bnumber%5D=2&page%5Bsize%5D=10"
:next: "/foo?page%5Bnumber%5D=4&page%5Bsize%5D=10"
pagy/extras/jsonapi::#pagy_jsonapi_links (keyset)#test_0001_returns the ordered links:
:keyset_result:
:first: "/foo?page%5Bcursor%5D&page%5Bsize%5D=10"
:last:
:prev:
:next: "/foo?page%5Bcursor%5D=eyJpZCI6MjB9&page%5Bsize%5D=10"

0 comments on commit ca99d9a

Please sign in to comment.