Skip to content

A platform for community discussion. Free, open, simple.

FIX: Handle tags with underscores correctly (#26839)

Daniel Waterworth9f9c7f0a2346
diff --git a/lib/discourse_tagging.rb b/lib/discourse_tagging.rb
index a8d40c731ff..9a92dcfcd71 100644
--- a/lib/discourse_tagging.rb
+++ b/lib/discourse_tagging.rb
@@ -509,8 +509,8 @@ module DiscourseTagging
term = opts[:term]
if term.present?
- term = term.gsub("_", "\\_").downcase
builder_params[:cleaned_term] = term
+ term = term.gsub("_", "\\_").downcase
if opts[:term_type] == DiscourseTagging.term_types[:starts_with]
builder_params[:term] = "#{term}%"
diff --git a/spec/lib/discourse_tagging_spec.rb b/spec/lib/discourse_tagging_spec.rb
index 2484ab2a70c..d783da107ba 100644
--- a/spec/lib/discourse_tagging_spec.rb
+++ b/spec/lib/discourse_tagging_spec.rb
@@ -426,6 +426,24 @@ RSpec.describe DiscourseTagging do
expect(tags).to contain_exactly(tag1.name, tag3.name)
end
+ context "with tags with underscores" do
+ fab!(:tag_with_underscore) { Fabricate(:tag, name: "tag_1") }
+ fab!(:another_tag_with_underscore) do
+ Fabricate(:tag, name: "tag_1a", public_topic_count: 10)
+ end
+
+ it "puts the exact match at the start of the results" do
+ tags =
+ DiscourseTagging.filter_allowed_tags(
+ nil,
+ term: "tag_1",
+ order_search_results: true,
+ ).map(&:name)
+
+ expect(tags).to eq(%w[tag_1 tag_1a])
+ end
+ end
+
context "with tag with colon" do
fab!(:tag_with_colon) { Fabricate(:tag, name: "with:colon") }