Module: Foreman::Controller::AutoCompleteSearch

Instance Method Summary collapse

Instance Method Details

#auto_complete_searchObject


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/foreman/controller/auto_complete_search.rb', line 2

def auto_complete_search
  begin
    model = controller_name == "hosts" ? Host::Managed : controller_name.singularize.camelize.constantize
    @items = model.complete_for(params[:search])
    @items = @items.map do |item|
      category = (['and','or','not','has'].include?(item.to_s.sub(/^.*\s+/,''))) ? 'Operators' : ''
      part = item.to_s.sub(/^.*\b(and|or)\b/i) {|match| match.sub(/^.*\s+/,'')}
      completed = item.to_s.chomp(part)
      {:completed => completed, :part => part, :label => item, :category => category}
    end
  rescue ScopedSearch::QueryNotSupported => e
    @items = [{:error =>e.to_s}]
  end
  render :json => @items
end

#invalid_search_query(e) ⇒ Object


18
19
20
21
# File 'lib/foreman/controller/auto_complete_search.rb', line 18

def invalid_search_query(e)
  error (_("Invalid search query: %s") % e)
  redirect_to :back
end