Show posts for last page when page out of range

This commit is contained in:
Chris Hunt 2013-05-18 13:11:01 -07:00
parent c5b2fd8067
commit c2a4f16f24
2 changed files with 34 additions and 9 deletions

View file

@ -283,14 +283,10 @@ class TopicView
private
def filter_posts_in_range(min, max)
max_index = (filtered_post_ids.length - 1)
post_count = (filtered_post_ids.length - 1)
# If we're off the charts, return nil
return nil if min > max_index
# Pin max to the last post
max = max_index if max > max_index
min = 0 if min < 0
max = [max, post_count].min
min = [[min, max].min, 0].max
@index_offset = min

View file

@ -224,6 +224,26 @@ describe TopicView do
end
end
describe '#filter_posts_paged' do
before { SiteSetting.stubs(:posts_per_page).returns(1) }
it 'returns correct posts for first page' do
topic_view.filter_posts_paged(1).should == [p1, p2]
end
it 'returns correct posts for requested page number' do
topic_view.filter_posts_paged(2).should == [p2, p3]
end
it 'returns correct posts for last page' do
topic_view.filter_posts_paged(4).should == [p5]
end
it 'returns posts for last page when page is out of range' do
topic_view.filter_posts_paged(100).should == [p5]
end
end
describe "fitler_posts_before" do
it "returns undeleted posts before a post" do
topic_view.filter_posts_before(p5.post_number).should == [p3, p2, p1]
@ -289,9 +309,18 @@ describe TopicView do
near_view.index_offset.should == 1
near_view.index_reverse.should be_false
end
context "when 'posts per page' exceeds the number of posts" do
before { SiteSetting.stubs(:posts_per_page).returns(100) }
it 'returns all the posts' do
near_view = topic_view_near(p5)
near_view.posts.should == [p1, p2, p3, p5]
near_view.index_offset.should == 0
near_view.index_reverse.should be_false
end
end
end
end
end