From 147a239b2807bb98e5f50c162d07c71d71ad1e52 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Thu, 18 Jul 2013 14:47:59 -0400 Subject: [PATCH] Prefer unread topics in the same category --- lib/topic_query.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/topic_query.rb b/lib/topic_query.rb index bf793cb9e0e..1be360926d4 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -87,7 +87,7 @@ class TopicQuery # When logged in we start with different results if @user - builder.add_results(unread_results(per_page: builder.results_left)) + builder.add_results(unread_results(topic: topic, per_page: builder.results_left)) builder.add_results(new_results(per_page: builder.results_left)) unless builder.full? end builder.add_results(random_suggested(topic, builder.results_left)) unless builder.full? @@ -233,9 +233,15 @@ class TopicQuery end def unread_results(options={}) - TopicQuery.unread_filter(default_results(options.reverse_merge(:unordered => true))) - .order('CASE WHEN topics.user_id = tu.user_id THEN 1 ELSE 2 END') - .order(TopicQuery.order_nocategory_with_pinned_sql) + result = TopicQuery.unread_filter(default_results(options.reverse_merge(:unordered => true))) + .order('CASE WHEN topics.user_id = tu.user_id THEN 1 ELSE 2 END') + + # Prefer unread in the same category + if options[:topic] && options[:topic].category_id + result = result.order("CASE WHEN topics.category_id = #{options[:topic].category_id.to_i} THEN 0 ELSE 1 END") + end + + result.order(TopicQuery.order_nocategory_with_pinned_sql) end def random_suggested(topic, count) @@ -243,7 +249,7 @@ class TopicQuery # If we are in a category, prefer it for the random results if topic.category_id - result = result.order("CASE WHEN topics.category_id = #{topic.category_id.to_i} THEN 0 ELSE 1 END, RANDOM()") + result = result.order("CASE WHEN topics.category_id = #{topic.category_id.to_i} THEN 0 ELSE 1 END") end result.order("RANDOM()")