Fix exception in Pool#previous_post_id when the post id is not contained in the pool. This can happen when a post's pool_string lists the post as being in a certain pool, but the post is not included in the pool's post_ids. Such pool_string / post_ids inconsistencies exist because of past bugs.
This commit is contained in:
@@ -268,19 +268,25 @@ class Pool < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def first_post?(post_id)
|
def first_post?(post_id)
|
||||||
page_number(post_id) == 1
|
post_id == post_ids.first
|
||||||
|
end
|
||||||
|
|
||||||
|
def last_post?(post_id)
|
||||||
|
post_id == post_ids.last
|
||||||
end
|
end
|
||||||
|
|
||||||
# XXX finds wrong post when the pool contains multiple copies of the same post (#2042).
|
# XXX finds wrong post when the pool contains multiple copies of the same post (#2042).
|
||||||
def previous_post_id(post_id)
|
def previous_post_id(post_id)
|
||||||
|
return nil if first_post?(post_id) || !contains?(post_id)
|
||||||
|
|
||||||
n = post_ids.index(post_id) - 1
|
n = post_ids.index(post_id) - 1
|
||||||
return nil if n < 0
|
|
||||||
post_ids[n]
|
post_ids[n]
|
||||||
end
|
end
|
||||||
|
|
||||||
def next_post_id(post_id)
|
def next_post_id(post_id)
|
||||||
|
return nil if last_post?(post_id) || !contains?(post_id)
|
||||||
|
|
||||||
n = post_ids.index(post_id) + 1
|
n = post_ids.index(post_id) + 1
|
||||||
return nil if n >= post_ids.size
|
|
||||||
post_ids[n]
|
post_ids[n]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<%= content_tag :li, id: "nav-link-for-pool-#{pool.id}", class: "pool-category-#{pool.category} pool-selected-#{selected}" do -%>
|
<%= content_tag :li, id: "nav-link-for-pool-#{pool.id}", class: "pool-category-#{pool.category} pool-selected-#{selected}" do -%>
|
||||||
<% if !pool.first_post?(post.id) -%>
|
<% if !pool.first_post?(post.id) && pool.post_ids.first -%>
|
||||||
<%= link_to("«".html_safe, post_path(pool.post_ids.first, pool_id: pool.id), class: "first", title: "to page 1") %>
|
<%= link_to("«".html_safe, post_path(pool.post_ids.first, pool_id: pool.id), class: "first", title: "to page 1") %>
|
||||||
<% else -%>
|
<% else -%>
|
||||||
<span class="first">«</span>
|
<span class="first">«</span>
|
||||||
@@ -25,7 +25,7 @@
|
|||||||
<% end -%>
|
<% end -%>
|
||||||
<% end -%>
|
<% end -%>
|
||||||
|
|
||||||
<% if post.id != pool.post_ids.last -%>
|
<% if !pool.last_post?(post.id) && pool.post_ids.last -%>
|
||||||
<%= link_to("»".html_safe, post_path(pool.post_ids.last, pool_id: pool.id), class: "last", title: "to page #{pool.post_count}") -%>
|
<%= link_to("»".html_safe, post_path(pool.post_ids.last, pool_id: pool.id), class: "last", title: "to page #{pool.post_count}") -%>
|
||||||
<% else -%>
|
<% else -%>
|
||||||
<span class="last">»</span>
|
<span class="last">»</span>
|
||||||
|
|||||||
Reference in New Issue
Block a user