Class: Queries::AssertedDistribution::Autocomplete

Inherits:
Query
  • Object
show all
Defined in:
lib/queries/asserted_distribution/autocomplete.rb

Instance Attribute Summary

Attributes inherited from Query

#dynamic_limit, #options, #project_id, #query_string, #terms

Instance Method Summary collapse

Methods inherited from Query

#alphabetic_strings, #autocomplete_cached, #autocomplete_cached_wildcard_anywhere, #autocomplete_exact_id, #autocomplete_exactly_named, #autocomplete_named, #autocomplete_ordered_wildcard_pieces_in_cached, #autocomplete_start_date, #build_terms, #cached, #combine_or_clauses, #end_wildcard, #exactly_named, #fragments, #integers, #match_ordered_wildcard_pieces_in_cached, #match_wildcard_end_in_cached, #match_wildcard_in_cached, #named, #no_digits, #only_ids, #only_integers?, #parent, #parent_child_join, #parent_child_where, #pieces, #result, #scope, #simple_date, #start_and_end_wildcard, #start_wildcard, #wildcard_pieces, #wildcard_wrapped_integers, #wildcard_wrapped_years, #with_cached, #with_cached_like, #with_id, #with_project_id, #with_start_date, #year_letter, #years

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)


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

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: 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



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

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



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

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

#autocomplete_matching_sourceObject

Dubious use



42
43
44
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 42

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

#autocomplete_matching_taxon_nameObject



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

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

#base_queryObject



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

def base_query
  ::AssertedDistribution.select('asserted_distributions.*')
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