Class: Otu::Autocomplete

Inherits:
Queries::Query show all
Defined in:
lib/queries/otu/autocomplete.rb

Overview

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

Instance Attribute Summary

Attributes inherited from Queries::Query

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

Instance Method Summary (collapse)

Methods inherited from Queries::Query

#alphabetic_strings, #autocomplete, #autocomplete_cached, #autocomplete_cached_wildcard_anywhere, #autocomplete_identifier_cached_exact, #autocomplete_identifier_cached_like, #autocomplete_ordered_wildcard_pieces_in_cached, #autocomplete_start_date, #build_terms, #cached, #combine_or_clauses, #end_wildcard, #exactly_named, #fragments, #identifier_table, #initialize, #integers, #match_ordered_wildcard_pieces_in_cached, #match_wildcard_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_id, #with_identifier, #with_identifier_like, #with_project_id, #with_start_date, #year_letter, #years

Constructor Details

This class inherits a constructor from Queries::Query

Instance Method Details

- (Scope) all

Returns:

  • (Scope)


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

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

- (String) authorship

Returns:

  • (String)


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

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_value do |v|
      if v.kind_of?(Hash) && v[:authorship]
        return v[:authorship]
      end
    end
  end
  nil
end

- (Scope) or_clauses

Returns:

  • (Scope)


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

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

- (Arel::Table) table

Returns:

  • (Arel::Table)


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

def table
  ::Otu.arel_table
end

- (Arel::Nodes::Matches) taxon_name_author_year_matches

Returns:

  • (Arel::Nodes::Matches)


59
60
61
62
63
# File 'lib/queries/otu/autocomplete.rb', line 59

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

- (Arel::Nodes::Matches) taxon_name_named

Returns:

  • (Arel::Nodes::Matches)


54
55
56
# File 'lib/queries/otu/autocomplete.rb', line 54

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

- (Arel::Table) taxon_name_table

Returns:

  • (Arel::Table)


44
45
46
# File 'lib/queries/otu/autocomplete.rb', line 44

def taxon_name_table
  ::TaxonName.arel_table
end

- (Scope) where_sql

Returns:

  • (Scope)


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

def where_sql
  with_project_id.and(or_clauses).to_sql
end