Class: Queries::TaxonName::Autocomplete

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

Instance Attribute Summary (collapse)

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_identifier_cached_exact, #autocomplete_identifier_cached_like, #autocomplete_ordered_wildcard_pieces_in_cached, #autocomplete_start_date, #build_terms, #cached, #combine_or_clauses, #end_wildcard, #exactly_named, #fragments, #identifier_table, #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, #simple_date, #start_and_end_wildcard, #start_wildcard, #wildcard_pieces, #wildcard_wrapped_integers, #wildcard_wrapped_years, #with_id, #with_identifier, #with_identifier_like, #with_project_id, #with_start_date, #year_letter, #years

Constructor Details

- (Autocomplete) initialize(string, project_id: nil, valid: nil, nomenclature_group: [], type: [], parent_id: [], exact: false)

Returns a new instance of Autocomplete

Parameters:

  • args (Hash)


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

def initialize(string, project_id: nil, valid: nil, nomenclature_group: [], type: [], parent_id: [], exact: false)
  @nomenclature_group = nomenclature_group
  @valid = valid == 'true' ? true : (valid == 'false' ? false : nil)
  @type = type
  @parent_id = parent_id
  @exact = exact == 'true' ? true : (exact == 'false' ? false : nil)
  super
end

Instance Attribute Details

- (Boolean) exact

Returns &exact=<“true”|“false”> if 'true' then only #name = query_string results are returned (no fuzzy matching)

Returns:

  • (Boolean)

    &exact=<“true”|“false”> if 'true' then only #name = query_string results are returned (no fuzzy matching)



29
30
31
# File 'lib/queries/taxon_name/autocomplete.rb', line 29

def exact
  @exact
end

- (Array) nomenclature_group

and_clause

Returns:

  • (Array)


8
9
10
# File 'lib/queries/taxon_name/autocomplete.rb', line 8

def nomenclature_group
  @nomenclature_group
end

- (Array) parent_id

Returns &parent_id[]=<int>&parent_id=<other_int> etc.

Returns:

  • (Array)

    &parent_id[]=<int>&parent_id=<other_int> etc.



24
25
26
# File 'lib/queries/taxon_name/autocomplete.rb', line 24

def parent_id
  @parent_id
end

- (Array) type

Returns &type[]=<Protonym, Combination, Hybrid, etc.>&type[]=<other type> etc.

Returns:

  • (Array)

    &type[]=<Protonym, Combination, Hybrid, etc.>&type[]=<other type> etc.



20
21
22
# File 'lib/queries/taxon_name/autocomplete.rb', line 20

def type
  @type
end

- (Boolean?) valid

Returns &valid=<“true”|“false”>

if 'true'  then id == cached_valid_taxon_name_id
if 'false' then id != cached_valid_taxon_name
if nil   then no check made, i.e. all names

string is converted to Boolean here

Returns:

  • (Boolean, nil)

    &valid=<“true”|“false”>

    if 'true'  then id == cached_valid_taxon_name_id
    if 'false' then id != cached_valid_taxon_name
    if nil   then no check made, i.e. all names

    string is converted to Boolean here



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

def valid
  @valid
end

Instance Method Details

- (Scope) all

Returns:

  • (Scope)


132
133
134
135
136
137
138
# File 'lib/queries/taxon_name/autocomplete.rb', line 132

def all
  ::TaxonName.select('taxon_names.*, char_length(taxon_names.cached)').
    includes(:ancestor_hierarchies).
    where(where_sql).
    references(:taxon_name_hierarchies).
    limit(dynamic_limit).order('char_length(taxon_names.cached), taxon_names.cached').distinct.all
end

- (Arel:Nodes?) and_clauses

Returns:

  • (Arel:Nodes, nil)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/queries/taxon_name/autocomplete.rb', line 63

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

- (Array) autocomplete

rubocop:disable Metrics/MethodLength

Returns:

  • (Array)


249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/queries/taxon_name/autocomplete.rb', line 249

def autocomplete
  z = genus_species

  queries = [
    autocomplete_exact_cached,
    autocomplete_exact_cached_original_combination,
    autocomplete_exact_name_and_year,
    autocomplete_top_name,
    autocomplete_top_cached,
    autocomplete_top_cached_subgenus, # not tested
    autocomplete_genus_species1(z), # not tested
    autocomplete_genus_species2(z), # not tested
    autocomplete_cached_end_wildcard,
    autocomplete_cached_name_end_wildcard,
    autocomplete_cached_wildcard_whitespace,
    autocomplete_name_author_year_fragment,
    autocomplete_cached_author_year,
    autocomplete_wildcard_joined_strings,
    autocomplete_wildcard_author_year_joined_pieces
  ]
  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..19]
end

- (Scope) autocomplete_cached_author_year

Returns:

  • (Scope)


