autocomplete: fix exception when completing saved search labels.

Fix an exception that was thrown when trying to autocomplete saved
search labels (e.g. `search:all`) as an anonymous user. This was a
pre-existing bug.
This commit is contained in:
evazion
2020-12-13 00:45:22 -06:00
parent d6a5b9e252
commit 119268e118
3 changed files with 12 additions and 1 deletions

View File

@@ -155,7 +155,8 @@ class AutocompleteService
end
def autocomplete_saved_search_label(string)
labels = SavedSearch.search_labels(current_user.id, label: string).take(limit)
string = "*" + string + "*" unless string.include?("*")
labels = current_user.saved_searches.labels_like(string).take(limit)
labels.map do |label|
{ label: label.tr("_", " "), value: label }

View File

@@ -63,6 +63,14 @@ class SavedSearch < ApplicationRecord
.gsub(/[[:space:]]/, "_")
end
def all_labels
select(Arel.sql("distinct unnest(labels) as label")).order(:label)
end
def labels_like(label)
all_labels.select { |ss| ss.label.ilike?(label) }.map(&:label)
end
def search_labels(user_id, params)
labels = labels_for(user_id)