Class: Queries::Image::Autocomplete

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

#initialize(string, project_id: nil) ⇒ Autocomplete

Returns a new instance of Autocomplete

Parameters:

  • args (Hash)


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

def initialize(string, project_id: nil)
  super
end

Instance Method Details

#and_clausesArel:Nodes?

Returns:

  • (Arel:Nodes, nil)


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

def and_clauses
  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)


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
92
# File 'lib/queries/image/autocomplete.rb', line 66

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



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

def autocomplete_depicting_otu_by_otu_name
  o = otu_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



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

def autocomplete_depicting_otu_by_taxon_name
  o = taxon_name_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

#base_queryScope

Returns:

  • (Scope)


117
118
119
# File 'lib/queries/image/autocomplete.rb', line 117

def base_query
  ::Image.select('images.*')
end

#depiction_tableObject



130
131
132
# File 'lib/queries/image/autocomplete.rb', line 130

def depiction_table
  ::Depiction.arel_table
end

#or_andArel:Nodes

Returns:

  • (Arel:Nodes)


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

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)


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

def or_clauses
  clauses = []

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

  clauses.compact!

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

#otu_tableObject



126
127
128
# File 'lib/queries/image/autocomplete.rb', line 126

def otu_table
  ::Otu.arel_table
end

#tableArel::Table

Returns:

  • (Arel::Table)


122
123
124
# File 'lib/queries/image/autocomplete.rb', line 122

def table
  ::Image.arel_table
end

#taxon_name_tableObject



134
135
136
# File 'lib/queries/image/autocomplete.rb', line 134

def taxon_name_table 
  ::TaxonName.arel_table
end

#where_sqlString

Returns:

  • (String)


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

def where_sql
  with_project_id.and(or_and).to_sql
end