Class: Queries::TaxonName::Filter

Inherits:
Query
  • Object
show all
Includes:
Concerns::Tags
Defined in:
lib/queries/taxon_name/filter.rb

Instance Attribute Summary (collapse)

Attributes inherited from Query

#dynamic_limit, #options, #query_string, #terms

Instance Method Summary (collapse)

Methods inherited from Query

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

- (Filter) initialize(params)

Returns a new instance of Filter

Parameters:

  • params (Params)

    a permitted via controller



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/queries/taxon_name/filter.rb', line 90

def initialize(params)
  @name = params[:name]
  @author = params[:author]
  @year = params[:year].to_s 
  @exact = (params[:exact] == 'true' ? true : false) if !params[:exact].nil?
  @parent_id = params[:parent_id] || []
  @descendants = (params[:descendants] == 'true' ? true : false) if !params[:descendants].nil?
  @updated_since = params[:updated_since].to_s 
  @validity = (params[:validity] == 'true' ? true : false) if !params[:validity].nil?
  @taxon_name_relationship = params[:taxon_name_relationship] || {}
  @taxon_name_classification = params[:taxon_name_classification] || [] 
  @type_metadata = (params[:type_metadata] == 'true' ? true : false) if !params[:type_metadata].nil?
  @citations = params[:citations]
  @otus = (params[:otus] == 'true' ? true : false) if !params[:otus].nil?
  @project_id = params[:project_id]

  @nomenclature_group = params[:nomenclature_group]  if !params[:nomenclature_group].nil?
  @nomenclature_code = params[:nomenclature_code]  if !params[:nomenclature_code].nil?

  # TODO: support here? 
  @keyword_ids ||= []
end

Instance Attribute Details

- (Object) author

Parameters:

  • author (String)

    Use “&” for “and”. Matches against cached_author_year.



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

def author
  @author
end

- (Object) citations

Returns the value of attribute citations



68
69
70
# File 'lib/queries/taxon_name/filter.rb', line 68

def citations
  @citations
end

- (Object) descendants

'true' or 'false'

on initialize

Ignored when parent_id[].empty? Return descendants of parents as well.

Parameters:

  • descendants (Boolean)


44
45
46
# File 'lib/queries/taxon_name/filter.rb', line 44

def descendants
  @descendants
end

- (Object) exact

Parameters:

  • exact (Boolean)
    'true' or 'false'

    on initialize

    true if only valid, false if only invalid, nil if both



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

def exact
  @exact
end

- (Object) name

Parameters:

  • name (String)

    Matches against cached. See also exact.



10
11
12
# File 'lib/queries/taxon_name/filter.rb', line 10

def name
  @name
end

- (String?) nomenclature_code

Returns accessor for attr :nomenclature_code, wrap with needed wildcards

Returns:

  • (String, nil)

    accessor for attr :nomenclature_code, wrap with needed wildcards



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

def nomenclature_code
  @nomenclature_code
end

- (String?) nomenclature_group

Returns accessor for attr :nomenclature_group, wrap with needed wildcards

Returns:

  • (String, nil)

    accessor for attr :nomenclature_group, wrap with needed wildcards



82
83
84
# File 'lib/queries/taxon_name/filter.rb', line 82

def nomenclature_group
  @nomenclature_group
end

- (Object) otus

'true' or 'false'

on initialize

whether the name has an Otu

Parameters:

  • otus (Boolean)


73
74
75
# File 'lib/queries/taxon_name/filter.rb', line 73

def otus
  @otus
end

- (Object) parent_id

Return all children of these parents. Results includes self (parent_id).

Returns:

  • Return all children of these parents. Results includes self (parent_id).



39
40
41
# File 'lib/queries/taxon_name/filter.rb', line 39

def parent_id
  @parent_id
end

- (Object) project_id

TODO: probably should be an array for API purposes. TODO: unify globally as to whether param belongs here, or at controller level.

Parameters:

  • project_id (String)

    The project scope.



63
64
65
# File 'lib/queries/taxon_name/filter.rb', line 63

def project_id
  @project_id
end

- (Object) taxon_name_classification

Parameters:

  • taxon_name_classification (Array)

    Class names of TaxonNameClassification, as strings.



57
58
59
# File 'lib/queries/taxon_name/filter.rb', line 57

def taxon_name_classification
  @taxon_name_classification
end

- (Object) taxon_name_relationship

Root keys are unique symbols, typically numbers. Each entry must have a 'type' Each entry must have one (and only one) of 'subject_taxon_name_id' or 'object_taxon_name_id'

Return all taxon names in a relationship of a given type and in relation to a another name. For example, return all synonyms of Aus bus.

Parameters:

  • taxon_name_relationship (Hash)

    { “0” => => 'TaxonNameRelationship::<>', 'subject|object_taxon_name_id' => '123', “1” => {} … }



53
54
55
# File 'lib/queries/taxon_name/filter.rb', line 53

def taxon_name_relationship
  @taxon_name_relationship
end

- (Object) type_metadata

'true' or 'false'

on initialize

whether the name has TypeMaterial


