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

#alphabetic_strings, #autocomplete_cached, #autocomplete_cached_wildcard_anywhere, #autocomplete_ordered_wildcard_pieces_in_cached, #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, #start_and_end_wildcard, #start_wildcard, #wildcard_pieces, #wildcard_wrapped_integers, #with_id, #with_identifier, #with_identifier_like, #with_project_id, #year_letter, #years

Constructor Details

This class inherits a constructor from Queries::Query

Instance Method Details

- (Scope) all

Returns:

  • (Scope)


25
26
27
28
29
30
# File 'lib/queries/biological_collection_object_autocomplete_query.rb', line 25

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



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

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

- (Array) or_clauses

Returns:

  • (Array)


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

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)


74
75
76
# File 'lib/queries/biological_collection_object_autocomplete_query.rb', line 74

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

- (Arel::Table) otu_table

Returns:

  • (Arel::Table)


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

def otu_table
  ::Otu.arel_table
end

- (Arel::Table) table

Returns:

  • (Arel::Table)


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

def table
  ::CollectionObject.arel_table
end

- (Arel::Nodes::Grouping) taxon_name_determined_as

Returns:

  • (Arel::Nodes::Grouping)


79
80
81
# File 'lib/queries/biological_collection_object_autocomplete_query.rb', line 79

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

- (Arel::Table) taxon_name_table

Returns:

  • (Arel::Table)


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

def taxon_name_table
  ::TaxonName.arel_table
end

- (String) where_sql

Returns:

  • (String)


18
19
20
21
22
# File 'lib/queries/biological_collection_object_autocomplete_query.rb', line 18

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