FIX: Next pages were missing on <noscript> content. Also fixed some long standing bugs.

This commit is contained in:
Robin Ward 2013-07-05 14:45:54 -04:00
parent 2e7eee7ebd
commit 4c90b16681
3 changed files with 23 additions and 14 deletions

View file

@ -21,7 +21,8 @@ class TopicView
guardian.ensure_can_see!(@topic) 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; @limit = options[:limit] || SiteSetting.posts_per_page;
@ -56,10 +57,16 @@ class TopicView
path path
end end
def last_post
return nil if @posts.blank?
@last_post ||= @posts.last
end
def next_page def next_page
last_post = @filtered_posts.last @next_page ||= begin
if last_post.present? && (@topic.highest_post_number > last_post.post_number) if last_post && (@topic.highest_post_number > last_post.post_number)
(@filtered_posts[0].post_number / SiteSetting.posts_per_page) + 1 @page + 1
end
end end
end end
@ -142,7 +149,8 @@ class TopicView
def filter_posts_paged(page) def filter_posts_paged(page)
page = [page, 1].max page = [page, 1].max
min = SiteSetting.posts_per_page * (page - 1) 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) filter_posts_in_range(min, max)
end end

View file

@ -113,10 +113,9 @@ describe TopicView do
end end
describe "#next_page" do describe "#next_page" do
let(:posts) { [stub(post_number: 1), stub(post_number: 2)] } let(:p2) { stub(post_number: 2) }
let(:topic) do let(:topic) do
topic = Fabricate(:topic) topic = Fabricate(:topic)
topic.stubs(:posts).returns(posts)
topic.stubs(:highest_post_number).returns(5) topic.stubs(:highest_post_number).returns(5)
topic topic
end end
@ -125,11 +124,12 @@ describe TopicView do
before do before do
described_class.any_instance.expects(:find_topic).with(1234).returns(topic) described_class.any_instance.expects(:find_topic).with(1234).returns(topic)
described_class.any_instance.stubs(:filter_posts) described_class.any_instance.stubs(:filter_posts)
described_class.any_instance.stubs(:last_post).returns(p2)
SiteSetting.stubs(:posts_per_page).returns(2) SiteSetting.stubs(:posts_per_page).returns(2)
end end
it "should return the next page" do 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
end end
@ -222,12 +222,13 @@ describe TopicView do
end end
describe '#filter_posts_paged' do 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 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(1).should == [p1, p2]
topic_view.filter_posts_paged(2).should == [p2, p3] topic_view.filter_posts_paged(2).should == [p3, p5]
topic_view.filter_posts_paged(4).should == [p5]
topic_view.filter_posts_paged(100).should == [] topic_view.filter_posts_paged(100).should == []
end end
end end

View file

@ -497,19 +497,19 @@ describe TopicsController do
it 'grabs first page when no filter is provided' do it 'grabs first page when no filter is provided' do
SiteSetting.stubs(:posts_per_page).returns(20) 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 xhr :get, :show, topic_id: topic.id, slug: topic.slug
end end
it 'grabs first page when first page is provided' do it 'grabs first page when first page is provided' do
SiteSetting.stubs(:posts_per_page).returns(20) 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 xhr :get, :show, topic_id: topic.id, slug: topic.slug, page: 1
end end
it 'grabs correct range when a page number is provided' do it 'grabs correct range when a page number is provided' do
SiteSetting.stubs(:posts_per_page).returns(20) 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 xhr :get, :show, topic_id: topic.id, slug: topic.slug, page: 2
end end