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

Also:

Move includes call higher which makes it possible to run all of the
intermediate queries for easier debugging.

Add tests for TagsController with categories in the path.
This commit is contained in:
Daniel Waterworth 2019-11-01 16:21:10 +00:00
parent 29ee467a04
commit 200cef90ea
2 changed files with 69 additions and 3 deletions

View File

@ -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)

View File

@ -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