mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Merge pull request #2399 from hyli-inc/0-10-stable
Handle edge case where requested current_page > total_pages in JSONAPI
This commit is contained in:
commit
8f38571ed9
@ -8,6 +8,8 @@ Features:
|
|||||||
|
|
||||||
Fixes:
|
Fixes:
|
||||||
|
|
||||||
|
- [#2399](https://github.com/rails-api/active_model_serializers/pull/2399) Handles edge case where requested current_page > total_pages (@f3z0)
|
||||||
|
|
||||||
Misc:
|
Misc:
|
||||||
|
|
||||||
### [v0.10.12 (2020-12-10)](https://github.com/rails-api/active_model_serializers/compare/v0.10.11...v0.10.12)
|
### [v0.10.12 (2020-12-10)](https://github.com/rails-api/active_model_serializers/compare/v0.10.11...v0.10.12)
|
||||||
|
|||||||
@ -55,11 +55,15 @@ module ActiveModelSerializers
|
|||||||
|
|
||||||
def prev_page_url
|
def prev_page_url
|
||||||
return nil if collection.current_page == FIRST_PAGE
|
return nil if collection.current_page == FIRST_PAGE
|
||||||
|
if collection.current_page > collection.total_pages
|
||||||
|
return url_for_page(collection.total_pages)
|
||||||
|
end
|
||||||
url_for_page(collection.current_page - FIRST_PAGE)
|
url_for_page(collection.current_page - FIRST_PAGE)
|
||||||
end
|
end
|
||||||
|
|
||||||
def next_page_url
|
def next_page_url
|
||||||
return nil if collection.total_pages == 0 || collection.current_page == collection.total_pages
|
return nil if collection.total_pages == 0 ||
|
||||||
|
collection.current_page >= collection.total_pages
|
||||||
url_for_page(collection.next_page)
|
url_for_page(collection.next_page)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -90,6 +90,18 @@ module ActiveModelSerializers
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def greater_than_last_page_links
|
||||||
|
{
|
||||||
|
links: {
|
||||||
|
self: "#{URI}?page%5Bnumber%5D=4&page%5Bsize%5D=2",
|
||||||
|
first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
|
||||||
|
prev: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=2",
|
||||||
|
next: nil,
|
||||||
|
last: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=2"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
def expected_response_when_unpaginatable
|
def expected_response_when_unpaginatable
|
||||||
data
|
data
|
||||||
end
|
end
|
||||||
@ -122,6 +134,13 @@ module ActiveModelSerializers
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def expected_response_with_greater_than_last_page_pagination_links
|
||||||
|
{}.tap do |hash|
|
||||||
|
hash[:data] = []
|
||||||
|
hash.merge! greater_than_last_page_links
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def expected_response_with_empty_collection_pagination_links
|
def expected_response_with_empty_collection_pagination_links
|
||||||
{}.tap do |hash|
|
{}.tap do |hash|
|
||||||
hash[:data] = []
|
hash[:data] = []
|
||||||
@ -141,6 +160,18 @@ module ActiveModelSerializers
|
|||||||
assert_equal expected_response_with_pagination_links, adapter.serializable_hash
|
assert_equal expected_response_with_pagination_links, adapter.serializable_hash
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_pagination_links_invalid_current_page_using_kaminari
|
||||||
|
adapter = load_adapter(using_kaminari(4), mock_request)
|
||||||
|
|
||||||
|
assert_equal expected_response_with_greater_than_last_page_pagination_links, adapter.serializable_hash
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_pagination_links_invalid_current_page_using_will_paginate
|
||||||
|
adapter = load_adapter(using_will_paginate(4), mock_request)
|
||||||
|
|
||||||
|
assert_equal expected_response_with_greater_than_last_page_pagination_links, adapter.serializable_hash
|
||||||
|
end
|
||||||
|
|
||||||
def test_pagination_links_with_additional_params
|
def test_pagination_links_with_additional_params
|
||||||
adapter = load_adapter(using_will_paginate, mock_request(test: 'test'))
|
adapter = load_adapter(using_will_paginate, mock_request(test: 'test'))
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user