Class: Queries::Lead::Autocomplete

Inherits:
Query::Autocomplete show all
Defined in:
lib/queries/lead/autocomplete.rb

Instance Attribute Summary

Attributes inherited from Query::Autocomplete

#dynamic_limit, #project_id, #query_string

Attributes inherited from Query

#query_string, #terms

Instance Method Summary collapse

Methods inherited from Query::Autocomplete

#autocomplete_cached, #autocomplete_cached_wildcard_anywhere, #autocomplete_common_name_exact, #autocomplete_common_name_like, #autocomplete_exact_id, #autocomplete_exactly_named, #autocomplete_named, #autocomplete_ordered_wildcard_pieces_in_cached, #combine_or_clauses, #common_name_name, #common_name_table, #common_name_wild_pieces, #exactly_named, #fragments, #integers, #least_levenshtein, #match_wildcard_end_in_cached, #match_wildcard_in_cached, #named, #only_ids, #only_integers?, #parent, #parent_child_join, #parent_child_where, #pieces, #scope, #string_fragments, #wildcard_wrapped_integers, #wildcard_wrapped_years, #with_cached, #with_cached_like, #with_id, #with_project_id, #year_letter, #years

Methods inherited from Query

#alphabetic_strings, #alphanumeric_strings, base_name, #base_name, #base_query, #build_terms, #cached_facet, #end_wildcard, #levenshtein_distance, #match_ordered_wildcard_pieces_in_cached, #no_terms?, referenced_klass, #referenced_klass, #referenced_klass_except, #referenced_klass_intersection, #referenced_klass_union, #start_and_end_wildcard, #start_wildcard, #table, #wildcard_pieces

Constructor Details

#initialize(string, project_id: nil) ⇒ Autocomplete

Returns a new instance of Autocomplete.



5
6
7
# File 'lib/queries/lead/autocomplete.rb', line 5

def initialize(string, project_id: nil)
  super
end

Instance Method Details

#autocompleteArray

Returns:

  • (Array)


68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/queries/lead/autocomplete.rb', line 68

def autocomplete
  queries = updated_queries

  result = []

  queries.each do |q|
    result += q.to_a
    result.uniq!
    break if result.count > 19
  end
  result[0..19]
end

#autocomplete_description_contains_matchObject



22
23
24
25
26
27
# File 'lib/queries/lead/autocomplete.rb', line 22

def autocomplete_description_contains_match
  return nil if query_string.length < 2
  base_query
    .where('parent_id is null')
    .where('leads.description ilike ?', '%' + query_string + '%').limit(20)
end

#autocomplete_exact_origin_labelObject



29
30
31
# File 'lib/queries/lead/autocomplete.rb', line 29

def autocomplete_exact_origin_label
  base_query.where(origin_label: query_string).limit(20)
end

#autocomplete_matching_otu_name_startObject



33
34
35
36
37
# File 'lib/queries/lead/autocomplete.rb', line 33

def autocomplete_matching_otu_name_start
  return nil if query_string.length < 2
  base_query.joins(:otu)
    .where(otu_table[:name].matches(query_string + '%')).limit(20)
end

#autocomplete_matching_taxon_name_startObject



39
40
41
42
43
# File 'lib/queries/lead/autocomplete.rb', line 39

def autocomplete_matching_taxon_name_start
  return nil if query_string.length < 2
  base_query.joins(:taxon_name)
    .where(taxon_name_table[:cached].matches(query_string + '%')).limit(20)
end

#autocomplete_text_contains_matchObject



17
18
19
20
# File 'lib/queries/lead/autocomplete.rb', line 17

def autocomplete_text_contains_match
  return nil if query_string.length < 2
  base_query.where('leads.text ilike ?', '%' + query_string + '%').limit(20)
end

#otu_tableObject



9
10
11
# File 'lib/queries/lead/autocomplete.rb', line 9

def otu_table
  ::Otu.arel_table
end

#taxon_name_tableObject



13
14
15
# File 'lib/queries/lead/autocomplete.rb', line 13

def taxon_name_table
  ::TaxonName.arel_table
end

#updated_queriesObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/queries/lead/autocomplete.rb', line 45

def updated_queries
  queries = [
    autocomplete_text_contains_match,
    autocomplete_description_contains_match,
    autocomplete_exact_origin_label,
    autocomplete_matching_otu_name_start,
    autocomplete_matching_taxon_name_start
  ]

  queries.compact!

  return [] if queries.empty?

  updated_queries = []

  queries.each do |q|
    a = q.where(project_id:) if project_id.present?
    updated_queries.push a
  end
  updated_queries
end