228
229
230
231
# File 'lib/queries/taxon_name/autocomplete.rb', line 228

def autocomplete_cached_author_year
  a = table[:cached_author_year].matches("#{query_string.gsub(/[,\s]/, '%')}")
  base_query.where(a.to_sql).order('cached ASC').limit(20)
end

- (Scope) autocomplete_cached_end_wildcard

Returns:

  • (Scope)


199
200
201
202
# File 'lib/queries/taxon_name/autocomplete.rb', line 199

def autocomplete_cached_end_wildcard
  a = table[:cached].matches("#{query_string}%")
  base_query.where(a.to_sql).limit(20)
end

- (Scope) autocomplete_cached_name_end_wildcard

Returns:

  • (Scope)


205
206
207
208
# File 'lib/queries/taxon_name/autocomplete.rb', line 205

def autocomplete_cached_name_end_wildcard
  a = table[:name].matches("#{query_string}%")
  base_query.where(a.to_sql).limit(20)
end

- (Scope) autocomplete_cached_wildcard_whitespace

Returns:

  • (Scope)


211
212
213
214
# File 'lib/queries/taxon_name/autocomplete.rb', line 211

def autocomplete_cached_wildcard_whitespace
  a = table[:cached].matches("#{query_string.gsub('. ', ' ').gsub(' ', '%')}")
  base_query.where(a.to_sql).limit(20)
end

- (Scope) autocomplete_exact_cached

Returns:

  • (Scope)


141
142
143
144
# File 'lib/queries/taxon_name/autocomplete.rb', line 141

def autocomplete_exact_cached
  a = table[:cached].eq(query_string)
  base_query.where(a.to_sql).order('cached_author_year ASC').limit(20)
end

- (Scope) autocomplete_exact_cached_original_combination

Returns:

  • (Scope)


147
148
149
150
# File 'lib/queries/taxon_name/autocomplete.rb', line 147

def autocomplete_exact_cached_original_combination
  a = table[:cached_original_combination].eq(query_string)
  base_query.where(a.to_sql).order('cached_author_year ASC').limit(20)
end

- (Scope) autocomplete_exact_name_and_year

Returns:

  • (Scope)


153
154
155
156
157
158
159
160
161
162
# File 'lib/queries/taxon_name/autocomplete.rb', line 153

def autocomplete_exact_name_and_year
  a = alphabetic_strings
  b = years
  if a.size == 1 && !b.empty?
    a = table[:name].eq(a.first).and(table[:cached_author_year].matches_any(wildcard_wrapped_years))
    base_query.where(a.to_sql).limit(10)
  else
    nil
  end
end

- (Scope) autocomplete_genus_species1(result)

Parameters:

  • result (String)

Returns:

  • (Scope)


184
185
186
187
188
# File 'lib/queries/taxon_name/autocomplete.rb', line 184

def autocomplete_genus_species1(result)
  return nil if result.nil?
  a = table[:cached].matches(result)
  base_query.where(a.to_sql).order('type DESC, cached ASC').limit(8)
end

- (Scope) autocomplete_genus_species2(result)

Parameters:

  • result (String)

Returns:

  • (Scope)


192
193
194
195
196
# File 'lib/queries/taxon_name/autocomplete.rb', line 192

def autocomplete_genus_species2(result)
  return nil if result.nil?
  a = table[:cached].matches(result + '%')
  base_query.where(a.to_sql).order('type DESC, cached ASC').limit(8)
end

- (Scope?) autocomplete_name_author_year_fragment

Returns:

  • (Scope, nil)


217
218
219
220
221
222
223
224
225
# File 'lib/queries/taxon_name/autocomplete.rb', line 217

def autocomplete_name_author_year_fragment
  f = fragments
  if f.size == 2
    a = table[:name].matches(f[0]).and(table[:cached_author_year].matches(f[1]))
    base_query.where(a.to_sql).limit(20)
  else
    nil
  end
end

- (Scope) autocomplete_top_cached

Returns:

  • (Scope)


171
172
173
174
# File 'lib/queries/taxon_name/autocomplete.rb', line 171

def autocomplete_top_cached
  a = table[:cached].matches("#{query_string}")
  base_query.where(a.to_sql).limit(1)
end

- (Scope) autocomplete_top_cached_subgenus

Returns:

  • (Scope)


177
178
179
180
# File 'lib/queries/taxon_name/autocomplete.rb', line 177

def autocomplete_top_cached_subgenus
  a = table[:cached].matches("%(#{query_string})")
  base_query.where(a.to_sql).limit(1)
end

- (Scope) autocomplete_top_name

Returns:

  • (Scope)


165
166
167
168
# File 'lib/queries/taxon_name/autocomplete.rb', line 165

