From c2a4f16f24aac59ce2144d98a2a6346844e3382e Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Sat, 18 May 2013 13:11:01 -0700 Subject: [PATCH 1/2] Show posts for last page when page out of range --- lib/topic_view.rb | 10 +++------ spec/components/topic_view_spec.rb | 33 ++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/lib/topic_view.rb b/lib/topic_view.rb index bcd89d9c885..bad4704b50a 100644 --- a/lib/topic_view.rb +++ b/lib/topic_view.rb @@ -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 diff --git a/spec/components/topic_view_spec.rb b/spec/components/topic_view_spec.rb index 5ebf3f004e0..293feb62703 100644 --- a/spec/components/topic_view_spec.rb +++ b/spec/components/topic_view_spec.rb @@ -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 From fb4e5973db1d63f4735deeac1bf54baec1955005 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Sat, 18 May 2013 13:11:34 -0700 Subject: [PATCH 2/2] Fix spelling mistake in spec doc --- spec/components/topic_view_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/components/topic_view_spec.rb b/spec/components/topic_view_spec.rb index 293feb62703..78c3a929fe1 100644 --- a/spec/components/topic_view_spec.rb +++ b/spec/components/topic_view_spec.rb @@ -244,7 +244,7 @@ describe TopicView do end end - describe "fitler_posts_before" do + describe "filter_posts_before" do it "returns undeleted posts before a post" do topic_view.filter_posts_before(p5.post_number).should == [p3, p2, p1] topic_view.should_not be_initial_load