From 1bbd7283ce5f3a7f8f054119ed3a67d46154d094 Mon Sep 17 00:00:00 2001 From: Gregory Ray Date: Wed, 2 Dec 2020 15:04:36 -0800 Subject: [PATCH 1/4] this fixes a bug where an invalid next link appears if the current page is greater than total_pages adding tests fixing rubocop violation --- .../adapter/json_api/pagination_links.rb | 6 +++- .../adapter/json_api/pagination_links_test.rb | 31 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/active_model_serializers/adapter/json_api/pagination_links.rb b/lib/active_model_serializers/adapter/json_api/pagination_links.rb index b4890c65..a41153a6 100644 --- a/lib/active_model_serializers/adapter/json_api/pagination_links.rb +++ b/lib/active_model_serializers/adapter/json_api/pagination_links.rb @@ -55,11 +55,15 @@ module ActiveModelSerializers def prev_page_url 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) end 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) end diff --git a/test/adapter/json_api/pagination_links_test.rb b/test/adapter/json_api/pagination_links_test.rb index e8ee5ab4..be1f475a 100644 --- a/test/adapter/json_api/pagination_links_test.rb +++ b/test/adapter/json_api/pagination_links_test.rb @@ -90,6 +90,18 @@ module ActiveModelSerializers } 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 data end @@ -122,6 +134,13 @@ module ActiveModelSerializers 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 {}.tap do |hash| hash[:data] = [] @@ -141,6 +160,18 @@ module ActiveModelSerializers assert_equal expected_response_with_pagination_links, adapter.serializable_hash 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 adapter = load_adapter(using_will_paginate, mock_request(test: 'test')) From 1f2da3f644a915e1eaf74617e0347e2ade5fb161 Mon Sep 17 00:00:00 2001 From: Gregory Ray Date: Sat, 2 Jan 2021 15:23:49 -0800 Subject: [PATCH 2/4] adding #2399 to CHANGELOG.md --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bcd56b02..d15f7bbc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ Misc: Fixes: - [#2398](https://github.com/rails-api/active_model_serializers/pull/2398) Update rails dependency to < 6.2 (@ritikesh) +- [#2399](https://github.com/rails-api/active_model_serializers/pull/2399) Handles edge case where requested current_page > total_pages (@f3z0) ### [v0.10.11 (2020-12-04)](https://github.com/rails-api/active_model_serializers/compare/v0.10.10...v0.10.11) @@ -534,7 +535,7 @@ Misc: - [#888](https://github.com/rails-api/active_model_serializers/pull/888) Changed duplicated test name in action controller test (@groyoh) - [#890](https://github.com/rails-api/active_model_serializers/pull/890) Remove unused method `def_serializer` (@JustinAiken) - [#887](https://github.com/rails-api/active_model_serializers/pull/887) Fixing tests on JRuby (@joaomdmoura) -- [#885](https://github.com/rails-api/active_model_serializers/pull/885) Updates rails versions for test and dev (@tonyta) +- [#885](https://github.com/rails-api/active_model_serializers/pull/885) Updates rails for test and dev (@tonyta) ### [v0.10.0.rc1 (2015-04-22)](https://github.com/rails-api/active_model_serializers/compare/86fc7d7227f3ce538fcb28c1e8c7069ce311f0e1...v0.10.0.rc1) - [#810](https://github.com/rails-api/active_model_serializers/pull/810) Adding Fragment Cache to AMS (@joaomdmoura) From c954d39f3566864170f94e1bca45b0af923d09d9 Mon Sep 17 00:00:00 2001 From: Gregory Ray Date: Sat, 2 Jan 2021 15:55:36 -0800 Subject: [PATCH 3/4] adding #2399 to CHANGELOG.md --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d15f7bbc..987adc2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ Features: Fixes: +- [#2399](https://github.com/rails-api/active_model_serializers/pull/2399) Handles edge case where requested current_page > total_pages (@f3z0) + Misc: ### [v0.10.12 (2020-12-10)](https://github.com/rails-api/active_model_serializers/compare/v0.10.11...v0.10.12) @@ -15,7 +17,6 @@ Misc: Fixes: - [#2398](https://github.com/rails-api/active_model_serializers/pull/2398) Update rails dependency to < 6.2 (@ritikesh) -- [#2399](https://github.com/rails-api/active_model_serializers/pull/2399) Handles edge case where requested current_page > total_pages (@f3z0) ### [v0.10.11 (2020-12-04)](https://github.com/rails-api/active_model_serializers/compare/v0.10.10...v0.10.11) From e2f48b5067f6027f0f85c6ef8937bb57f0a63058 Mon Sep 17 00:00:00 2001 From: Gregory Ray Date: Sat, 2 Jan 2021 15:57:27 -0800 Subject: [PATCH 4/4] adding #2399 to CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 987adc2a..7eafc33d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -536,7 +536,7 @@ Misc: - [#888](https://github.com/rails-api/active_model_serializers/pull/888) Changed duplicated test name in action controller test (@groyoh) - [#890](https://github.com/rails-api/active_model_serializers/pull/890) Remove unused method `def_serializer` (@JustinAiken) - [#887](https://github.com/rails-api/active_model_serializers/pull/887) Fixing tests on JRuby (@joaomdmoura) -- [#885](https://github.com/rails-api/active_model_serializers/pull/885) Updates rails for test and dev (@tonyta) +- [#885](https://github.com/rails-api/active_model_serializers/pull/885) Updates rails versions for test and dev (@tonyta) ### [v0.10.0.rc1 (2015-04-22)](https://github.com/rails-api/active_model_serializers/compare/86fc7d7227f3ce538fcb28c1e8c7069ce311f0e1...v0.10.0.rc1) - [#810](https://github.com/rails-api/active_model_serializers/pull/810) Adding Fragment Cache to AMS (@joaomdmoura)