DEV: Fix `TopcisFilter#filter_tags` not working for a single tag (#20840)

Follow-up to dd88fdeabc
This commit is contained in:
Alan Guo Xiang Tan 2023-03-27 16:58:40 +08:00 committed by GitHub
parent 392cea5852
commit 4624cca00f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 4 deletions

View File

@ -74,10 +74,10 @@ class TopicsFilter
def exclude_topics_with_any_tags(tag_ids)
@scope =
@scope
.left_joins(:topic_tags)
.where("topic_tags.tag_id IS NULL OR topic_tags.tag_id NOT IN (?)", tag_ids)
.distinct(:id)
@scope.where(
"topics.id NOT IN (SELECT DISTINCT topic_id FROM topic_tags WHERE topic_tags.tag_id IN (?))",
tag_ids,
)
end
def include_topics_with_all_tags(tag_ids)

View File

@ -153,6 +153,13 @@ RSpec.describe TopicsFilter do
end
it "should only return topics that are not tagged with all of the specified tags when `match_all` is `true` and `exclude` is `true`" do
expect(
TopicsFilter
.new(guardian: Guardian.new)
.filter_tags(tag_names: [tag.name], match_all: true, exclude: true)
.pluck(:id),
).to contain_exactly(topic_without_tag.id, topic_with_tag2.id, topic_with_group_only_tag.id)
expect(
TopicsFilter
.new(guardian: Guardian.new)
@ -167,6 +174,13 @@ RSpec.describe TopicsFilter do
end
it "should only return topics that are not tagged with any of the specified tags when `match_all` is `false` and `exclude` is `true`" do
expect(
TopicsFilter
.new(guardian: Guardian.new)
.filter_tags(tag_names: [tag.name], match_all: false, exclude: true)
.pluck(:id),
).to contain_exactly(topic_without_tag.id, topic_with_group_only_tag.id, topic_with_tag2.id)
expect(
TopicsFilter
.new(guardian: Guardian.new)