FIX: Allow `match_all_tags` to be passed as a URL param (#17972)

`TopicQueryParams` allows for `match_all_tags` to be passed as a query parameter. `TagsController` forces the value to be true.

This change allows a value to be passed, and only sets it to true if no value has been set. It then uses `ActiveModel::Type::Boolean.new.cast` to compare the value.
This commit is contained in:
jbrw 2022-08-19 15:41:56 -04:00 committed by GitHub
parent 42385d020d
commit 73b2522261
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 2 deletions

View File

@ -479,7 +479,7 @@ class TagsController < ::ApplicationController
options[:no_tags] = true
else
options[:tags] = tag_params
options[:match_all_tags] = true
options[:match_all_tags] ||= true
end
options

View File

@ -680,7 +680,7 @@ class TopicQuery
tags_query = tags_arg[0].is_a?(String) ? Tag.where_name(tags_arg) : Tag.where(id: tags_arg)
tags = tags_query.select(:id, :target_tag_id).map { |t| t.target_tag_id || t.id }.uniq
if @options[:match_all_tags]
if ActiveModel::Type::Boolean.new.cast(@options[:match_all_tags])
# ALL of the given tags:
if tags_arg.length == tags.length
tags.each_with_index do |tag, index|

View File

@ -427,6 +427,10 @@ RSpec.describe TopicQuery do
expect(TopicQuery.new(moderator, tags: [tag.name, other_tag.name], match_all_tags: true).list_latest.topics.map(&:id)).to eq([tagged_topic3.id])
end
it "can return topics with tag intersections using truthy/falsey values" do
expect(TopicQuery.new(moderator, tags: [tag.name, other_tag.name], match_all_tags: "false").list_latest.topics.map(&:id).sort).to eq([tagged_topic1.id, tagged_topic2.id, tagged_topic3.id].sort)
end
it "returns an empty relation when an invalid tag is passed" do
expect(TopicQuery.new(moderator, tags: [tag.name, 'notatag'], match_all_tags: true).list_latest.topics).to be_empty
end