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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/queries/taxon_name/filter.rb', line 99

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_relationship_type = params[:taxon_name_relationship_type] || [] 
  @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]
  @leaves = (params[:leaves] == 'true' ? true : false) if !params[:leaves].nil?
  @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



70
71
72
# File 'lib/queries/taxon_name/filter.rb', line 70

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)


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

def descendants
  @descendants
end

- (Object) exact

Parameters:

  • exact (Boolean)

    true if matching must be exact, false if partial matches are allowed.



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

def exact
  @exact
end

- (Boolean?) leaves

TODO: inverse is duplicated in autocomplete

Returns:

  • (Boolean, nil)

    &leaves=<“true”|“false”> if 'true' then return only names without descendents if 'false' then return only names with descendents



95
96
97
# File 'lib/queries/taxon_name/filter.rb', line 95

def leaves
  @leaves
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



88
89
90
# File 'lib/queries/taxon_name/filter.rb', line 88

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



84
85
86
# File 'lib/queries/taxon_name/filter.rb', line 84

def nomenclature_group
  @nomenclature_group
end

- (Object) otus

'true' or 'false'

on initialize

whether the name has an Otu

Parameters:

  • otus (Boolean, nil)


75
76
77
# File 'lib/queries/taxon_name/filter.rb', line 75

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



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

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.



65
66
67
# File 'lib/queries/taxon_name/filter.rb', line 65

def project_id
  @project_id
end

- (Object) taxon_name_classification

Parameters:

  • taxon_name_classification (Array)

    Class names of TaxonNameClassification, as strings.



59
60
61
# File 'lib/queries/taxon_name/filter.rb', line 59

def taxon_name_classification
  @taxon_name_classification
end

- (Object) taxon_name_relationship

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 (Array)
    { 'type' => 'TaxonNameRelationship::<>', 'subject|object_taxon_name_id' => '123' } … {}


51
52
53
# File 'lib/queries/taxon_name/filter.rb', line 51

def taxon_name_relationship
  @taxon_name_relationship
end

- (Object) taxon_name_relationship_type

Parameters:

  • taxon_name_relationship (Array)

    All names involved in any of these relationship



55
56
57
# File 'lib/queries/taxon_name/filter.rb', line 55

def taxon_name_relationship_type
  @taxon_name_relationship_type
end

- (Object) type_metadata

'true' or 'false'

on initialize

whether the name has TypeMaterial


80
81
82
# File 'lib/queries/taxon_name/filter.rb', line 80

def 
  @type_metadata
end

- (Object) updated_since

Parameters:

  • updated_since (String)

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



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

def updated_since
  @updated_since
end

- (Object) validity

'true' or 'false'

on initialize

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


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

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)


345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/queries/taxon_name/filter.rb', line 345

def all
  a = and_clauses
  b = merge_clauses

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

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

- (ActiveRecord::Relation) and_clauses

Returns:

  • (ActiveRecord::Relation)


296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/queries/taxon_name/filter.rb', line 296

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



262
263
264
265
266
267
268
269
# File 'lib/queries/taxon_name/filter.rb', line 262

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



253
254
255
256
257
258
259
260
# File 'lib/queries/taxon_name/filter.rb', line 253

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



226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/queries/taxon_name/filter.rb', line 226

def citations_facet 
  return nil if citations.nil?

  citation_conditions = ::Citation.arel_table[:citation_object_id].eq(::TaxonName.arel_table[:id]).and(
    ::Citation.arel_table[:citation_object_type].eq('TaxonName'))

  if citations == 'without_origin_citation'
    citation_conditions = citation_conditions.and(::Citation.arel_table[:is_original].eq(true))
  end

  ::TaxonName.where.not(::Citation.where(citation_conditions).arel.exists)
end

- (Object) descendant_facet

A merge facet.

Returns:

  • Scope match only names that are a descendant of some parent_id



149
150
151
152
153
154
155
156
157
# File 'lib/queries/taxon_name/filter.rb', line 149

def descendant_facet
  return nil if parent_id.empty? || !descendants
  ::TaxonName.where(
    ::TaxonNameHierarchy.where(
      ::TaxonNameHierarchy.arel_table[:descendant_id].eq(::TaxonName.arel_table[:id]).and(
      ::TaxonNameHierarchy.arel_table[:ancestor_id].in(parent_id))
    ).arel.exists
  )
