From 659546c4e4fb338951c1761ebaa43f8bea91bbd3 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 4 Feb 2014 15:55:30 -0500 Subject: [PATCH] CHANGE: Hide category definition topics unless you are viewing that category. --- lib/topic_query.rb | 14 ++++++++------ spec/components/topic_query_spec.rb | 4 ++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/topic_query.rb b/lib/topic_query.rb index c6f86c4f286..cee6faf2bdd 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -121,8 +121,7 @@ class TopicQuery end def list_category(category) - create_list(:category, unordered: true) do |list| - list = list.where(category_id: category.id) + create_list(:category, unordered: true, category: category.id) do |list| if @user list.order(TopicQuerySQL.order_with_pinned_sql) else @@ -132,10 +131,8 @@ class TopicQuery end def list_new_in_category(category) - create_list(:new_in_category, unordered: true) do |list| - list.where(category_id: category.id) - .by_newest - .first(25) + create_list(:new_in_category, unordered: true, category: category.id) do |list| + list.by_newest.first(25) end end @@ -242,6 +239,11 @@ class TopicQuery result = result.listable_topics.includes(category: :topic_only_relative_url) result = result.where('categories.name is null or categories.name <> ?', options[:exclude_category]).references(:categories) if options[:exclude_category] + # Don't include the category topic unless restricted to that category + if options[:category].blank? + result = result.where('COALESCE(categories.topic_id, 0) <> topics.id') + end + result = result.limit(options[:per_page]) unless options[:limit] == false result = result.visible if options[:visible] || @user.nil? || @user.regular? result = result.where.not(topics: {id: options[:except_topic_ids]}).references(:topics) if options[:except_topic_ids] diff --git a/spec/components/topic_query_spec.rb b/spec/components/topic_query_spec.rb index 28e1410ed7d..a530aa87baf 100644 --- a/spec/components/topic_query_spec.rb +++ b/spec/components/topic_query_spec.rb @@ -28,12 +28,12 @@ describe TopicQuery do Topic.recent(10).count.should == 0 # mods can see every group and hidden topics - TopicQuery.new(moderator).list_latest.topics.count.should == 3 + TopicQuery.new(moderator).list_latest.topics.count.should == 2 group.add(user) group.save - TopicQuery.new(user).list_latest.topics.count.should == 2 + TopicQuery.new(user).list_latest.topics.count.should == 1 end