Skip to content

Commit 81b4de3

Browse files
authored
FIX: prevents crash in discourse_tagging with empty term (discourse#8548)
1 parent 6ab12ed commit 81b4de3

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

lib/discourse_tagging.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def self.filter_allowed_tags(guardian, opts = {})
222222

223223
distinct_clause = if opts[:order_popularity]
224224
"DISTINCT ON (topic_count, name)"
225-
elsif opts[:order_search_results]
225+
elsif opts[:order_search_results] && opts[:term].present?
226226
"DISTINCT ON (lower(name) = lower(:cleaned_term), topic_count, name)"
227227
else
228228
""
@@ -278,9 +278,7 @@ def self.filter_allowed_tags(guardian, opts = {})
278278

279279
term = opts[:term]
280280
if term.present?
281-
term = term.gsub("_", "\\_")
282-
clean_tag(term)
283-
term.downcase!
281+
term = term.gsub("_", "\\_").downcase
284282
builder.where("LOWER(name) LIKE :term")
285283
builder_params[:cleaned_term] = term
286284
builder_params[:term] = "%#{term}%"

spec/components/discourse_tagging_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@
129129
end
130130
end
131131

132+
context 'empty term' do
133+
it "works with an empty term" do
134+
tags = DiscourseTagging.filter_allowed_tags(Guardian.new(user),
135+
term: '',
136+
order_search_results: true
137+
).map(&:name)
138+
expect(sorted_tag_names(tags)).to eq(sorted_tag_names([tag1, tag2, tag3]))
139+
end
140+
end
141+
132142
context 'tag synonyms' do
133143
fab!(:base_tag) { Fabricate(:tag, name: 'discourse') }
134144
fab!(:synonym) { Fabricate(:tag, name: 'discource', target_tag: base_tag) }

0 commit comments

Comments
 (0)