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

View file

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