From 81b4de39ee778221449ed12980a30d3e7e24ecc0 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Tue, 17 Dec 2019 10:55:06 +0100 Subject: [PATCH] FIX: prevents crash in discourse_tagging with empty term (#8548) --- lib/discourse_tagging.rb | 6 ++---- spec/components/discourse_tagging_spec.rb | 10 ++++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/discourse_tagging.rb b/lib/discourse_tagging.rb index 3abeefbe925..94b9f2cf93b 100644 --- a/lib/discourse_tagging.rb +++ b/lib/discourse_tagging.rb @@ -222,7 +222,7 @@ module DiscourseTagging distinct_clause = if opts[:order_popularity] "DISTINCT ON (topic_count, name)" - elsif opts[:order_search_results] + elsif opts[:order_search_results] && opts[:term].present? "DISTINCT ON (lower(name) = lower(:cleaned_term), topic_count, name)" else "" @@ -278,9 +278,7 @@ module DiscourseTagging term = opts[:term] if term.present? - term = term.gsub("_", "\\_") - clean_tag(term) - term.downcase! + term = term.gsub("_", "\\_").downcase builder.where("LOWER(name) LIKE :term") builder_params[:cleaned_term] = term builder_params[:term] = "%#{term}%" diff --git a/spec/components/discourse_tagging_spec.rb b/spec/components/discourse_tagging_spec.rb index dcd0f7f1c3d..ee12b4ce37b 100644 --- a/spec/components/discourse_tagging_spec.rb +++ b/spec/components/discourse_tagging_spec.rb @@ -129,6 +129,16 @@ describe DiscourseTagging do end end + context 'empty term' do + it "works with an empty term" do + tags = DiscourseTagging.filter_allowed_tags(Guardian.new(user), + term: '', + order_search_results: true + ).map(&:name) + expect(sorted_tag_names(tags)).to eq(sorted_tag_names([tag1, tag2, tag3])) + end + end + context 'tag synonyms' do fab!(:base_tag) { Fabricate(:tag, name: 'discourse') } fab!(:synonym) { Fabricate(:tag, name: 'discource', target_tag: base_tag) }