diff --git a/lib/topic_view.rb b/lib/topic_view.rb index 579269954..5e852e704 100644 --- a/lib/topic_view.rb +++ b/lib/topic_view.rb @@ -21,7 +21,8 @@ class TopicView guardian.ensure_can_see!(@topic) - @post_number, @page = options[:post_number], options[:page] + @post_number, @page = options[:post_number], options[:page].to_i + @page = 1 if @page == 0 @limit = options[:limit] || SiteSetting.posts_per_page; @@ -56,10 +57,16 @@ class TopicView path end + def last_post + return nil if @posts.blank? + @last_post ||= @posts.last + end + def next_page - last_post = @filtered_posts.last - if last_post.present? && (@topic.highest_post_number > last_post.post_number) - (@filtered_posts[0].post_number / SiteSetting.posts_per_page) + 1 + @next_page ||= begin + if last_post && (@topic.highest_post_number > last_post.post_number) + @page + 1 + end end end @@ -142,7 +149,8 @@ class TopicView def filter_posts_paged(page) page = [page, 1].max min = SiteSetting.posts_per_page * (page - 1) - max = min + SiteSetting.posts_per_page + max = (min + SiteSetting.posts_per_page) - 1 + filter_posts_in_range(min, max) end diff --git a/spec/components/topic_view_spec.rb b/spec/components/topic_view_spec.rb index 59c817155..ce9b88c4e 100644 --- a/spec/components/topic_view_spec.rb +++ b/spec/components/topic_view_spec.rb @@ -113,10 +113,9 @@ describe TopicView do end describe "#next_page" do - let(:posts) { [stub(post_number: 1), stub(post_number: 2)] } + let(:p2) { stub(post_number: 2) } let(:topic) do topic = Fabricate(:topic) - topic.stubs(:posts).returns(posts) topic.stubs(:highest_post_number).returns(5) topic end @@ -125,11 +124,12 @@ describe TopicView do before do described_class.any_instance.expects(:find_topic).with(1234).returns(topic) described_class.any_instance.stubs(:filter_posts) + described_class.any_instance.stubs(:last_post).returns(p2) SiteSetting.stubs(:posts_per_page).returns(2) end it "should return the next page" do - described_class.new(1234, user).next_page.should eql(1) + described_class.new(1234, user).next_page.should eql(2) end end @@ -222,12 +222,13 @@ describe TopicView do end describe '#filter_posts_paged' do - before { SiteSetting.stubs(:posts_per_page).returns(1) } + before { SiteSetting.stubs(:posts_per_page).returns(2) } it 'returns correct posts for all pages' do + puts [p1.id, p2.id, p3.id, p4.id, p5.id].inspect + topic_view.filter_posts_paged(1).should == [p1, p2] - topic_view.filter_posts_paged(2).should == [p2, p3] - topic_view.filter_posts_paged(4).should == [p5] + topic_view.filter_posts_paged(2).should == [p3, p5] topic_view.filter_posts_paged(100).should == [] end end diff --git a/spec/controllers/topics_controller_spec.rb b/spec/controllers/topics_controller_spec.rb index a0578198a..fff0e2715 100644 --- a/spec/controllers/topics_controller_spec.rb +++ b/spec/controllers/topics_controller_spec.rb @@ -497,19 +497,19 @@ describe TopicsController do it 'grabs first page when no filter is provided' do SiteSetting.stubs(:posts_per_page).returns(20) - TopicView.any_instance.expects(:filter_posts_in_range).with(0, 20) + TopicView.any_instance.expects(:filter_posts_in_range).with(0, 19) xhr :get, :show, topic_id: topic.id, slug: topic.slug end it 'grabs first page when first page is provided' do SiteSetting.stubs(:posts_per_page).returns(20) - TopicView.any_instance.expects(:filter_posts_in_range).with(0, 20) + TopicView.any_instance.expects(:filter_posts_in_range).with(0, 19) xhr :get, :show, topic_id: topic.id, slug: topic.slug, page: 1 end it 'grabs correct range when a page number is provided' do SiteSetting.stubs(:posts_per_page).returns(20) - TopicView.any_instance.expects(:filter_posts_in_range).with(20, 40) + TopicView.any_instance.expects(:filter_posts_in_range).with(20, 39) xhr :get, :show, topic_id: topic.id, slug: topic.slug, page: 2 end