Class: Queries::TaxonNameAutocompleteQuery

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

Instance Attribute Summary (collapse)

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

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

Returns a new instance of TaxonNameAutocompleteQuery



29
30
31
32
33
34
35
36
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 29

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)



27
28
29
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 27

def exact
  @exact
end

- (Object) nomenclature_group

and_clause



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

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.



22
23
24
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 22

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.



18
19
20
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 18

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



14
15
16
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 14

def valid
  @valid
end

Instance Method Details

- (Object) all



121
122
123
124
125
126
127
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 121

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

- (Object) and_clauses



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 58

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

- (Object) autocomplete



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 176

def autocomplete
  z = genus_species
  
  queries = [
    autocomplete_top_name,
    autocomplete_top_cached,
    autocomplete_genus_species1(z),
    autocomplete_genus_species2(z),
    autocomplete_cached_end_wildcard,
    autocomplete_cached_wildcard_whitespace,
    autocomplete_fragments,
    autocomplete_name_author_year
  ]

  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

- (Object) autocomplete_cached_end_wildcard



151
152
153
154
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 151

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

- (Object) autocomplete_cached_wildcard_whitespace



156
157
158
159
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 156

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

- (Object) autocomplete_fragments



161
162
163
164
165
166
167
168
169
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 161

def autocomplete_fragments
  f = fragments 
  if f.size == 2
    a = table[:cached].matches(f[0]).and(table[:cached_author_year].matches(f[1]))
    base_query.where(a.to_sql).order('char_length(cached), cached ASC').limit(20)
  else
    nil
  end
end

- (Object) autocomplete_genus_species1(result)



139
140
141
142
143
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 139

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

- (Object) autocomplete_genus_species2(result)



145
146
147
148
149
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 145

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

- (Object) autocomplete_name_author_year



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

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

- (Object) autocomplete_top_cached



134
135
136
137
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 134

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

- (Object) autocomplete_top_name



129
130
131
132
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 129

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

- (Object) base_query



225
226
227
228
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 225

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

- (Object) genus_species



210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 210

def genus_species
  parser = ScientificNameParser.new
  h = parser.parse(query_string)
  n = h[:scientificName][:details]

  if n && n.first && n.first[:genus] && n.first[:species]
    a = n.first[:genus][:string]
    b = n.first[:species][:string]

    a + '%' + b 
  else
    nil
  end
end

- (Object) is_type

and clause



98
99
100
101
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 98

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

- (Object) or_and



75
76
77
78
79
80
81
82
83
84
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 75

def or_and
  a = or_clauses
  b = and_clauses

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

- (Object) or_clauses



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

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

- (Object) table



230
231
232
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 230

def table
  TaxonName.arel_table
end

- (Object) taxon_name_hierarchies_table



234
235
236
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 234

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

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

and clause

Returns:

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


92
93
94
95
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 92

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

- (Object) where_sql



86
87
88
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 86

def where_sql
  with_project_id.and(or_and).to_sql
end

- (Object) with_cached_author_year



238
239
240
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 238

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



111
112
113
114
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 111

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

- (Object) with_parent_id

and clause, limit to ancestors or [ids]



104
105
106
107
# File 'lib/queries/taxon_name_autocomplete_query.rb', line 104

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