Class: Queries::Image::Autocomplete

Inherits:
Query::Autocomplete show all
Defined in:
lib/queries/image/autocomplete.rb

Instance Attribute Summary

Attributes inherited from Query::Autocomplete

#dynamic_limit, #project_id, #query_string

Attributes inherited from Query

#query_string, #terms

Instance Method Summary collapse

Methods inherited from Query::Autocomplete

#autocomplete_cached, #autocomplete_cached_wildcard_anywhere, #autocomplete_common_name_exact, #autocomplete_common_name_like, #autocomplete_exact_id, #autocomplete_exactly_named, #autocomplete_named, #autocomplete_ordered_wildcard_pieces_in_cached, #cached_facet, #combine_or_clauses, #common_name_name, #common_name_table, #common_name_wild_pieces, #exactly_named, #fragments, #integers, #least_levenshtein, #match_wildcard_end_in_cached, #match_wildcard_in_cached, #named, #only_ids, #only_integers?, #parent, #parent_child_join, #parent_child_where, #pieces, #scope, #string_fragments, #wildcard_wrapped_integers, #wildcard_wrapped_years, #with_cached, #with_cached_like, #with_id, #with_project_id, #year_letter, #years

Methods inherited from Query

#alphabetic_strings, #alphanumeric_strings, base_name, #base_name, #base_query, #build_terms, #cached_facet, #end_wildcard, #levenshtein_distance, #match_ordered_wildcard_pieces_in_cached, #no_terms?, referenced_klass, #referenced_klass, #referenced_klass_except, #referenced_klass_intersection, #referenced_klass_union, #start_and_end_wildcard, #start_wildcard, #table, #wildcard_pieces

Constructor Details

#initialize(string, project_id: nil) ⇒ Autocomplete

Returns a new instance of Autocomplete.

Parameters:

  • args (Hash)


6
7
8
# File 'lib/queries/image/autocomplete.rb', line 6

def initialize(string, project_id: nil)
  super
end

Instance Method Details

#and_clausesArel:Nodes?

Returns:

  • (Arel:Nodes, nil)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/queries/image/autocomplete.rb', line 29

def and_clauses
  return []
  # clauses = [
  #   #  valid_state,
  #   #  is_type,
  #   #  with_parent_id,
  #   #  with_nomenclature_group
  # ].compact
  #
  # return nil if clauses.nil?
  #
  # a = clauses.shift
  # clauses.each do |b|
  #   a = a.and(b)
  # end
  # a
end

#autocompleteArray

Returns:

  • (Array)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/queries/image/autocomplete.rb', line 65

def autocomplete
  queries = [
    autocomplete_exact_id,
    autocomplete_identifier_identifier_exact,
    autocomplete_identifier_cached_like,
    autocomplete_depicting_otu_by_otu_name,
    autocomplete_depicting_otu_by_taxon_name
  ]
  queries.compact!

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

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

  result[0..40]
end

#autocomplete_depicting_otu_by_otu_nameObject



93
94
95
96
97
98
99
100
101
102
# File 'lib/queries/image/autocomplete.rb', line 93

def autocomplete_depicting_otu_by_otu_name
  o = ::Otu.arel_table[:name].matches_any(terms)

  ::Image.
    includes(:otus).
    joins(:otus).
    where(o.to_sql).
    references(:depictions, :otus).
    order('otus.name ASC').limit(20)
end

#autocomplete_depicting_otu_by_taxon_nameObject



104
105
106
107
108
109
110
111
112
113
# File 'lib/queries/image/autocomplete.rb', line 104

def autocomplete_depicting_otu_by_taxon_name
  o = ::TaxonName.arel_table[:cached].matches_any(terms)

  ::Image.
    includes(:taxon_names).
    joins(:taxon_names).
    where(o.to_sql).
    references(:depictions, :taxon_names).
    order('taxon_names.cached ASC').limit(20)
end

#or_andArel:Nodes

Returns:

  • (Arel:Nodes)


48
49
50
51
52
53
54
55
56
57
# File 'lib/queries/image/autocomplete.rb', line 48

def or_and
  a = or_clauses
  b = and_clauses

  if a && b
    a.and(b)
  else
    a
  end
end

#or_clausesArel:Nodes

Returns:

  • (Arel:Nodes)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/queries/image/autocomplete.rb', line 11

def or_clauses
  return []
  # clauses = []

  #  clauses += [
  #    #  only_ids,
  #  ] unless exact

  #  clauses.compact!

  #  a = clauses.shift
  #  clauses.each do |b|
  #    a = a.or(b)
  #  end
  #  a
end

#where_sqlString

Returns:

  • (String)


60
61
62
# File 'lib/queries/image/autocomplete.rb', line 60

def where_sql
  with_project_id.and(or_and).to_sql
end