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:
parent
42385d020d
commit
73b2522261
|
@ -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
|
||||
|
|
|
@ -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|
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue