Class: Queries::BiologicalCollectionObjectAutocompleteQuery

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

Instance Attribute Summary

Attributes inherited from Query

#dynamic_limit, #project_id, #query_string, #terms

Instance Method Summary (collapse)

Methods inherited from Query

#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, #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

- (Object) all



22
23
24
25
26
27
# File 'lib/queries/biological_collection_object_autocomplete_query.rb', line 22

def all 
  CollectionObject.
    includes(:identifiers, taxon_determinations: [ otu: [:taxon_name], determiners: [] ] ).
    where(where_sql).
    references(:taxon_names, :otus, :identifiers)
end

- (Array) autocomplete

Returns results for autocomplete

Returns:

  • (Array)

    results for autocomplete



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/queries/biological_collection_object_autocomplete_query.rb', line 31

def autocomplete
  queries = []
  queries.push CollectionObject.includes(:identifiers, taxon_determinations: [otu: :taxon_name]).joins(:identifiers).where(with_identifier.to_sql).references(:identifiers).limit(2) if with_identifier
  queries.push CollectionObject.includes(:identifiers, taxon_determinations: [otu: :taxon_name]).joins(:identifiers).where(with_identifier_like.to_sql).references(:identifiers).order('identifiers.cached ASC').limit(10) if with_identifier_like
  queries.push CollectionObject.includes(:identifiers, taxon_determinations: [otu: :taxon_name]).where(with_id.to_sql).limit(1) if with_id
  queries.push CollectionObject.includes(:identifiers, taxon_determinations: [otu: :taxon_name]).joins(taxon_determinations: [:otu]).where(otu_determined_as.to_sql).references(:taxon_determinations, :otus).order('otus.name ASC').limit(10) if otu_determined_as
  queries.push CollectionObject.includes(:identifiers, taxon_determinations: [otu: :taxon_name]).joins(taxon_determinations: [otu: :taxon_name]).where(taxon_name_determined_as.to_sql).references(:taxon_determinations, :otus, :taxon_names).order('taxon_names.cached ASC').limit(10) if taxon_name_determined_as  

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

  result = []
  queries.each do |q|
    result += q.to_a
    result.uniq!
    break if result.count > 19
  end

  result[0..19]
end

- (Object) or_clauses



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/queries/biological_collection_object_autocomplete_query.rb', line 4

def or_clauses
  combine_or_clauses(
    [
      with_id, 
      with_identifier,
      with_identifier_like,
      otu_determined_as,
      taxon_name_determined_as
    ]
  )
end

- (Arel::Nodes::Grouping) otu_determined_as

Returns:

  • (Arel::Nodes::Grouping)


68
69
70
# File 'lib/queries/biological_collection_object_autocomplete_query.rb', line 68

def otu_determined_as 
  otu_table[:name].matches_any(terms)
end

- (Object) otu_table



59
60
61
# File 'lib/queries/biological_collection_object_autocomplete_query.rb', line 59

def otu_table
  Otu.arel_table
end

- (Object) table



63
64
65
# File 'lib/queries/biological_collection_object_autocomplete_query.rb', line 63

def table
  CollectionObject.arel_table
end

- (Object) taxon_name_determined_as



72
73
74
# File 'lib/queries/biological_collection_object_autocomplete_query.rb', line 72

def taxon_name_determined_as 
  taxon_name_table[:name].matches_any(terms).or(taxon_name_table[:cached].matches_any(terms) )
end

- (Object) taxon_name_table



55
56
57
# File 'lib/queries/biological_collection_object_autocomplete_query.rb', line 55

def taxon_name_table
  TaxonName.arel_table
end

- (Object) where_sql



16
17
18
19
20
# File 'lib/queries/biological_collection_object_autocomplete_query.rb', line 16

def where_sql
  a = or_clauses
  a = a.and(with_project_id) if project_id
  a.to_sql
end