Class: QuickSearch::BaseQuickSearch

Inherits:
Object
  • Object
show all
Defined in:
app/queries/quick_search/base_quick_search.rb

Constant Summary collapse

DEFAULT_MAX_RECORDS =
30

Instance Method Summary collapse

Constructor Details

#initialize(fallback) ⇒ BaseQuickSearch

Returns a new instance of BaseQuickSearch.



4
5
6
# File 'app/queries/quick_search/base_quick_search.rb', line 4

def initialize(fallback)
  @fallback = fallback
end

Instance Method Details

#authoritative_searchObject



55
56
57
# File 'app/queries/quick_search/base_quick_search.rb', line 55

def authoritative_search
  false
end

#extract_term(query) ⇒ Object



49
50
51
52
53
# File 'app/queries/quick_search/base_quick_search.rb', line 49

def extract_term(query)
  return unless query.present?

  query&.scrub&.squeeze(' ')&.strip
end

#find(params, max_records_param = nil, existing_results = []) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/queries/quick_search/base_quick_search.rb', line 8

def find(params, max_records_param = nil, existing_results = [])
  remaining_records = max_records_param || DEFAULT_MAX_RECORDS
  max_records = if params && params[:force_limit]
                  remaining_records
                else
                  [query_limit, remaining_records, DEFAULT_MAX_RECORDS].compact.min
                end
  results = []
  return results unless max_records > 0

  Rails.logger.debug("Performing search", search_class: self.class.name, limit: max_records)
  keep_searching = true

  query = params[:query]
  query_type = params[:query_type]
  if handles_query_type?(query_type) and (term = extract_term(query)).present?
    Rails.logger.info " - Extracted search term: '#{term}'"
    begin
      results = perform_find(term, existing_results).limit(max_records).to_a
      if (sz = results.size) > 0
        remaining_records -= sz
        # Keep searching if we have not exhausted our max records and this is not a authoritative_search
      end
    rescue StandardError => e
      Rails.logger.error "Error performing quick search: #{e.message} on #{self.class.name} with term '#{term}'"
      ErrorReporting.error(e, 'Error performing quick search', { class: self.class.name, term: term })
    end
    keep_searching = (remaining_records.positive? and !authoritative_search)
  end

  if keep_searching and @fallback
    Rails.logger.info ' - Search continues, sending to fallback' unless results
    results += @fallback.find(params, remaining_records, results)
  end
  results.uniq
end

#handles_query_type?(query_type) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'app/queries/quick_search/base_quick_search.rb', line 63

def handles_query_type?(query_type)
  return true if query_type.blank?

  allowed = Array(query_type)
  allowed_classes = allowed.select { |t| t.is_a?(Class) || (t.is_a?(String) && t.include?('::')) }
  allowed_symbols = allowed.select { |t| t.is_a?(Symbol) || (t.is_a?(String) && !t.include?('::')) }

  # If any explicit classes are provided, only run those classes
  if allowed_classes.any?
    return allowed_classes.any? do |t|
      case t
      when Class
        t == self.class
      when String
        t == self.class.name
      else
        false
      end
    end
  end

  # Otherwise, honor friendly type symbols only for classes that declare support
  if allowed_symbols.any?
    return false unless self.class.respond_to?(:handles_types)

    supported = Array(self.class.handles_types).map(&:to_sym)
    return (supported & allowed_symbols.map(&:to_sym)).any?

  end

  false
end

#perform_find(*_args) ⇒ Object



45
46
47
# File 'app/queries/quick_search/base_quick_search.rb', line 45

def perform_find(*_args)
  raise 'Not implemented'
end

#query_limitObject



59
60
61
# File 'app/queries/quick_search/base_quick_search.rb', line 59

def query_limit
  nil
end