Class: Queries::OtuAutocompleteQuery

Inherits:
Query
  • Object
show all
Includes:
Arel::Nodes
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

#build_terms, #end_wildcard, #fragments, #identifier_table, #initialize, #integers, #named, #no_digits, #only_ids, #only_integers?, #parent, #parent_child_join, #parent_child_where, #result, #scope, #start_and_end_wildcard, #start_wildcard, #strings, #wildcard_wrapped_integers, #with_id, #with_identifier_like, #with_project_id, #years

Constructor Details

This class inherits a constructor from Queries::Query

Instance Method Details

- (Scope) all

Returns:

  • (Scope)


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

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



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

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



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

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



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

def table
  Otu.arel_table
end

- (Object) taxon_name_author_year_matches



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

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



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

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

- (Object) taxon_name_table



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

def taxon_name_table
  TaxonName.arel_table
end

- (Object) where_sql



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

def where_sql
  with_project_id.and(or_clauses).to_sql
end