Class: Queries::TaxonName::Filter

Inherits:
Query::Filter show all
Includes:
Concerns::Citations, Concerns::Confidences, Concerns::DataAttributes, Concerns::Depictions, Concerns::Notes, Concerns::Tags, Helpers
Defined in:
lib/queries/taxon_name/filter.rb

Constant Summary collapse

PARAMS =
[
  :ancestors,
  :ancestrify,
  :author,
  :author_exact,
  :authors,
  :collecting_event_id,
  :collection_object_id,
  :combinations,
  :combinationify,
  :descendants,
  :descendants_max_depth,
  :epithet_only,
  :etymology,
  :latinized,
  :leaves,
  :name,
  :name_exact,
  :nomenclature_code,
  :nomenclature_date,
  :nomenclature_group, # !! different than autocomplete
  :not_specified,
  :original_combination,
  :otu_id,
  :otus,
  :rank,
  :relation_to_relationship,
  :sort,
  :taxon_name_relationship_target,
  :synonymify,
  :taxon_name_author_id_or,
  :taxon_name_id,
  :taxon_name_type,
  :type_metadata,
  :validify,
  :validity,
  :verbatim_name,
  :year,
  :year_end,
  :year_start,

  collection_object_id: [],
  collecting_event_id: [],
  combination_taxon_name_id: [],
  name: [],
  otu_id: [],
  parent_id: [],
  rank: [],
  taxon_name_author_id: [],
  taxon_name_classification: [],
  taxon_name_id: [],
  taxon_name_relationship: [
    :subject_taxon_name_id,
    :object_taxon_name_id,
    :type,
  ],
  taxon_name_relationship_type_subject: [],
  taxon_name_relationship_type_object: [],
  taxon_name_relationship_type_either: [],
  type: [],
].freeze

Constants inherited from Query::Filter

Query::Filter::FILTER_QUERIES, Query::Filter::SUBQUERIES

Instance Attribute Summary collapse

Attributes inherited from Query::Filter

#api, #asserted_distribution_query, #biological_association_query, #biological_associations_graph_query, #collecting_event_query, #collection_object_query, #content_query, #controlled_vocabulary_term_query, #conveyance_query, #data_attribute_query, #depiction_query, #descriptor_query, #document_query, #dwc_occurrence_query, #extract_query, #field_occurrence_query, #image_query, #loan_query, #object_global_id, #observation_query, #order_by, #otu_query, #page, #paginate, #params, #per, #person_query, #project_id, #recent, #recent_target, #roll_call, #sound_query, #taxon_name_query, #taxon_name_relationship_query, #venn, #venn_ignore_pagination, #venn_mode

Attributes inherited from Query

#query_string, #terms

Instance Method Summary collapse

Methods included from Helpers

#boolean_param, #integer_param

Methods inherited from Query::Filter

#all_and_clauses, #all_merge_clauses, #annotator_and_clauses, #annotator_merge_clauses, annotator_params, api_except_params, api_excluded_params, #apply_venn, #attribute_exact_facet, base_filter, base_query_name, base_query_to_h, #deep_permit, #disable_paging, included_annotator_facets, instantiated_base_filter, inverted_subqueries, #object_global_id_facet, #only_project?, #paging_state, params, #permitted_params, #process_url_into_params, #project_id_facet, query_name, #set_nested_queries, #set_paging, set_paging, #shared_and_clauses, #subquery_vector, #target_and_clauses, #venn_query

Methods inherited from Query

#alphabetic_strings, #alphanumeric_strings, base_name, #base_name, #base_query, #build_terms, #cached_facet, #end_wildcard, #levenshtein_distance, #match_ordered_wildcard_pieces_in_cached, #no_terms?, referenced_klass, #referenced_klass, #referenced_klass_except, #referenced_klass_intersection, #referenced_klass_union, #start_and_end_wildcard, #start_wildcard, #table, #wildcard_pieces

Constructor Details

#initialize(query_params) ⇒ Filter

Returns a new instance of Filter.

Parameters:

  • params (Params)

    as permitted via controller



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/queries/taxon_name/filter.rb', line 340