78
79
80
# File 'lib/queries/taxon_name/filter.rb', line 78

def 
  @type_metadata
end

- (Object) updated_since

Parameters:

  • updated_since (String)

    in format yyyy-mm-dd Names updated (.modified_at) since this date.



28
29
30
# File 'lib/queries/taxon_name/filter.rb', line 28

def updated_since
  @updated_since
end

- (Object) validity

'true' or 'false'

on initialize

true if only valid, false if only invalid, nil if both


33
34
35
# File 'lib/queries/taxon_name/filter.rb', line 33

def validity
  @validity
end

- (Object) year

Matches against cached_author_year.

Parameters:

  • year (String)

    “yyyy”



19
20
21
# File 'lib/queries/taxon_name/filter.rb', line 19

def year
  @year
end

Instance Method Details

- (ActiveRecord::Relation) all

Returns:

  • (ActiveRecord::Relation)


409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/queries/taxon_name/filter.rb', line 409

def all
  a = and_clauses
  b = merge_clauses

  q = nil 
  if a && b
    q = b.where(a).distinct
  elsif a
    q = ::TaxonName.where(a).distinct
  elsif b
    q = b.distinct
  else
    q = ::TaxonName.all
  end

  q = q.where(project_id: project_id) if project_id
  q
end

- (ActiveRecord::Relation) and_clauses

Returns:

  • (ActiveRecord::Relation)


360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/queries/taxon_name/filter.rb', line 360

def and_clauses
  clauses = []

  clauses += [
    author_facet,
    cached_name,
    year_facet,
    updated_since_facet,
    validity_facet,
    parent_facet,
    with_nomenclature_group,
    with_nomenclature_code,
  ].compact

  return nil if clauses.empty?

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

- (Object) author_facet



326
327
328
329
330
331
332
333
# File 'lib/queries/taxon_name/filter.rb', line 326

def author_facet 
  return nil if author.blank?
  if exact
    table[:cached_author_year].eq(author)
  else
    table[:cached_author_year].matches('%' + author + '%')
  end
end

- (Object) cached_name



317
318
319
320
321
322
323
324
# File 'lib/queries/taxon_name/filter.rb', line 317

def cached_name
  return nil if name.blank?
  if exact
    table[:cached].eq(name)
  else
    table[:cached].matches('%' + name + '%')
  end
end

- (Object) citations_facet

Returns Scope

Returns:

  • Scope



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/queries/taxon_name/filter.rb', line 279

def citations_facet 
  return nil if citations.nil?
  o = table
  h = ::Citation.arel_table

  a = o.alias("tff_")
  b = o.project(a[Arel.star]).from(a)

  c = h.alias("tff_r_")

  j = a[:id].eq(c[:citation_object_id]).
      and(c[:citation_object_type].eq('TaxonName'))

  j = j.and(c[:is_original].eq(true)) if citations == 'without_origin_citation'

  b = b.join(c, Arel::Nodes::OuterJoin).on(j)

  b = b.where(c[:id].eq(nil))
  b = b.group(a[:id])
  b = b.as("tff_a_")

  ::TaxonName.joins(Arel::Nodes::InnerJoin.new(b, Arel::Nodes::On.new(b['id'].eq(table['id']))))
end

- (Object) descendant_facet

A merge facet.

Returns:

  • Scope names that are not leaves



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/queries/taxon_name/filter.rb', line 139

def descendant_facet
  return nil if parent_id.empty? || !descendants
  o = table
  h = ::TaxonNameHierarchy.arel_table

  a = o.alias('tfa_')
  b = o.project(a[Arel.star]).from(a)

  c = h.alias('tfa_r')

  b = b.join(c, Arel::Nodes::OuterJoin)
    .on(
      a[:id].eq(c[:descendant_id])
  )

  e = c[:descendant_id].not_eq(nil)
  f = c[:ancestor_id].eq_any(parent_id)

  b = b.where(e.and(f))
  b = b.group(a[:id])
  b = b.as('tfa_a_')

  ::TaxonName.joins(Arel::Nodes::InnerJoin.new(b, Arel::Nodes::On.new(b['id'].eq(table['id']))))
end

- (Object) merge_clauses



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/queries/taxon_name/filter.rb', line 383

def merge_clauses
  clauses = [
    descendant_facet,
    taxon_name_classification_facet,
    matching_keyword_ids,
    ,
    otus_facet,
    citations_facet
  ].compact

  i = 0
  taxon_name_relationship.each do |k, values|
    clauses << taxon_name_relationship_facet(values, i.to_s)
    i += 1
  end

  return nil if clauses.empty?

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

- (Object) otus_facet

Returns Scope

Returns:

  • Scope



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
# File 'lib/queries/taxon_name/filter.rb', line 251

def otus_facet 
  return nil if otus.nil?
  o = table
  h = ::Otu.arel_table

  a = o.alias("tfe_")
  b = o.project(a[Arel.star]).from(a)

  c = h.alias("tfe_r_")

  b = b.join(c, Arel::Nodes::OuterJoin)
    .on(
      a[:id].eq(c[:taxon_name_id])
  )

  if otus 
    b = b.where(c[:taxon_name_id].not_eq(nil))
  else
    b = b.where(c[:taxon_name_id].eq(nil))
  end

  b = b.group(a[:id])
  b = b.as("tfe_a_")

  ::TaxonName.joins(Arel::Nodes::InnerJoin.new(b, Arel::Nodes::On.new(b['id'].eq(table['id']))))
