Class: Queries::AssertedDistribution::Autocomplete

Inherits:
Query::Autocomplete show all
Defined in:
lib/queries/asserted_distribution/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, #cached_facet, #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/asserted_distribution/autocomplete.rb', line 5

def initialize(string, project_id: nil)
  super
end

Instance Method Details

#autocompleteArray

Returns:

  • (Array)


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
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 43

def autocomplete
  queries = [
    autocomplete_matching_source,
    autocomplete_matching_otu_name,
    autocomplete_matching_taxon_name,
    autocomplete_matching_geographic_area,
  ]

  queries = queries.compact

  return [] if queries.empty?
  updated_queries = []

  queries.each_with_index do |q ,i|
    a = q.where(asserted_distributions: {project_id:})
    updated_queries[i] = a
  end

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

#autocomplete_matching_geographic_areaObject



33
34
35
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 33

def autocomplete_matching_geographic_area
  base_query.joins(:geographic_area).where( geographic_area_table[:name].matches(query_string + '%')   ).limit(50)
end

#autocomplete_matching_otu_nameObject



25
26
27
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 25

def autocomplete_matching_otu_name
  base_query.joins(:otu).where( otu_table[:name].matches(query_string + '%')   ).limit(20)
end

#autocomplete_matching_sourceObject

Dubious use



38
39
40
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 38

def autocomplete_matching_source
  base_query.joins(:sources).where( source_table[:cached].matches('%' + query_string + '%') ).limit(5)
end

#autocomplete_matching_taxon_nameObject



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

def autocomplete_matching_taxon_name
  base_query.joins(:taxon_name).where( taxon_name_table[:cached].matches(query_string + '%')   ).limit(20)
end

#geographic_area_tableObject



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

def geographic_area_table
  ::GeographicArea.arel_table
end

#otu_tableObject



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

def otu_table
  ::Otu.arel_table
end

#source_tableObject



21
22
23
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 21

def source_table
  ::Source.arel_table
end

#taxon_name_tableObject



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

def taxon_name_table
  ::TaxonName.arel_table
end