Class: Compass::SearchController

Inherits:
ApplicationController show all
Defined in:
app/controllers/compass/search_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate, #context_modified_at, #current_context, #current_context_id, #set_cache_headers, #validate_context_id

Instance Method Details

#indexObject



5
6
7
8
9
10
11
12
# File 'app/controllers/compass/search_controller.rb', line 5

def index
  if params[:q]
    results = Compass::Search::Provider.global_search(params.require(:q), current_context)
    render json: { results: results }
  else
    providers
  end
end

#providersObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/compass/search_controller.rb', line 28

def providers
  render json: {
    context_id: params[:context_id],
    providers: available_provider_names.map do |name|
      provider_class = find_provider_by_name(name)

      base_info = {
        name: name,
        class_name: provider_class&.name
      }
      begin
        label = provider_class&.new(**current_context)&.label || name.humanize
        base_info.merge(label: label)
      rescue => e
        Rails.logger.warn "Error getting provider info for #{name}: #{e.message}"
        base_info.merge(
            label: name.humanize,
            error: e.message
        )
      end
    end
  }
end

#showObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/compass/search_controller.rb', line 14

def show
  result = Compass::Search::Provider.search(params[:q], params[:provider_name], current_context)
  render json: {
    provider: params[:provider_name],
    context_id: params[:context_id],
    **result
  }
rescue Compass::Search::UnknownProvider => error
  render json: {
    error: error.message,
    available_providers: error.available_providers
  }, status: :not_found
end