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
This commit is contained in:
evazion
2020-06-28 02:45:05 -05:00
parent cc73f2468b
commit ad02b26f3d
2 changed files with 41 additions and 5 deletions

View File

@@ -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 %>

View File

@@ -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