def autocomplete_top_name
  a = table[:name].eq(query_string)
  base_query.where(a.to_sql).order('cached_author_year ASC').limit(20)
end

- (Scope?) autocomplete_wildcard_author_year_joined_pieces

Returns:

  • (Scope, nil)


234
235
236
237
238
# File 'lib/queries/taxon_name/autocomplete.rb', line 234

def autocomplete_wildcard_author_year_joined_pieces
  return nil if pieces.empty?
  a = table[:cached_author_year].matches("%#{pieces.join('%')}%")
  base_query.where(a.to_sql).order('cached ASC').limit(20)
end

- (Scope?) autocomplete_wildcard_joined_strings

Returns:

  • (Scope, nil)


241
242
243
244
245
# File 'lib/queries/taxon_name/autocomplete.rb', line 241

def autocomplete_wildcard_joined_strings
  return nil if alphabetic_strings.empty?
  a = table[:cached].matches("%#{alphabetic_strings.join('%')}%")
  base_query.where(a.to_sql).limit(1)
end

- (Scope) base_query

Returns:

  • (Scope)


308
309
310
311
312
# File 'lib/queries/taxon_name/autocomplete.rb', line 308

def base_query
  ::TaxonName.select('taxon_names.*, char_length(taxon_names.cached)').
    includes(:ancestor_hierarchies).
    order(Arel.sql('char_length(cached), cached ASC'))
end

- (String?) genus_species

Returns parse and only return what is assumed to be genus/species, with a wildcard in front

Returns:

  • (String, nil)

    parse and only return what is assumed to be genus/species, with a wildcard in front



292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/queries/taxon_name/autocomplete.rb', line 292

def genus_species
  p = TaxonWorks::Vendor::Biodiversity::Result.new
  p.name = query_string
  r = p.parse

  a = p.genus
  b = p.species

  if a && b
    a + '%' + b
  else
    nil
  end
end

- (Arel::Nodes::<>?) is_type

and clause

Returns:

  • (Arel::Nodes::<>, nil)


106
107
108
109
# File 'lib/queries/taxon_name/autocomplete.rb', line 106

def is_type
  return nil if type.empty?
  table[:type].eq_any(type)
end

- (Arel:Nodes) or_and

Returns:

  • (Arel:Nodes)


81
82
83
84
85
86
87
88
89
90
# File 'lib/queries/taxon_name/autocomplete.rb', line 81

def or_and
  a = or_clauses
  b = and_clauses

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

- (Arel:Nodes) or_clauses

Returns:

  • (Arel:Nodes)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/queries/taxon_name/autocomplete.rb', line 42

def or_clauses
  clauses = []

  clauses.push exactly_named if exact

  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

- (Arel::Table) table

Returns:

  • (Arel::Table)


315
316
317
# File 'lib/queries/taxon_name/autocomplete.rb', line 315

def table
  ::TaxonName.arel_table
end

- (Arel::Table) taxon_name_hierarchies_table

Returns:

  • (Arel::Table)


320
321
322
# File 'lib/queries/taxon_name/autocomplete.rb', line 320

def taxon_name_hierarchies_table
  Arel::Table.new('taxon_name_hierarchies')
end

- (Arel::Nodes::<>?) valid_state

and clause

Returns:

  • (Arel::Nodes::<>, nil)


99
100
101
102
# File 'lib/queries/taxon_name/autocomplete.rb', line 99

def valid_state
  return nil if @valid.nil?
  valid ? table[:id].eq(table[:cached_valid_taxon_name_id]) : table[:id].not_eq(table[:cached_valid_taxon_name_id])
end

- (String) where_sql

Returns:

  • (String)


93
94
95
# File 'lib/queries/taxon_name/autocomplete.rb', line 93

def where_sql
  with_project_id.and(or_and).to_sql
end

- (Arel::Nodes::Matches) with_cached_author_year

Returns:

  • (Arel::Nodes::Matches)


325
326
327
# File 'lib/queries/taxon_name/autocomplete.rb', line 325

def with_cached_author_year
  table[:cached_author_year].matches_any(terms)
end

- (Arel::Nodes::Grouping?) with_nomenclature_group

Returns and clause

Returns:

  • (Arel::Nodes::Grouping, nil)

    and clause



120
121
122
123
# File 'lib/queries/taxon_name/autocomplete.rb', line 120

def with_nomenclature_group
  return nil if nomenclature_group.empty?
  table[:rank_class].matches_any(nomenclature_group)
end

- (Arel::Nodes::<>?) with_parent_id

and clause, limit to ancestors or [ids]

Returns:

  • (Arel::Nodes::<>, nil)


113
114
115
116
# File 'lib/queries/taxon_name/autocomplete.rb', line 113

def with_parent_id
  return nil if parent_id.empty?
  taxon_name_hierarchies_table[:ancestor_id].eq_any(parent_id)
end