def initialize(query_params)
  super

  @ancestors = boolean_param(params, :ancestors)
  @ancestrify = boolean_param(params, :ancestrify)
  @author = params[:author]
  @author_exact = boolean_param(params, :author_exact)
  @authors = boolean_param(params, :authors)
  @collecting_event_id = params[:collecting_event_id]
  @collection_object_id = params[:collection_object_id]
  @combination_taxon_name_id = params[:combination_taxon_name_id]
  @combinations = boolean_param(params, :combinations)
  @combinationify = boolean_param(params, :combinationify)
  @descendants = boolean_param(params, :descendants)
  @descendants_max_depth = params[:descendants_max_depth]
  @etymology = boolean_param(params, :etymology)
  @epithet_only = params[:epithet_only]
  @latinized = boolean_param(params, :latinized)
  @geo_json = params[:geo_json]
  @leaves = boolean_param(params, :leaves)
  @name = params[:name]
  @name_exact = boolean_param(params, :name_exact)
  @nomenclature_date = boolean_param(params, :nomenclature_date)
  @nomenclature_code = params[:nomenclature_code] if params[:nomenclature_code].present?
  @nomenclature_group = params[:nomenclature_group] if params[:nomenclature_group].present?
  @not_specified = boolean_param(params, :not_specified)
  @otu_id = params[:otu_id]
  @otus = boolean_param(params, :otus)
  @original_combination = boolean_param(params, :original_combination)
  @parent_id = params[:parent_id]
  @rank = params[:rank]
  @relation_to_relationship = params[:relation_to_relationship]
  @sort = params[:sort]
  @synonymify = boolean_param(params, :synonymify)
  @taxon_name_author_id = params[:taxon_name_author_id]
  @taxon_name_author_id_or = boolean_param(params, :taxon_name_author_id_or)
  @taxon_name_classification = params[:taxon_name_classification] || []
  @taxon_name_id = params[:taxon_name_id]
  @taxon_name_relationship = params[:taxon_name_relationship] || []
  @taxon_name_relationship_target = params[:taxon_name_relationship_target]
  @taxon_name_relationship_type_subject =
    params[:taxon_name_relationship_type_subject] || []
  @taxon_name_relationship_type_object =
    params[:taxon_name_relationship_type_object] || []
  @taxon_name_relationship_type_either =
    params[:taxon_name_relationship_type_either] || []
  @taxon_name_type = params[:taxon_name_type]
  @type_metadata = boolean_param(params, :type_metadata)
  @validify = boolean_param(params, :validify)
  @validity = boolean_param(params, :validity)
  @verbatim_name = boolean_param(params, :verbatim_name)

  @year = params[:year]
  @year_end = params[:year_end]
  @year_start = params[:year_start]

  set_confidences_params(params)
  set_citations_params(params)
  set_depiction_params(params)
  set_notes_params(params)
  set_data_attributes_params(params)
  set_tags_params(params)
end

Instance Attribute Details

#ancestorsObject

