FIX: accessing a topic with a 'page' parameter

This commit is contained in:
Régis Hanol 2013-04-04 00:12:27 +02:00
parent 5beaf61ac0
commit 7105349f1f
2 changed files with 18 additions and 7 deletions

View File

@ -140,8 +140,8 @@ class TopicView
end
def filter_posts_paged(page)
page ||= 0
min = (SiteSetting.posts_per_page * page)
page = [page, 1].max
min = SiteSetting.posts_per_page * (page - 1)
max = min + SiteSetting.posts_per_page
filter_posts_in_range(min, max)
end
@ -172,7 +172,7 @@ class TopicView
@posts = @filtered_posts.order('sort_order').where("sort_order > ?", sort_order)
@posts = @posts.includes(:reply_to_user).includes(:topic).joins(:user).limit(@limit)
end
def filter_best(max)
@index_offset = 0
@posts = @filtered_posts.order('percent_rank asc, sort_order asc').where("post_number > 1")
@ -283,7 +283,6 @@ class TopicView
private
def filter_posts_in_range(min, max)
max_index = (filtered_post_ids.length - 1)
# If we're off the charts, return nil

View File

@ -334,12 +334,24 @@ describe TopicsController do
context 'filters' do
it 'grabs first page when no post number is selected' do
TopicView.any_instance.expects(:filter_posts_paged).with(0)
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)
xhr :get, :show, id: topic.id
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)
xhr :get, :show, id: topic.id, 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)
xhr :get, :show, id: topic.id, page: 2
end
it 'delegates a post_number param to TopicView#filter_posts_near' do
TopicView.any_instance.expects(:filter_posts_near).with(p2.post_number)
xhr :get, :show, id: topic.id, post_number: p2.post_number