From ad02b26f3dfa4d47ff8377e894bcb67c677ed707 Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 28 Jun 2020 02:45:05 -0500 Subject: [PATCH] pagination: fix exception on empty pages in seq. pagination. Fix exception when the page is empty during sequential pagination. Caused because the paginator can't figure out the next or previous page when the current page is empty. * https://danbooru.donmai.us/posts?page=b0 * https://danbooru.donmai.us/posts?page=a10000000 --- app/views/application/_meta_links.html.erb | 12 ++++---- test/functional/posts_controller_test.rb | 34 ++++++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/app/views/application/_meta_links.html.erb b/app/views/application/_meta_links.html.erb index 7b398ed83..d0ddc7cd4 100644 --- a/app/views/application/_meta_links.html.erb +++ b/app/views/application/_meta_links.html.erb @@ -1,11 +1,13 @@ <%# collection %> <% content_for(:html_header) do %> - <% if collection.try(:prev_page) %> - <%= tag.link rel: "prev", href: url_for(nav_params_for(collection.prev_page)) %> - <% end %> + <% if collection.try(:records).present? %> + <% if collection.try(:prev_page) %> + <%= tag.link rel: "prev", href: url_for(nav_params_for(collection.prev_page)) %> + <% end %> - <% if collection.try(:next_page) %> - <%= tag.link rel: "next", href: url_for(nav_params_for(collection.next_page)) %> + <% if collection.try(:next_page) %> + <%= tag.link rel: "next", href: url_for(nav_params_for(collection.next_page)) %> + <% end %> <% end %> <% end %> diff --git a/test/functional/posts_controller_test.rb b/test/functional/posts_controller_test.rb index 1e673a532..a1c7d3851 100644 --- a/test/functional/posts_controller_test.rb +++ b/test/functional/posts_controller_test.rb @@ -17,6 +17,40 @@ class PostsControllerTest < ActionDispatch::IntegrationTest create_list(:post, 2) end + context "when using sequential pagination" do + should "work with page=a0" do + get posts_path(page: "a0") + assert_response :success + assert_select ".post-preview", count: 3 + assert_select "#paginator-prev", count: 0 + assert_select "#paginator-next", count: 1 + end + + should "work with page=b0" do + get posts_path(page: "b0") + assert_response :success + assert_select ".post-preview", count: 0 + assert_select "#paginator-prev", count: 0 + assert_select "#paginator-next", count: 0 + end + + should "work with page=b100000" do + get posts_path(page: "b100000") + assert_response :success + assert_select ".post-preview", count: 3 + assert_select "#paginator-prev", count: 1 + assert_select "#paginator-next", count: 0 + end + + should "work with page=a100000" do + get posts_path(page: "a100000") + assert_response :success + assert_select ".post-preview", count: 0 + assert_select "#paginator-prev", count: 0 + assert_select "#paginator-next", count: 0 + end + end + context "for an empty search" do should "render the first page" do get root_path