diff --git a/lib/topic_view.rb b/lib/topic_view.rb index 21b9fb90d53..b5f01fd585b 100644 --- a/lib/topic_view.rb +++ b/lib/topic_view.rb @@ -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 diff --git a/spec/controllers/topics_controller_spec.rb b/spec/controllers/topics_controller_spec.rb index 58b46d4255e..e9927e74905 100644 --- a/spec/controllers/topics_controller_spec.rb +++ b/spec/controllers/topics_controller_spec.rb @@ -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