Class: QuickSearch::SearchController

Inherits:
ApplicationController show all
Includes:
DoiTrap, EncodeUtf8, OnCampus, QueryFilter, QueryParser, SearcherConcern, SearcherConfig
Defined in:
app/controllers/quick_search/search_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



17
18
19
20
21
# File 'app/controllers/quick_search/search_controller.rb', line 17

def index
  loaded_searches
  @common_searches = common_searches
  http_search
end

#log_eventObject



126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/controllers/quick_search/search_controller.rb', line 126

def log_event
  if params[:category].present? && params[:event_action].present? && params[:label].present?
    Event.create(category: params[:category], action: params[:event_action], label: params[:label][0..250])
    if params[:callback].present?
      head :ok, content_type: 'text/javascript'
    else
      head :ok
    end
  else
    head :bad_request
  end
end

#log_searchObject



117
118
119
120
121
122
123
124
# File 'app/controllers/quick_search/search_controller.rb', line 117

def log_search
  if params[:query].present? && params[:page].present?
    Search.create(query: params[:query], page: params[:page])
    head :ok
  else
    head :bad_request
  end
end

#single_searcherObject

TODO: throw error if required files not in place



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/quick_search/search_controller.rb', line 24

def single_searcher
  searcher_name = params[:searcher_name]

  searcher_cfg = searcher_config(searcher_name)
  if searcher_cfg and searcher_cfg.has_key? 'loaded_searches'
    additional_services = Array.new(searcher_cfg['loaded_searches'])
  else
    additional_services = []
  end
  loaded_searches(additional_services)
  @common_searches = searcher_cfg['common_searches'] || []
  #TODO: maybe a default template for single-searcher searches?
  http_search(searcher_name, "quick_search/search/#{searcher_name}_search")
end

#xhr_searchObject

The following searches for individual sections of the page. This allows us to do client-side requests in cases where the original server-side request times out or otherwise fails.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/controllers/quick_search/search_controller.rb', line 42

def xhr_search
  endpoint = params[:endpoint]

  if params[:template] == 'with_paging'
    template = 'xhr_response_with_paging'
  else
    template = 'xhr_response'
  end

  @query = params_q_scrubbed
  @page = page
  @per_page = per_page(endpoint)
  @offset = offset(@page,@per_page)

  http_client = HTTPClient.new
  update_searcher_timeout(http_client, endpoint, true)

  benchmark "%s xhr #{endpoint}" % CGI.escape(@query.to_str) do

    klass = "QuickSearch::#{endpoint.camelize}Searcher".constantize
    searcher = klass.new(http_client,
                         extracted_query(params_q_scrubbed),
                         @per_page,
                         @offset,
                         @page,
                         on_campus?(ip),
                         extracted_scope(params_q_scrubbed))
    searcher.search

    searcher_partials = {}
    searcher_cfg = searcher_config(endpoint)
    unless searcher_cfg.blank?
      services = searcher_cfg['services'].blank? ? [] : searcher_cfg['services']
    else
      services = []
    end
    services << endpoint

    respond_to do |format|

      format.html {
        services.each do |service|
          service_template = render_to_string(
            :partial => "quick_search/search/#{template}",
            :layout => false,
            :locals => { module_display_name: t("#{endpoint}_search.display_name"),
                         searcher: searcher,
                         search: '',
                         service_name: service
                        })
          searcher_partials[service] = service_template
        end
        render :json => searcher_partials
      }

      format.json {

        # prevents openstruct object from results being nested inside tables
        # See: http://stackoverflow.com/questions/7835047/collecting-hashes-into-openstruct-creates-table-entry
        result_list = []
        searcher.results.each do |result|
          result_list << result.to_h
        end

        render :json => { :endpoint => endpoint,
                          :per_page => @per_page.to_s,
                          :page => @page.to_s,
                          :total => searcher.total,
                          :results => result_list
        }
      }
    end
  end
end