diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 096c4d8c382..662d8da5029 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -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 diff --git a/lib/topic_query.rb b/lib/topic_query.rb index f525bb195d8..3f7a5aecb44 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -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| diff --git a/spec/lib/topic_query_spec.rb b/spec/lib/topic_query_spec.rb index 47a5c8a92df..b2183f2c980 100644 --- a/spec/lib/topic_query_spec.rb +++ b/spec/lib/topic_query_spec.rb @@ -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