Returns Boolean Ignored when taxon_name_id[].empty? Works as AND clause with descendants :(.

Parameters:

  • ancestors (Boolean, 'true', 'false', nil)

Returns:

  • Boolean Ignored when taxon_name_id[].empty? Works as AND clause with descendants :(



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

def ancestors
  @ancestors
end

#ancestrifyObject

!! This parameter is not like the others, it is applied POST result, see also synonymify and validify, etc.

Parameters:

  • ancestrify (Boolean)

    true - extend result to include all ancestors of names in the result along with the result false/nil - ignore



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

def ancestrify
  @ancestrify
end

#authorObject

Parameters:

  • author (String)

    Use “&” for “and”. Matches against cached_author_year. See also author_exact.



126
127
128
# File 'lib/queries/taxon_name/filter.rb', line 126

def author
  @author
end

#author_exactObject

Returns Boolean.

Returns:

  • Boolean



129
130
131
# File 'lib/queries/taxon_name/filter.rb', line 129

def author_exact
  @author_exact
end

#authorsObject

‘true’ or ‘false’

on initialize

whether the name has an author string, from any source, provided

Parameters:

  • authors (Boolean, nil)


257
258
259
# File 'lib/queries/taxon_name/filter.rb', line 257

def authors
  @authors
end

#collecting_event_idObject

Returns Array.

Parameters:

  • collecting_event_id (String, Array)

Returns:

  • Array



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

def collecting_event_id
  @collecting_event_id
end

#collection_object_idObject

Returns Array.

Parameters:

  • collection_object_id (String, Array)

Returns:

  • Array



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

def collection_object_id
  @collection_object_id
end

#combination_taxon_name_idArray

Returns taxon_name_ids for which all Combinations will be returned.

Returns:

  • (Array)

    taxon_name_ids for which all Combinations will be returned



316
317
318
# File 'lib/queries/taxon_name/filter.rb', line 316

def combination_taxon_name_id
  @combination_taxon_name_id
end

#combinationifyObject

!! This parameter is not like the others, it is applied POST result, see also synonymify and validify

Parameters:

  • combinationify (Boolean)

    true - extend result to include all Combinations in which the finest name is a member of the result false/nil - ignore



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

def combinationify
  @combinationify
end

#combinationsObject

This parameter should be used along side species or genus group limits.

Parameters:

  • combinations (Boolean)

    true - only return names that have (subsequent) Combinations false - only return names without (subequent) Combinations nil - ignore



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

def combinations
  @combinations
end

#descendantsBoolean

Returns true - only descendants NOT SELF false - self AND descendants nil - ignored Ignored when taxon_name_id[].empty?.

Parameters:

  • descendants (Boolean, 'true', 'false', nil)

    Read carefully! descendants = false is NOT no descendants, it’s descendants and self

Returns:

  • (Boolean)

    true - only descendants NOT SELF false - self AND descendants nil - ignored Ignored when taxon_name_id[].empty?



187
188
189
# File 'lib/queries/taxon_name/filter.rb', line 187

def descendants
  @descendants
end

#descendants_max_depthInteger?

A positive integer indicating how many levels deep of descendants to retrieve.

Ignored when descentants is false/unspecified.
Defaults to nil

Parameters:

  • descendants_max_depth (Integer)

Returns:

  • (Integer, nil)


194
195
196
# File 'lib/queries/taxon_name/filter.rb', line 194

def descendants_max_depth
  @descendants_max_depth
end

#epithet_onlyObject

Parameters:

  • epithet_only (Boolean)

    true - use name property instead of cached to find taxon name false/nil - ignore



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

def epithet_only
  @epithet_only
end

#etymologyObject

‘true’ or ‘false’

on initialize

whether the name has etymology

Parameters:

  • etymology (Boolean, nil)


252
253
254
# File 'lib/queries/taxon_name/filter.rb', line 252

def etymology
  @etymology
end

#geo_jsonObject

Returns the value of attribute geo_json.



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

def geo_json
  @geo_json
end

#latinizedBoolean?

Returns &latinized=<“true”|“false”> if ‘true’ then return only genus group names with gender and species

group names with part of speech

if ‘false’ then return only genus group names without gender and

species group names without part of speech

if nil, ignore.

Returns:

  • (Boolean, nil)

    &latinized=<“true”|“false”> if ‘true’ then return only genus group names with gender and species

    group names with part of speech
    

    if ‘false’ then return only genus group names without gender and

    species group names without part of speech
    

    if nil, ignore



294
295
296
# File 'lib/queries/taxon_name/filter.rb', line 294

def latinized
  @latinized
end

#leavesBoolean?

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



285
286
287
# File 'lib/queries/taxon_name/filter.rb', line 285

def leaves
  @leaves
end

#nameArray

Returns Matches against cached. See also name_exact.

Parameters:

  • name (String, Array)

Returns:

  • (Array)

    Matches against cached. See also name_exact.



119
120
121
# File 'lib/queries/taxon_name/filter.rb', line 119

def name
  @name
end

#name_exactObject

Returns Boolean.

Returns:

  • Boolean



122
123
124
# File 'lib/queries/taxon_name/filter.rb', line 122

def name_exact
  @name_exact
end

#nomenclature_codeString?

Returns accessor for attr :nomenclature_code, wrap with needed wildcards.

Returns:

  • (String, nil)

    accessor for attr :nomenclature_code, wrap with needed wildcards



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

def nomenclature_code
  @nomenclature_code
end

#nomenclature_dateObject

Returns Boolean with/out cached nomenclature date set.

Parameters:

  • ancestors (Boolean, 'true', 'false', nil)

Returns:

  • Boolean with/out cached nomenclature date set



139
140
141
# File 'lib/queries/taxon_name/filter.rb', line 139

def nomenclature_date
  @nomenclature_date
end

#nomenclature_groupString?

Returns accessor for attr :nomenclature_group, wrap with needed wildcards.

Returns:

  • (String, nil)

    accessor for attr :nomenclature_group, wrap with needed wildcards



274
275
276
# File 'lib/queries/taxon_name/filter.rb', line 274

def nomenclature_group
  @nomenclature_group
end

#not_specifiedObject

Returns the value of attribute not_specified.



269
270
271
# File 'lib/queries/taxon_name/filter.rb', line 269

def not_specified
  @not_specified
end

#original_combinationBoolean?

Returns true - name has at least one element of original combination false - name has no element of original combination nil - ignored.

Parameters:

  • original_combination (Boolean)

Returns:

  • (Boolean, nil)

    true - name has at least one element of original combination false - name has no element of original combination nil - ignored



237
238
239
# File 'lib/queries/taxon_name/filter.rb', line 237

def original_combination
  @original_combination
end

#otu_idArray?

Returns one or more OTU ids.

Parameters:

  • otu_id (Array, nil)

Returns:

  • (Array, nil)

    one or more OTU ids



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

def otu_id
  @otu_id
end

#otusObject

‘true’ or ‘false’

on initialize

whether the name has an Otu

Parameters:

  • otus (Boolean, nil)


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

def otus
  @otus
end

#parent_idObject

Return the taxon names with this/these parent_ids

Returns:

  • Return the taxon names with this/these parent_ids



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

def parent_id
  @parent_id
end

#rankArray

Returns:

  • (Array)


323
324
325
# File 'lib/queries/taxon_name/filter.rb', line 323

def rank
  @rank
end

#relation_to_relationshipString?

Returns &relation_to_relationship=<subject|object|either> All names that are subject|object|either of any relationship.

Returns:

  • (String, nil)

    &relation_to_relationship=<subject|object|either> All names that are subject|object|either of any relationship



226
227
228
# File 'lib/queries/taxon_name/filter.rb', line 226

def relation_to_relationship
  @relation_to_relationship
end

#sortString?

Parameters:

  • sort (String, nil)

    one of :classification, :alphabetical

Returns:

  • (String, nil)


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

def sort
  @sort
end

#synonymifyObject

!! This parameter is not like the others, it is applied POST result, see also combinationify and validify

Parameters:

  • synonymify (Boolean)

    true - extend result to include all Synonyms of any member of the list false/nil - ignore



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

def synonymify
  @synonymify
end

#taxon_name_author_idArray

Returns:

  • (Array)


301
302
303
# File 'lib/queries/taxon_name/filter.rb', line 301

def taxon_name_author_id
  @taxon_name_author_id
end

#taxon_name_author_id_orBoolean

Parameters:

  • `false`, (String)

    nil - treat the ids in taxon_name_author_id as “or” (match any TaxonName with any of these authors) ‘true’ - treat the ids in taxon_name_author_id as “and” (only TaxonNames with all and only all will match)

Returns:

  • (Boolean)


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

def taxon_name_author_id_or
  @taxon_name_author_id_or
end

#taxon_name_classificationObject

Parameters:

  • taxon_name_classification (Array)

    Class names of TaxonNameClassification, as strings.



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

def taxon_name_classification
  @taxon_name_classification
end

#taxon_name_idObject

Return the taxon name(s) with this/these ids

Returns:

  • Return the taxon name(s) with this/these ids



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

def taxon_name_id
  @taxon_name_id
end

#taxon_name_relationshipObject

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’ } … {}


208
209
210
# File 'lib/queries/taxon_name/filter.rb', line 208

def taxon_name_relationship
  @taxon_name_relationship
end

#taxon_name_relationship_targetBoolean

Applies only to taxon_name_relationship_query_facet, is only present in queries sent from Filter TaxonNameRelationship.

Parameters:

  • taxon_name_relationship_target (Boolean)

Returns:

  • (Boolean)
    • ‘subject’: only return subjects of relationships from taxon_name_relationship_query_facet

    • ‘object’: only return objects of relationships from taxon_name_relationship_query_facet

    • nil: return both subjects and objects



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

def taxon_name_relationship_target
  @taxon_name_relationship_target
end

#taxon_name_relationship_type_eitherObject

Parameters:

  • taxon_name_relationship_type_either (Array)

    All names involved in any of these relationships as either subject or object



221
222
223
# File 'lib/queries/taxon_name/filter.rb', line 221

def taxon_name_relationship_type_either
  @taxon_name_relationship_type_either
end

#taxon_name_relationship_type_objectObject

Parameters:

  • taxon_name_relationship_type_object (Array)

    All names involved in any of these relationships as object



216
217
218
# File 'lib/queries/taxon_name/filter.rb', line 216

def taxon_name_relationship_type_object
  @taxon_name_relationship_type_object
end

#taxon_name_relationship_type_subjectObject

Parameters:

  • taxon_name_relationship_type_subject (Array)

    All names involved in any of these relationships as subject



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

def taxon_name_relationship_type_subject
  @taxon_name_relationship_type_subject
end

#taxon_name_typeString?

Returns &taxon_name_type=<Protonym|Combination|Hybrid>.

Returns:

  • (String, nil)

    &taxon_name_type=<Protonym|Combination|Hybrid>



298
299
300
# File 'lib/queries/taxon_name/filter.rb', line 298

def taxon_name_type
  @taxon_name_type
end

#type_metadataObject

‘true’ or ‘false’

on initialize

whether the name has TypeMaterial


262
263
264
# File 'lib/queries/taxon_name/filter.rb', line 262

def 
  @type_metadata
end

#validifyObject

name is returned !! This param is not like the others. !!

Returns:

  • Boolean if true then for each name in the result its valid



161
162
163
# File 'lib/queries/taxon_name/filter.rb', line 161

def validify
  @validify
end

#validityObject

‘true’ or ‘false’

on initialize

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


154
155
156
# File 'lib/queries/taxon_name/filter.rb', line 154

def validity
  @validity
end

#verbatim_nameObject

Returns Boolean if true then ‘verbatim_name` is populated.

Returns:

  • Boolean if true then ‘verbatim_name` is populated



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

def verbatim_name
  @verbatim_name
end

#yearObject

Matches against cached_author_year.

Parameters:

  • year (String)

    “yyyy”



134
135
136
# File 'lib/queries/taxon_name/filter.rb', line 134

def year
  @year
end

#year_endObject

Matches against cached_nomenclature_date

Parameters:

  • year_end (String)

    “yyyy”



149
150
151
# File 'lib/queries/taxon_name/filter.rb', line 149

def year_end
  @year_end
end

#year_startObject

Matches against cached_nomenclature_date

Parameters:

  • year_start (String)

    “yyyy”



144
145
146
# File 'lib/queries/taxon_name/filter.rb', line 144

def year_start
  @year_start
end

Instance Method Details

#all(nil_empty = false) ⇒ ActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
# File 'lib/queries/taxon_name/filter.rb', line 1090

def all(nil_empty = false)
  # Everything below subqueries on q in some way, so q can't have paging
  # enabled.
  paging_state = disable_paging
  q = super

  # Order matters, use this first to go up
  q = ancestrify_result(q) if ancestrify

  # Then out in various ways
  q = validify_result(q) if validify
  q = combinationify_result(q) if combinationify
  q = synonimify_result(q) if synonymify

  # Then sort
  if sort
    q = order_clause(q)
    paging_state[:ordered] = true
  end

  q = self.class.set_paging(q, paging_state)

  q
end

#ancestor_facetObject

A merge facet.

Returns:

  • Scope match only names that are a ancestor of some taxon_name_id



556
557
558
559
560
561
562
563
564
565
566
# File 'lib/queries/taxon_name/filter.rb', line 556

def ancestor_facet
  return nil if taxon_name_id.empty? || !(ancestors == true)

  ancestors_subquery = ::TaxonNameHierarchy.where(
    ::TaxonNameHierarchy.arel_table[:ancestor_id].eq(::TaxonName.arel_table[:id]).and(
      ::TaxonNameHierarchy.arel_table[:descendant_id].in(taxon_name_id)
    )
  )

  ::TaxonName.where(ancestors_subquery.arel.exists)
end

#ancestrify_result(q) ⇒ Object



1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
# File 'lib/queries/taxon_name/filter.rb', line 1059

def ancestrify_result(q)
  s = ::TaxonName
     .with(tn_q4: q)
    .joins('JOIN taxon_name_hierarchies tnh on tnh.ancestor_id = taxon_names.id')
    .joins('JOIN tn_q4 ON tn_q4.id = tnh.descendant_id')
    .distinct
    .to_sql

  # !! Do not use .distinct here
  ::TaxonName.from('(' + s + ') as taxon_names')
end

#and_clausesActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
# File 'lib/queries/taxon_name/filter.rb', line 967

def and_clauses
  [
    nomenclature_date_facet,
    author_facet,
    name_facet,
    parent_id_facet,
    rank_facet,
    taxon_name_type_facet,
    validity_facet,
    verbatim_name_facet,
    with_nomenclature_code,
    with_nomenclature_group,
    year_facet,
  ]
end

#asserted_distribution_query_facetObject



890
891
892
893
894
895
896
897
898
899
# File 'lib/queries/taxon_name/filter.rb', line 890

def asserted_distribution_query_facet
  return nil if asserted_distribution_query.nil?
  s = 'WITH query_ad_tn AS (' + asserted_distribution_query.all.to_sql + ') ' +
      ::TaxonName
        .joins(otus: [:asserted_distributions])
        .joins('JOIN query_ad_tn as query_ad_tn1 on query_ad_tn1.otu_id = asserted_distributions.otu_id')
        .to_sql

  ::TaxonName.from('(' + s + ') as taxon_names').distinct
end

#author_facetObject



810
811
812
813
814
815
816
817
# File 'lib/queries/taxon_name/filter.rb', line 810

def author_facet
  return nil if author.blank?
  if author_exact
    table[:cached_author_year].eq(author.strip)
  else
    table[:cached_author_year].matches('%' + author.strip.gsub(/\s/, '%') + '%')
  end
end

#authors_facetObject

This is not true! It includes records that are year only.

Returns:

  • Scope



586
587
588
589
590
591
# File 'lib/queries/taxon_name/filter.rb', line 586

def authors_facet
  return nil if authors.nil?
  authors ?
    ::TaxonName.where.not(cached_author_year: nil) :
    ::TaxonName.where(cached_author_year: nil)
end

#biological_association_query_facetObject



923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
# File 'lib/queries/taxon_name/filter.rb', line 923

def biological_association_query_facet
  return nil if biological_association_query.nil?
  s = 'WITH query_tn_ba AS (' + biological_association_query.all.to_sql + ') '

  a = ::TaxonName
    .joins(:otus)
    .joins("JOIN query_tn_ba as query_tn_ba1 on query_tn_ba1.biological_association_subject_id = otus.id AND query_tn_ba1.biological_association_subject_type = 'Otu'").to_sql

  b = ::TaxonName
    .joins(:otus)
    .joins("JOIN query_tn_ba as query_tn_ba2 on query_tn_ba2.biological_association_object_id = otus.id AND query_tn_ba2.biological_association_object_type = 'Otu'").to_sql

  s << ::TaxonName.from("((#{a}) UNION (#{b})) as taxon_names").to_sql

  ::TaxonName.from('(' + s + ') as taxon_names')
end

#collecting_event_id_facetObject



862
863
864
865
866
867
# File 'lib/queries/taxon_name/filter.rb', line 862

def collecting_event_id_facet
  return nil if collecting_event_id.empty?
  ::TaxonName
    .joins(:collection_objects)
    .where(collection_objects: { collecting_event_id: })
end

#collecting_event_query_facetObject



912
913
914
915
916
917
918
919
920
921
# File 'lib/queries/taxon_name/filter.rb', line 912

def collecting_event_query_facet
  return nil if collecting_event_query.nil?
  s = 'WITH query_ce_tns AS (' + collecting_event_query.all.to_sql + ') ' +
      ::TaxonName
        .joins(:collection_objects)
        .joins('JOIN query_ce_tns as query_ce_tns1 on collection_objects.collecting_event_id = query_ce_tns1.id')
        .to_sql

  ::TaxonName.from('(' + s + ') as taxon_names').distinct
end

#collection_object_id_facetObject



568
569
570
571
# File 'lib/queries/taxon_name/filter.rb', line 568

def collection_object_id_facet
  return nil if collection_object_id.empty?
  ::TaxonName.joins(:collection_objects).where(collection_objects: { id: collection_object_id })
end

#collection_object_query_facetObject



901
902
903
904
905
906
907
908
909
910
# File 'lib/queries/taxon_name/filter.rb', line 901

def collection_object_query_facet
  return nil if collection_object_query.nil?
  s = 'WITH query_collection_objects AS (' + collection_object_query.all.to_sql + ') ' +
      ::TaxonName
        .joins(:collection_objects)
        .joins('JOIN query_collection_objects as query_collection_objects1 on collection_objects.id = query_collection_objects1.id')
        .to_sql

  ::TaxonName.from('(' + s + ') as taxon_names').distinct
end

#combination_taxon_name_id_facetObject



851
852
853
854
855
856
857
858
859
860
# File 'lib/queries/taxon_name/filter.rb', line 851

def combination_taxon_name_id_facet
  return nil if combination_taxon_name_id.empty?
  ::Combination.joins(:related_taxon_name_relationships)
    .where(
      taxon_name_relationships: {
        type: ::TAXON_NAME_RELATIONSHIP_COMBINATION_TYPES.values,
        subject_taxon_name_id: combination_taxon_name_id,
      },
    ).distinct
end

#combinationify_result(q) ⇒ Object



1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
# File 'lib/queries/taxon_name/filter.rb', line 1047

def combinationify_result(q)
  s = ::TaxonName
    .with(tn_q3: q)
    .joins('JOIN tn_q3 ON tn_q3.id = taxon_names.cached_valid_taxon_name_id')
    .where("taxon_names.type = 'Combination'")
    .to_sql

  a = ::TaxonName.from('(' + s + ') as taxon_names').distinct

  referenced_klass_union([q, a])
end

#combinations_facetObject



869
870
871
872
873
874
875
876
877
# File 'lib/queries/taxon_name/filter.rb', line 869

def combinations_facet
  return nil if combinations.nil?
  a = ::Protonym.joins(:combination_relationships)
  if combinations
    a
  else
    referenced_klass_except(a)
  end
end

#descendant_facetObject

Returns Scope match only names that are a descendant of some taxon_name_id.

Returns:

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



526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/queries/taxon_name/filter.rb', line 526

def descendant_facet
  return nil if taxon_name_id.empty? || descendants.nil?

  h = ::TaxonNameHierarchy.arel_table

  if descendants
    descendants_subquery = ::TaxonNameHierarchy.where(
      h[:descendant_id].eq(::TaxonName.arel_table[:id]).and(
        h[:ancestor_id].in(taxon_name_id)
      )
    ).where(h[:ancestor_id].not_eq(h[:descendant_id]))

    if descendants_max_depth.present?
      descendants_subquery = descendants_subquery.where(::TaxonNameHierarchy.arel_table[:generations].lteq(descendants_max_depth.to_i))
    end

    ::TaxonName.where(descendants_subquery.arel.exists)
  else
    q = ::TaxonName.joins('JOIN taxon_name_hierarchies ON taxon_name_hierarchies.descendant_id = taxon_names.id')
      .where(taxon_name_hierarchies: {ancestor_id: taxon_name_id})
     if descendants_max_depth.present?
       q = q.where(::TaxonNameHierarchy.arel_table[:generations].lteq(descendants_max_depth.to_i))
     end
     q
  end
end

#geo_json_facetObject



482
483
484
485
486
487
488
489
490
491
# File 'lib/queries/taxon_name/filter.rb', line 482

def geo_json_facet
  return nil if geo_json.nil?
  otus = ::Queries::Otu::Filter.new(geo_json:).all
  collection_objects = ::Queries::CollectionObject::Filter.new(geo_json:).all

  a = ::TaxonName.joins(:taxon_taxon_determinations).where(taxon_determinations: { taxon_determination_object: collection_objects })
  b = ::TaxonName.joins(:otus).where(otus:)

  ::TaxonName.from("((#{a.to_sql}) UNION (#{b.to_sql})) as taxon_names")
end

#latinized_facetObject

Returns Scope.

Returns:

  • Scope



641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
# File 'lib/queries/taxon_name/filter.rb', line 641

def latinized_facet
  return nil if latinized.nil?

  tnc = ::TaxonNameClassification.arel_table
  if latinized == true
    # Note the query here does not restrict to genus/species groups - a
    # genus whose rank is changed to subfamily can retain its gender,
    # e.g., and we want to include those here.
    ::TaxonName.where(
      ::TaxonNameClassification.where(
        tnc[:taxon_name_id].eq(table[:id]).and(
          tnc[:type].in(LATINIZED_TAXON_NAME_CLASSIFICATION_NAMES)
        )
      ).arel.exists
    )
  else
    ::TaxonName
      .where(
        table[:rank_class].in(GENUS_AND_SPECIES_RANK_NAMES)
      )
      .where.not(
        ::TaxonNameClassification.where(
          tnc[:taxon_name_id].eq(table[:id]).and(
            tnc[:type].in(LATINIZED_TAXON_NAME_CLASSIFICATION_NAMES)
          )
        ).arel.exists
      )
  end
end

#leaves_facetObject

Returns Scope.

Returns:

  • Scope



635
636
637
638
# File 'lib/queries/taxon_name/filter.rb', line 635

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

#merge_clausesObject



983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
# File 'lib/queries/taxon_name/filter.rb', line 983

def merge_clauses
  clauses = [
    asserted_distribution_query_facet,
    biological_association_query_facet,
    collecting_event_query_facet,
    collection_object_query_facet,
    otu_query_facet,
    taxon_name_relationship_query_facet,

    ancestor_facet,
    authors_facet,
    collecting_event_id_facet,
    collection_object_id_facet,
    combination_taxon_name_id_facet,
    combinations_facet,
    descendant_facet,
    latinized_facet,
    leaves_facet,
    not_specified_facet,
    original_combination_facet,
    otu_id_facet,
    otus_facet,
    relation_to_relationship_facet,
    taxon_name_author_id_facet,
    taxon_name_classification_facet,
    taxon_name_relationship_type_facet,
    ,
    with_etymology_facet,
    year_range_facet,
  ]

  taxon_name_relationship.each do |hsh|
    clauses << taxon_name_relationship_facet(hsh)
  end

  clauses.compact
end

#model_id_facetObject

Overrides base class



1022
1023
1024
1025
# File 'lib/queries/taxon_name/filter.rb', line 1022

def model_id_facet
  return nil if taxon_name_id.empty? || !descendants.nil? || ancestors
  table[:id].in(taxon_name_id)
end

#name_facetObject



787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
# File 'lib/queries/taxon_name/filter.rb', line 787

def name_facet
  return nil if name.empty?
  if name_exact
    if (epithet_only)
      table[:name].in(name)
    else
      table[:cached].in(name).or(table[:cached_original_combination].in(name))
    end
    #  table[:cached].eq(name.strip).or(table[:cached_original_combination].eq(name.strip))
  else
    if (epithet_only)
      table[:name].matches_any(name.collect { |n| '%' + n.gsub(/\s+/, '%') + '%' })
    else
      table[:cached].matches_any(name.collect { |n| '%' + n.gsub(/\s+/, '%') + '%' }).or(table[:cached_original_combination].matches_any(name.collect { |n| '%' + n.gsub(/\s+/, '%') + '%' }))
    end
  end
end

#nomenclature_date_facetObject



515
516
517
518
519
520
521
522
# File 'lib/queries/taxon_name/filter.rb', line 515

def nomenclature_date_facet
  return nil if nomenclature_date.nil?
  if nomenclature_date
    table[:cached_nomenclature_date].not_eq(nil)
  else
    table[:cached_nomenclature_date].eq(nil)
  end
end

#not_specified_facetObject



493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/queries/taxon_name/filter.rb', line 493

def not_specified_facet
  return nil if not_specified.nil?
  if not_specified
    ::TaxonName.where(table[:cached].matches('%NOT SPECIFIED%').or(
      table[:cached_original_combination].matches('%NOT SPECIFIED%')
    ))
  else
    ::TaxonName.where(table[:cached].does_not_match('%NOT SPECIFIED%').and(
      table[:cached_original_combination].does_not_match('%NOT SPECIFIED%')
    ))
  end
end

#order_clause(query) ⇒ Object



1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
# File 'lib/queries/taxon_name/filter.rb', line 1071

def order_clause(query)
  case sort
  when 'alphabetical'
    ::TaxonName.select('*').from(
      query.order('taxon_names.cached'), :inner_query
    )
  when 'classification'
    ::TaxonName.select('*').from(
      query
        .joins('INNER JOIN taxon_name_hierarchies ON taxon_names.id = taxon_name_hierarchies.descendant_id')
        .order('taxon_name_hierarchies.generations, taxon_name_hierarchies.ancestor_id, taxon_names.cached'),
      :inner_query
    )
  else
    query
  end
end

#original_combination_facetObject



506
507
508
509
510
511
512
513
# File 'lib/queries/taxon_name/filter.rb', line 506

def original_combination_facet
  return nil if original_combination.nil?
  if original_combination
    ::Protonym.joins(:original_combination_relationships).distinct
  else
    ::Protonym.left_joins(:original_combination_relationships).where(taxon_name_relationships: {id: nil})
  end
end

#otu_id_facetObject



573
574
575
576
# File 'lib/queries/taxon_name/filter.rb', line 573

def otu_id_facet
  return nil if otu_id.empty?
  ::TaxonName.joins(:otus).where(otus: { id: otu_id })
end

#otu_query_facetObject



879
880
881
882
883
884
885
886
887
888
# File 'lib/queries/taxon_name/filter.rb', line 879

def otu_query_facet
  return nil if otu_query.nil?
  s = 'WITH query_otu_tn AS (' + otu_query.all.to_sql + ') ' +
      ::TaxonName
        .joins(:otus)
        .joins('JOIN query_otu_tn as query_otu_tn1 on otus.id = query_otu_tn1.id') # Don't change, see `validify`
        .to_sql

  ::TaxonName.from('(' + s + ') as taxon_names').distinct
end

#otus_facetObject

Returns Scope.

Returns:

  • Scope



756
757
758
759
760
# File 'lib/queries/taxon_name/filter.rb', line 756

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

#parent_id_facetObject



805
806
807
808
# File 'lib/queries/taxon_name/filter.rb', line 805

def parent_id_facet
  return nil if parent_id.empty?
  table[:parent_id].in(parent_id)
end

#rank_facetArel::Nodes::Grouping?

Returns:

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


769
770
771
772
773
774
# File 'lib/queries/taxon_name/filter.rb', line 769

def rank_facet
  return nil if rank.blank?
  # We don't wildcard end so that we can isolate to specific ranks and below
  r = rank.collect { |i| "%#{i}" }
  table[:rank_class].matches_any(r)
end

#relation_to_relationship_facetObject

Returns Scope.

Returns:

  • Scope



690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
# File 'lib/queries/taxon_name/filter.rb', line 690

def relation_to_relationship_facet
  return nil if relation_to_relationship.nil?

  if relation_to_relationship == 'subject'
    ::TaxonName.with_taxon_name_relationships_as_subject.distinct
  elsif relation_to_relationship == 'object'
    ::TaxonName.with_taxon_name_relationships_as_object.distinct
  else
    # 3-4x more time-performant than using
    # :with_taxon_name_relationships.distinct
    ::TaxonName.joins('join taxon_name_relationships ON ' \
      'taxon_names.id = taxon_name_relationships.subject_taxon_name_id OR ' \
      'taxon_names.id = taxon_name_relationships.object_taxon_name_id'
    ).distinct
  end
end

#synonimify_result(q) ⇒ Object



1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
# File 'lib/queries/taxon_name/filter.rb', line 1036

def synonimify_result(q)
  s = ::TaxonName
    .with(tn_q2: q)
    .joins('JOIN tn_q2 ON tn_q2.id = taxon_names.cached_valid_taxon_name_id')
    .to_sql

  a = ::TaxonName.from('(' + s + ') as taxon_names').distinct

  referenced_klass_union([q, a])
end

#taxon_name_author_id_facetObject



720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
# File 'lib/queries/taxon_name/filter.rb', line 720

def taxon_name_author_id_facet
  return nil if taxon_name_author_id.empty?
  o = table
  r = ::Role.arel_table

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

  c = r.alias('r1')

  b = b.join(c, Arel::Nodes::OuterJoin)
    .on(
      a[:id].eq(c[:role_object_id])
        .and(c[:role_object_type].eq('TaxonName'))
        .and(c[:type].eq('TaxonNameAuthor'))
    )

  e = c[:id].not_eq(nil)
  f = c[:person_id].in(taxon_name_author_id)

  b = b.where(e.and(f))
  b = b.group(a['id'])
  b = b.having(a['id'].count.eq(taxon_name_author_id.length)) unless taxon_name_author_id_or
  b = b.as('tn_z1_')

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

#taxon_name_classification_facetObject

Returns Scope.

Returns:

  • Scope



708
709
710
711
712
713
714
715
716
717
718
# File 'lib/queries/taxon_name/filter.rb', line 708

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

#taxon_name_relationship_facet(hsh) ⇒ Object

Returns Scope wrapped in descendant_facet!.

Returns:

  • Scope wrapped in descendant_facet!



673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
# File 'lib/queries/taxon_name/filter.rb', line 673

def taxon_name_relationship_facet(hsh)
  hsh = hsh.symbolize_keys
  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

#taxon_name_relationship_query_facetObject



940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
# File 'lib/queries/taxon_name/filter.rb', line 940

def taxon_name_relationship_query_facet
  return nil if taxon_name_relationship_query.nil?

  a = nil
  b = nil
  if taxon_name_relationship_target == 'subject' ||
     taxon_name_relationship_target.nil?
    a = ::TaxonName
      .with(tnr_query: taxon_name_relationship_query.all)
      .joins(:taxon_name_relationships)
      .where('taxon_name_relationships.id IN (SELECT id FROM tnr_query)')
      .distinct
  end

  if taxon_name_relationship_target == 'object' ||
     taxon_name_relationship_target.nil?
    b = ::TaxonName
      .with(tnr_query: taxon_name_relationship_query.all)
      .joins(:related_taxon_name_relationships)
      .where('taxon_name_relationships.id IN (SELECT id FROM tnr_query)')
      .distinct
  end

  referenced_klass_union([a,b])
end

#taxon_name_relationship_type_facetObject

Returns Scope.

Returns:

  • Scope



602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
# File 'lib/queries/taxon_name/filter.rb', line 602

def taxon_name_relationship_type_facet
  if taxon_name_relationship_type_subject.empty? &&
     taxon_name_relationship_type_object.empty? &&
     taxon_name_relationship_type_either.empty?
    return nil
  end

  s = nil
  o = nil
  e = nil

  if taxon_name_relationship_type_subject.present?
    s = ::TaxonName.as_subject_with_taxon_name_relationship(
      taxon_name_relationship_type_subject
    ).distinct
  end

  if taxon_name_relationship_type_object.present?
    o = ::TaxonName.as_object_with_taxon_name_relationship(
      taxon_name_relationship_type_object
    ).distinct
  end

  if taxon_name_relationship_type_either.present?
    e = ::TaxonName.with_taxon_name_relationship(
      taxon_name_relationship_type_either
    )
  end

  referenced_klass_union([s, o, e])
end

#taxon_name_type_facetObject



782
783
784
785
# File 'lib/queries/taxon_name/filter.rb', line 782

def taxon_name_type_facet
  return nil if taxon_name_type.blank?
  table[:type].eq(taxon_name_type)
end

#type_metadata_facetObject

Returns Scope.

Returns:

  • Scope



749
750
751
752
753
# File 'lib/queries/taxon_name/filter.rb', line 749

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

#validify_result(q) ⇒ Object



1027
1028
1029
1030
1031
1032
1033
1034
# File 'lib/queries/taxon_name/filter.rb', line 1027

def validify_result(q)
  s = ::TaxonName
    .with(tn_q1: q)
    .joins('JOIN tn_q1 ON tn_q1.cached_valid_taxon_name_id = taxon_names.id')
    .to_sql

  ::TaxonName.from('(' + s + ') as taxon_names').distinct
end

#validity_facetObject



833
834
835
836
837
838
839
840
# File 'lib/queries/taxon_name/filter.rb', line 833

def validity_facet
  return nil if validity.nil?
  if validity
    table[:cached_is_valid].eq(true)
  else
    table[:cached_is_valid].eq(false)
  end
end

#verbatim_name_facetObject



842
843
844
845
846
847
848
849
# File 'lib/queries/taxon_name/filter.rb', line 842

def verbatim_name_facet
  return nil if verbatim_name.nil?
  if verbatim_name
    table[:verbatim_name].not_eq(nil)
  else
    table[:verbatim_name].eq(nil)
  end
end

#with_etymology_facetObject

Returns Scope.

Returns:

  • Scope



594
595
596
597
598
599
# File 'lib/queries/taxon_name/filter.rb', line 594

def with_etymology_facet
  return nil if etymology.nil?
  etymology ?
    ::TaxonName.where.not(etymology: nil) :
    ::TaxonName.where(etymology: nil)
end

#with_nomenclature_codeArel::Nodes::Grouping?

Returns:

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


777
778
779
780
# File 'lib/queries/taxon_name/filter.rb', line 777

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

#with_nomenclature_groupArel::Nodes::Grouping?

Returns:

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


763
764
765
766
# File 'lib/queries/taxon_name/filter.rb', line 763

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

#year_facetObject



819
820
821
822
823
824
825
# File 'lib/queries/taxon_name/filter.rb', line 819

def year_facet
  return nil if year.blank?

  s = Date.new(@year.to_i)
  e = Date.new(@year.to_i, 12, 31)
  table[:cached_nomenclature_date].between(s..e)
end

#year_range_facetObject



827
828
829
830
831
# File 'lib/queries/taxon_name/filter.rb', line 827

def year_range_facet
  return nil if year_end.nil? && year_start.nil?
  s, e = [year_start, year_end].compact
  ::TaxonName.where(cached_nomenclature_date: (s..e))
end