From 49a6d0b7890e7c80df554b18c5b64d4a28987a76 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 9 May 2016 16:33:55 -0400 Subject: [PATCH] FIX: Don't bother with negative offsets --- lib/topic_query.rb | 12 ++++++++++-- spec/controllers/list_controller_spec.rb | 5 +++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/topic_query.rb b/lib/topic_query.rb index f14c3dca42f..d43ffd3cbdc 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -374,7 +374,11 @@ class TopicQuery result = result.limit(options[:per_page]) unless options[:limit] == false result = result.visible if options[:visible] || @user.nil? || @user.regular? - result = result.offset(options[:page].to_i * options[:per_page]) if options[:page] + + if options[:page] + offset = options[:page].to_i * options[:per_page] + result = result.offset(offset) if offset > 0 + end result end @@ -463,7 +467,11 @@ class TopicQuery result = result.visible if options[:visible] result = result.where.not(topics: {id: options[:except_topic_ids]}).references(:topics) if options[:except_topic_ids] - result = result.offset(options[:page].to_i * options[:per_page]) if options[:page] + + if options[:page] + offset = options[:page].to_i * options[:per_page] + result.offset(offset) if offset > 0 + end if options[:topic_ids] result = result.where('topics.id in (?)', options[:topic_ids]).references(:topics) diff --git a/spec/controllers/list_controller_spec.rb b/spec/controllers/list_controller_spec.rb index e45b9c7ef2f..c07c6ad2f73 100644 --- a/spec/controllers/list_controller_spec.rb +++ b/spec/controllers/list_controller_spec.rb @@ -41,6 +41,11 @@ describe ListController do expect(parsed["topic_list"]["topics"].length).to eq(1) end + + it "doesn't throw an error with a negative page" do + xhr :get, :top, page: -1024 + expect(response).to be_success + end end describe 'RSS feeds' do