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_cached, #autocomplete_cached_wildcard_anywhere, #autocomplete_exact_id, #autocomplete_ordered_wildcard_pieces_in_cached, #autocomplete_start_date, #build_terms, #cached, #combine_or_clauses, #end_wildcard, #exactly_named, #fragments, #initialize, #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

This class inherits a constructor from Queries::Query

Instance Method Details

- (String) authorship

Returns:

  • (String)


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/queries/otu/autocomplete.rb', line 99

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

- (Array) autocomplete

Returns TODO: optimize limits

Returns:

  • (Array)

    TODO: optimize limits



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/queries/otu/autocomplete.rb', line 32

def autocomplete
  queries = [
    autocomplete_or_clauses,
    autocomplete_identifier_cached_exact,
    autocomplete_identifier_identifier_exact,
    autocomplete_identifier_cached_like,
  ]
    
  queries.compact!

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

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

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

- (Scope) autocomplete_or_clauses

Returns:

  • (Scope)


82
83
84
# File 'lib/queries/otu/autocomplete.rb', line 82

def autocomplete_or_clauses
  ::Otu.includes(:taxon_name).where(where_sql).references(:taxon_names).order(name: :asc).limit(50).order('taxon_names.cached ASC')
end

- (Object) base_query



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

def base_query
  ::Otu.select('otus.*')
end

- (Scope) or_clauses

Returns:

  • (Scope)


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

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)


26
27
28
# File 'lib/queries/otu/autocomplete.rb', line 26

def table
  ::Otu.arel_table
end

- (Arel::Nodes::Matches) taxon_name_author_year_matches

Returns:

  • (Arel::Nodes::Matches)


92
93
94
95
96
# File 'lib/queries/otu/autocomplete.rb', line 92

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)


87
88
89
# File 'lib/queries/otu/autocomplete.rb', line 87

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

- (Arel::Table) taxon_name_table

Returns:

  • (Arel::Table)


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

def taxon_name_table
  ::TaxonName.arel_table
end

- (Scope) where_sql

Returns:

  • (Scope)


77
78
79
# File 'lib/queries/otu/autocomplete.rb', line 77

def where_sql
  with_project_id.and(or_clauses).to_sql
end