end

- (Object) leaves_facet

Returns Scope

Returns:

  • Scope



175
176
177
178
# File 'lib/queries/taxon_name/filter.rb', line 175

def leaves_facet
  return nil if leaves.nil?
  leaves ? ::TaxonName.leaves : ::TaxonName.not_leaves
end

- (Object) merge_clauses



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/queries/taxon_name/filter.rb', line 319

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

  taxon_name_relationship.each do |hsh|
    clauses << taxon_name_relationship_facet(hsh)
  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



160
161
162
163
164
165
166
# File 'lib/queries/taxon_name/filter.rb', line 160

def otus_facet
  return nil if otus.nil?

  subquery = ::Otu.where(::Otu.arel_table[:taxon_name_id].eq(::TaxonName.arel_table[:id])).arel.exists

  ::TaxonName.where(otus ? subquery : subquery.not)
end

- (Object) parent_facet



290
291
292
293
# File 'lib/queries/taxon_name/filter.rb', line 290

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

- (Arel::Table) table

Returns:

  • (Arel::Table)


124
125
126
# File 'lib/queries/taxon_name/filter.rb', line 124

def table
  ::TaxonName.arel_table
end

- (Object) taxon_name_classification_facet

Returns Scope

Returns:

  • Scope



196
197
198
199
200
201
202
203
204
205
# File 'lib/queries/taxon_name/filter.rb', line 196

def taxon_name_classification_facet
  return nil if taxon_name_classification.empty?

  ::TaxonName.where(
    ::TaxonNameClassification.where(
      ::TaxonNameClassification.arel_table[:taxon_name_id].eq(::TaxonName.arel_table[:id]).and(
      ::TaxonNameClassification.arel_table[:type].in(taxon_name_classification))
    ).arel.exists
  )
end

- (Object) taxon_name_relationship_facet(hsh)

Returns Scope wrapped in descendant_facet!

Returns:

  • Scope wrapped in descendant_facet!



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/queries/taxon_name/filter.rb', line 182

def taxon_name_relationship_facet(hsh)
  param_key = hsh['subject_taxon_name_id'] ? 'subject_taxon_name_id' : 'object_taxon_name_id'
  join_key = hsh['subject_taxon_name_id'] ? 'object_taxon_name_id' : 'subject_taxon_name_id'

  ::TaxonName.where(
    ::TaxonNameRelationship.where(
      ::TaxonNameRelationship.arel_table[join_key].eq(::TaxonName.arel_table[:id]).and(
      ::TaxonNameRelationship.arel_table[param_key].eq(hsh[param_key])).and(
      ::TaxonNameRelationship.arel_table[:type].eq(hsh['type']))
    ).arel.exists
  )
end

- (Object) taxon_name_relationship_type_facet

Returns Scope

Returns:

  • Scope



169
170
171
172
# File 'lib/queries/taxon_name/filter.rb', line 169

def taxon_name_relationship_type_facet
  return nil if taxon_name_relationship_type.empty?
  ::TaxonName.with_taxon_name_relationship(taxon_name_relationship_type)
end

- (Object) type_metadata_facet

Returns Scope

Returns:

  • Scope



208
209
210
211
212
213
214
# File 'lib/queries/taxon_name/filter.rb', line 208

def 
  return nil if .nil?

  subquery = ::TypeMaterial.where(::TypeMaterial.arel_table[:protonym_id].eq(::TaxonName.arel_table[:id])).arel.exists

  ::TaxonName.where( ? subquery : subquery.not)
end

- (Object) updated_since_facet



276
277
278
279
# File 'lib/queries/taxon_name/filter.rb', line 276

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

- (Object) validity_facet



281
282
283
284
285
286
287
288
# File 'lib/queries/taxon_name/filter.rb', line 281

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



248
249
250
251
# File 'lib/queries/taxon_name/filter.rb', line 248

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



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

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

- (Object) year_facet



271
272
273
274
# File 'lib/queries/taxon_name/filter.rb', line 271

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