Class: Queries::OtuAutocompleteQuery

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

Overview

Lots of optimization possible, at minimum this is nice for nested OR

Instance Attribute Summary

Attributes inherited from Query

#dynamic_limit, #project_id, #query_string, #terms

Instance Method Summary (collapse)

Methods inherited from Query

#alphabetic_strings, #autocomplete, #build_terms, #cached, #combine_or_clauses, #end_wildcard, #exactly_named, #fragments, #identifier_table, #initialize, #integers, #named, #no_digits, #only_ids, #only_integers?, #parent, #parent_child_join, #parent_child_where, #pieces, #result, #scope, #start_and_end_wildcard, #start_wildcard, #strings, #wildcard_wrapped_integers, #with_id, #with_identifier, #with_identifier_like, #with_project_id, #years

Constructor Details

This class inherits a constructor from Queries::Query

Instance Method Details

- (Scope) all

Returns:

  • (Scope)


36
37
38
39
# File 'lib/queries/otu_autocomplete_query.rb', line 36

def all 
  # For references, this is equivalent: Otu.eager_load(:taxon_name).where(where_sql) 
  Otu.includes(:taxon_name).where(where_sql).references(:taxon_names).order(name: :asc).limit(50).order('taxon_names.cached ASC')
end

- (Object) authorship



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/queries/otu_autocomplete_query.rb', line 59

def authorship
  parser = ScientificNameParser.new
  a = parser.parse(query_string)
  b = a[:scientificName]
  return nil if b.nil? or b[:details].nil?

  b[:details].each do |detail|
    detail.each do |k,v|
      if v[:authorship]
        return v[:authorship] 
      end
    end
  end
  nil
end

- (Object) or_clauses



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/queries/otu_autocomplete_query.rb', line 20

def or_clauses
  clauses = [
    named,
    taxon_name_named,
    taxon_name_author_year_matches,
    with_id
  ].compact

  a = clauses.shift
  clauses.each do |b|
    a = a.or(b)
  end
  a
end

- (Object) table



45
46
47
# File 'lib/queries/otu_autocomplete_query.rb', line 45

def table
  Otu.arel_table
end

- (Object) taxon_name_author_year_matches



53
54
55
56
57
# File 'lib/queries/otu_autocomplete_query.rb', line 53

def taxon_name_author_year_matches
  a = authorship
  return nil if a.nil?
  taxon_name_table[:cached_author_year].matches(a)
end

- (Object) taxon_name_named



49
50
51
# File 'lib/queries/otu_autocomplete_query.rb', line 49

def taxon_name_named
  taxon_name_table[:name].matches_any(terms)
end

- (Object) taxon_name_table



41
42
43
# File 'lib/queries/otu_autocomplete_query.rb', line 41

def taxon_name_table
  TaxonName.arel_table
end

- (Object) where_sql



16
17
18
# File 'lib/queries/otu_autocomplete_query.rb', line 16

def where_sql
  with_project_id.and(or_clauses).to_sql
end