end

- (Object) parent_facet



354
355
356
357
# File 'lib/queries/taxon_name/filter.rb', line 354

def parent_facet
  return nil if parent_id.empty? || descendants
  table[:parent_id].eq_any(parent_id)
end

- (Arel::Table) table

Returns:

  • (Arel::Table)


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

def table
  ::TaxonName.arel_table
end

- (Object) taxon_name_classification_facet

Returns Scope

Returns:

  • Scope



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/queries/taxon_name/filter.rb', line 197

def taxon_name_classification_facet
  return nil if taxon_name_classification.empty?
  o = table
  h = ::TaxonNameClassification.arel_table

  a = o.alias("tfc_")
  b = o.project(a[Arel.star]).from(a)

  c = h.alias("tfc_r_")

  b = b.join(c, Arel::Nodes::OuterJoin)
    .on(
      a[:id].eq(c[:taxon_name_id])
  )

  e = c[:taxon_name_id].not_eq(nil)
  f = c[:type].eq_any(taxon_name_classification)

  b = b.where(e.and(f))
  b = b.group(a[:id])
  b = b.as("tfc_a_")

  ::TaxonName.joins(Arel::Nodes::InnerJoin.new(b, Arel::Nodes::On.new(b['id'].eq(table['id']))))
end

- (Object) taxon_name_relationship_facet(hsh, trn_alias = '1')

Returns Scope wrapped in descendant_facet!

Returns:

  • Scope wrapped in descendant_facet!



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/queries/taxon_name/filter.rb', line 166

def taxon_name_relationship_facet(hsh, trn_alias = '1') 
  o = table
  h = ::TaxonNameRelationship.arel_table

  a = o.alias("tfb_#{trn_alias}_")
  b = o.project(a[Arel.star]).from(a)

  c = h.alias("tfb_r_#{trn_alias}")

  trg = hsh['subject_taxon_name_id'] ? 'subject_taxon_name_id' : 'object_taxon_name_id'
  opp = hsh['subject_taxon_name_id'] ? 'object_taxon_name_id' : 'subject_taxon_name_id'

  typ = hsh['type']

  b = b.join(c, Arel::Nodes::OuterJoin)
    .on(
      a[:id].eq(c[opp]).
      and(c[:type].eq(typ))
  )

  e = c[trg].not_eq(nil)
  f = c[trg].eq(hsh[trg])

  b = b.where(e.and(f))
  b = b.group(a[:id])
  b = b.as("tfb_a_#{trn_alias}_")

  ::TaxonName.joins(Arel::Nodes::InnerJoin.new(b, Arel::Nodes::On.new(b['id'].eq(table['id']))))
end

- (Object) type_metadata_facet

Returns Scope

Returns:

  • Scope



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/queries/taxon_name/filter.rb', line 223

def  
  return nil if .nil?
  o = table
  h = ::TypeMaterial.arel_table

  a = o.alias("tfd_")
  b = o.project(a[Arel.star]).from(a)

  c = h.alias("tfd_r_")

  b = b.join(c, Arel::Nodes::OuterJoin)
    .on(
      a[:id].eq(c[:protonym_id])
  )

  if 
    b = b.where(c[:protonym_id].not_eq(nil))
  else
    b = b.where(c[:protonym_id].eq(nil))
  end

  b = b.group(a[:id])
  b = b.as("tfd_a_")

  ::TaxonName.joins(Arel::Nodes::InnerJoin.new(b, Arel::Nodes::On.new(b['id'].eq(table['id']))))
end

- (Object) updated_since_facet



340
341
342
343
# File 'lib/queries/taxon_name/filter.rb', line 340

def updated_since_facet
  return nil if updated_since.blank?
  table[:updated_at].gt(Date.parse(updated_since))
end

- (Object) validity_facet



345
346
347
348
349
350
351
352
# File 'lib/queries/taxon_name/filter.rb', line 345

def validity_facet 
  return nil if validity.nil? 
  if validity 
    table[:id].eq(table[:cached_valid_taxon_name_id])
  else
    table[:id].not_eq(table[:cached_valid_taxon_name_id])
  end
end

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

Returns and clause

Returns:

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

    and clause



312
313
314
315
# File 'lib/queries/taxon_name/filter.rb', line 312

def with_nomenclature_code
  return nil if nomenclature_code.nil?
  table[:rank_class].matches(nomenclature_code)
end

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

Returns and clause

Returns:

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

    and clause



305
306
307
308
# File 'lib/queries/taxon_name/filter.rb', line 305

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

- (Object) year_facet



335
336
337
338
# File 'lib/queries/taxon_name/filter.rb', line 335

def year_facet 
  return nil if year.blank?
  table[:cached_author_year].matches('%' + year + '%')
end