Skip to content

Commit

Permalink
FIX: Allow match_all_tags to be passed as a URL param (discourse#17972
Browse files Browse the repository at this point in the history
)

`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.
  • Loading branch information
jbrw authored Aug 19, 2022
1 parent 42385d0 commit 73b2522
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/controllers/tags_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ def build_topic_list_options
options[:no_tags] = true
else
options[:tags] = tag_params
options[:match_all_tags] = true
options[:match_all_tags] ||= true
end

options
Expand Down
2 changes: 1 addition & 1 deletion lib/topic_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ def default_results(options = {})
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|
Expand Down
4 changes: 4 additions & 0 deletions spec/lib/topic_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,10 @@
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
Expand Down

0 comments on commit 73b2522

Please sign in to comment.