Skip to content

A platform for community discussion. Free, open, simple.

FIX: TopicQuery doesn't react well to subcategories without definitions

Daniel Waterworth200cef90ea23
diff --git a/lib/topic_query.rb b/lib/topic_query.rb
index 5060180889b..8a2c4637f33 100644
--- a/lib/topic_query.rb
+++ b/lib/topic_query.rb
@@ -663,7 +663,7 @@ class TopicQuery
options[:visible] = false if @user && @user.id == options[:filtered_to_user]
# Start with a list of all topics
- result = Topic.unscoped
+ result = Topic.unscoped.includes(:category)
if @user
result = result.joins("LEFT OUTER JOIN topic_users AS tu ON (topics.id = tu.topic_id AND tu.user_id = #{@user.id.to_i})")
@@ -683,7 +683,7 @@ class TopicQuery
SELECT :category_id
) AND
topics.id NOT IN (
- SELECT c3.topic_id FROM categories c3 WHERE c3.parent_category_id = :category_id
+ SELECT c3.topic_id FROM categories c3 WHERE c3.parent_category_id = :category_id AND c3.topic_id IS NOT NULL
)
SQL
result = result.where(sql, category_id: category_id)
@@ -753,7 +753,7 @@ class TopicQuery
end
result = apply_ordering(result, options)
- result = result.listable_topics.includes(:category)
+ result = result.listable_topics
if options[:exclude_category_ids] && options[:exclude_category_ids].is_a?(Array) && options[:exclude_category_ids].size > 0
result = result.where("categories.id NOT IN (?)", options[:exclude_category_ids].map(&:to_i)).references(:categories)
diff --git a/spec/requests/tags_controller_spec.rb b/spec/requests/tags_controller_spec.rb
index f1b0d3a2f2a..500988cf19b 100644
--- a/spec/requests/tags_controller_spec.rb
+++ b/spec/requests/tags_controller_spec.rb
@@ -90,6 +90,72 @@ describe TagsController do
get "/tags/test"
expect(response.status).to eq(200)
end
+
+ context "with a category in the path" do
+ fab!(:topic_in_category) {
+ Fabricate(
+ :topic,
+ tags: [tag],
+ category: category
+ )
+ }
+
+ fab!(:topic_in_category_without_tag) {
+ Fabricate(
+ :topic,
+ category: category
+ )
+ }
+
+ fab!(:topic_out_of_category) {
+ Fabricate(
+ :topic,
+ tags: [tag]
+ )
+ }
+
+ it "should produce the topic inside the category and not the topic outside of it" do
+ get "/tags/c/#{category.slug}/#{tag.name}.json"
+
+ topic_ids = json['topic_list']['topics'].map { |x| x['id'] }
+ expect(topic_ids).to include(topic_in_category.id)
+ expect(topic_ids).to_not include(topic_out_of_category.id)
+ expect(topic_ids).to_not include(topic_in_category_without_tag.id)
+ end
+ end
+
+ context "with a subcategory in the path" do
+ fab!(:topic_in_subcategory) {
+ Fabricate(
+ :topic,
+ tags: [tag],
+ category: subcategory
+ )
+ }
+
+ fab!(:topic_in_subcategory_without_tag) {
+ Fabricate(
+ :topic,
+ category: subcategory
+ )
+ }
+
+ fab!(:topic_out_of_subcategory) {
+ Fabricate(
+ :topic,
+ tags: [tag]
+ )
+ }
+
+ it "should produce the topic inside the subcategory and not the topic outside of it" do
+ get "/tags/c/#{category.slug}/#{subcategory.slug}/#{tag.name}.json"
+
+ topic_ids = json['topic_list']['topics'].map { |x| x['id'] }
+ expect(topic_ids).to include(topic_in_subcategory.id)
+ expect(topic_ids).to_not include(topic_out_of_subcategory.id)
+ expect(topic_ids).to_not include(topic_in_subcategory_without_tag.id)
+ end
+ end
end
describe '#check_hashtag' do