Class: Queries::CollectionObject::Autocomplete

Inherits:
Query
  • Object
show all
Defined in:
lib/queries/collection_object/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_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

- (Autocomplete) initialize(string, project_id: nil)

Returns a new instance of Autocomplete



7
8
9
# File 'lib/queries/collection_object/autocomplete.rb', line 7

def initialize(string, project_id: nil)
  super
end

Instance Method Details

- (Array) autocomplete

Returns TODO: optimize limits

Returns:

  • (Array)

    TODO: optimize limits



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/queries/collection_object/autocomplete.rb', line 52

def autocomplete
  queries = [
    autocomplete_identifier_cached_exact,
    autocomplete_identifier_identifier_exact,
    autocomplete_exact_id, 
    autocomplete_taxon_name_determined_as,
    autocomplete_otu_determined_as,
    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

- (Object) autocomplete_otu_determined_as



41
42
43
44
45
46
47
48
# File 'lib/queries/collection_object/autocomplete.rb', line 41

def autocomplete_otu_determined_as
  t = otu_table[:name].matches_any(terms)

  ::CollectionObject.
    joins(taxon_determinations: [:otu]).
    where(t.to_sql).references(:taxon_determinations, :otus).
    order('otus.name ASC').limit(10) 
end

- (Object) autocomplete_taxon_name_determined_as



30
31
32
33
34
35
36
37
38
39
# File 'lib/queries/collection_object/autocomplete.rb', line 30

def autocomplete_taxon_name_determined_as
  t = taxon_name_table[:name].matches_any(terms).or(taxon_name_table[:cached].matches_any(terms) )

  ::CollectionObject.
    includes(:identifiers, taxon_determinations: [otu: :taxon_name]).
    joins(taxon_determinations: [:otu]).
    where(t.to_sql).
    references(:taxon_determinations, :otus, :taxon_names).
    order('otus.name ASC').limit(10)
end

- (Object) base_query



11
12
13
# File 'lib/queries/collection_object/autocomplete.rb', line 11

def base_query
  ::CollectionObject.select('collection_objects.*')
end

- (Arel::Table) otu_table

Returns:

  • (Arel::Table)


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

def otu_table
  ::Otu.arel_table
end

- (Arel::Table) table

Returns:

  • (Arel::Table)


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

def table
  ::CollectionObject.arel_table
end

- (Arel::Table) taxon_name_table

Returns:

  • (Arel::Table)


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

def taxon_name_table
  ::TaxonName.arel_table
end