Class: QuickSearch::BaseQuickSearch

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

Overview

Query object: base quick search.

Constant Summary collapse

DEFAULT_MAX_RECORDS =

Default max records.

30

Instance Method Summary collapse

Constructor Details

#initialize(fallback) ⇒ BaseQuickSearch

Returns a new instance of BaseQuickSearch.



7
8
9
# File 'app/queries/quick_search/base_quick_search.rb', line 7

def initialize(fallback)
  @fallback = fallback
end

Instance Method Details

#authoritative_searchObject



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

def authoritative_search
  false
end

#extract_term(query) ⇒ Object



52
53
54
55
56
# File 'app/queries/quick_search/base_quick_search.rb', line 52

def extract_term(query)
  return if query.blank?

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

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



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

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) && (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, message: 'Error performing quick search', class: self.class.name, term: term)
    end
    keep_searching = (remaining_records.positive? and !authoritative_search)
  end

  if keep_searching && @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)


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

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.exclude?('::')) }

  # 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.intersect?(allowed_symbols.map(&:to_sym))

  end

  false
end

#perform_find(*_args) ⇒ Object



48
49
50
# File 'app/queries/quick_search/base_quick_search.rb', line 48

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

#query_limitObject



62
63
64
# File 'app/queries/quick_search/base_quick_search.rb', line 62

def query_limit
  nil
end