Skip to content

Commit

Permalink
Fixes #36711 - Fix searching in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
girijaasoni committed Nov 24, 2023
1 parent 3054853 commit 6f09591
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/models/setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def validate(record)

scoped_search :on => :id, :complete_enabled => false, :only_explicit => true, :validator => ScopedSearch::Validators::INTEGER
scoped_search on: :name, complete_value: :true, operators: ['=', '~']
scoped_search on: :description, complete_value: :true, operators: ['~']

delegate :settings_type, :encrypted, :encrypted?, :default, to: :setting_definition, allow_nil: true

Expand All @@ -68,6 +69,10 @@ def self.per_page
20
end

def self.complete_for(search_query, opts = {})
SettingRegistry::SettingCompleter.auto_complete(Foreman.settings, scoped_search_definition, search_query, opts)
end

def self.[](name)
Foreman.settings[name]
end
Expand Down
2 changes: 1 addition & 1 deletion app/presenters/setting_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def matches_search_query?(query)
end

if query =~ /name\s*=\s*(\S+)/
name == tokenized.last
name == tokenized.last || full_name == tokenized.last
elsif query =~ /name\s*~\s*(\S+)/
search_value = tokenized.last
name.include?(search_value) || full_name&.include?(search_value)
Expand Down
43 changes: 43 additions & 0 deletions app/services/setting_registry.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,49 @@
class SettingRegistry
include Singleton
include Enumerable
extend ScopedSearch::ClassMethods

class SettingCompleter < ScopedSearch::AutoCompleteBuilder
def initialize(registry, definition, query, options)
@registry = registry
super(definition, query, options)
end

def self.auto_complete(registry, definition, query, options)
return [] if (query.nil? || definition.nil? || !definition.respond_to?(:fields))

new(registry, definition, query, options).build_autocomplete_options
end

def is_query_valid
true
end

def complete_value
if last_token_is(COMPARISON_OPERATORS)
token = tokens[tokens.size - 2]
val = ''
else
token = tokens[tokens.size - 3]
val = tokens[tokens.size - 1]
end
field = definition.field_by_name(token)
return [] unless field&.complete_value
complete_value_from_db(field, val)
end

def complete_value_from_db(field, val)
count = 20
if field.field == :name
results = @registry.filter_map { |set| ((set.full_name =~ /\s/) ? "\"#{set.full_name.gsub('"', '\"')}\"" : set.full_name) if set.name.include?(val) || set.full_name&.include?(val) }
results.first(count)
elsif field.field == :description
[]
else
raise ScopedSearch::QueryNotSupported, "Value '#{val}' is not valid for field '#{field.field}'"
end
end
end

def self.subset_registry(subset)
new(subset)
Expand Down

0 comments on commit 6f09591

Please sign in to comment.