autocomplete: fix exception when typing "/" in autocomplete.

Fix an exception that could occur when typing "/" by itself in
autocomplete and a regular tag starting with "/" was returned. This
caused an exception in `r[:antecedent].length` because the tag's
antecedent was nil.
This commit is contained in:
evazion
2020-12-14 21:57:28 -06:00
parent 4cdaf7bcdf
commit 26246b0ac9
2 changed files with 25 additions and 12 deletions

View File

@@ -69,7 +69,8 @@ class AutocompleteService
string = string + "*" unless string.include?("*")
results = tag_matches(string)
results += tag_abbreviation_matches(string)
results = results.uniq.sort_by { |r| [r[:antecedent].length, -r[:post_count]] }.take(limit)
results = results.sort_by { |r| [r[:antecedent].to_s.size, -r[:post_count]] }
results = results.uniq { |r| r[:value] }.take(limit)
elsif string.include?("*")
results = tag_matches(string)
results = tag_other_name_matches(string) if results.blank?

View File

@@ -82,19 +82,31 @@ class AutocompleteServiceTest < ActiveSupport::TestCase
assert_autocomplete_includes("touhou", "~tou", :tag_query)
end
should "autocomplete tag abbreviations" do
create(:tag, name: "mole", post_count: 150)
create(:tag, name: "mole_under_eye", post_count: 100)
create(:tag, name: "mole_under_mouth", post_count: 50)
context "for a tag abbreviation" do
should "autocomplete abbreviations" do
create(:tag, name: "mole", post_count: 150)
create(:tag, name: "mole_under_eye", post_count: 100)
create(:tag, name: "mole_under_mouth", post_count: 50)
assert_autocomplete_equals(%w[mole mole_under_eye mole_under_mouth], "/m", :tag_query)
assert_autocomplete_equals(%w[mole_under_eye mole_under_mouth], "/mu", :tag_query)
assert_autocomplete_equals(%w[mole_under_mouth], "/mum", :tag_query)
assert_autocomplete_equals(%w[mole_under_eye], "/mue", :tag_query)
assert_autocomplete_equals(%w[mole_under_eye], "/*ue", :tag_query)
assert_autocomplete_equals(%w[mole mole_under_eye mole_under_mouth], "/m", :tag_query)
assert_autocomplete_equals(%w[mole_under_eye mole_under_mouth], "/mu", :tag_query)
assert_autocomplete_equals(%w[mole_under_mouth], "/mum", :tag_query)
assert_autocomplete_equals(%w[mole_under_eye], "/mue", :tag_query)
assert_autocomplete_equals(%w[mole_under_eye], "/*ue", :tag_query)
assert_autocomplete_includes("mole_under_eye", "-/mue", :tag_query)
assert_autocomplete_includes("mole_under_eye", "~/mue", :tag_query)
assert_autocomplete_includes("mole_under_eye", "-/mue", :tag_query)
assert_autocomplete_includes("mole_under_eye", "~/mue", :tag_query)
end
should "work for regular tags starting with a /" do
create(:tag, name: "jojo_pose", post_count: 100)
create(:tag, name: "/jp/", post_count: 50)
assert_autocomplete_equals(%w[/jp/ jojo_pose], "/", :tag_query)
assert_autocomplete_equals(%w[/jp/ jojo_pose], "/j", :tag_query)
assert_autocomplete_equals(%w[/jp/ jojo_pose], "/jp", :tag_query)
assert_autocomplete_equals(%w[/jp/], "/jp/", :tag_query)
end
end
should "autocomplete tags from wiki and artist other names" do