Class: Queries::FieldOccurrence::Filter

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

Constant Summary collapse

PARAMS =
[
  *::Queries::CollectingEvent::Filter::BASE_PARAMS,
  *Queries::Concerns::DateRanges.params, # Fead to CE query. Revisit possibly.

  :biocuration_class_id,
  :biological_association_id,
  :biological_associations,
  # :biological_relationship_id,
  :collecting_event_id,
  :determiner_id,
  :field_occurrence_id,
  :geographic_area_id,
  # :collectors,
  # :current_determinations,
  :dates,
  :descendants,
  # :determiner_id_or,
  # :determiner_name_regex,
  # :determiners,
  # :dwc_indexed,
  :georeferences,
  # :import_dataset_id,
  :otu_id,
  # :sled_image_id,
  :spatial_geographic_areas,
  # :taxon_determination_id,
  # :taxon_name_id,
  # :validity,
  biocuration_class_id: [],
  biological_association_id: [],
  # biological_relationship_id: [],
  collecting_event_id: [],
  field_occurrence_id: [],
  determiner_id: [],
  geographic_area_id: [],
  # import_dataset_id: [],
  # is_type: [],
  otu_id: [],
  # taxon_name_id: [],
].inject([{}]) { |ary, k| k.is_a?(Hash) ? ary.last.merge!(k) : ary.unshift(k); ary }.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, #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, #taxon_name_query, #venn, #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, #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, included_annotator_facets, instantiated_base_filter, inverted_subqueries, #model_id_facet, #object_global_id_facet, #only_project?, params, #permitted_params, #process_url_into_params, #project_id_facet, query_name, #query_name, #set_nested_queries, #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

rubocop:disable Metric/MethodLength



173
174
175
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
209
210
211
212
213
# File 'lib/queries/field_occurrence/filter.rb', line 173

def initialize(query_params)
  super

  # Only CollectingEvent fields are permitted, for advanced nesting (e.g. tags on CEs), use collecting_event_query
  collecting_event_params = ::Queries::CollectingEvent::Filter.base_params + Queries::Concerns::DateRanges.params

  # project_id is handled during use
  @base_collecting_event_query = ::Queries::CollectingEvent::Filter.new(
    params.select{ |a, b| collecting_event_params.include?(a) } # maintain this to avoid sub query initialization for now
  )

  @biological_association_id = params[:biological_association_id]
  @biocuration_class_id = params[:biocuration_class_id]
  @biological_relationship_id = params[:biological_relationship_id] # TODO: no reference?
  @biological_associations = boolean_param(params, :biological_associations)
  @collectors = boolean_param(params, :collectors)
  @collecting_event_id = params[:collecting_event_id]
  @field_occurrence_id = params[:field_occurrence_id]
  @current_determinations = boolean_param(params, :current_determinations)
  @dates = boolean_param(params, :dates)
  @descendants = boolean_param(params, :descendants)
  @determiners = boolean_param(params, :determiners)
  @determiner_id = params[:determiner_id]
  @determiner_id_or = boolean_param(params, :determiner_id_or)
  @determiner_name_regex = params[:determiner_name_regex]
  @dwc_indexed = boolean_param(params, :dwc_indexed)
  @import_dataset_id = params[:import_dataset_id]
  @is_type = params[:is_type] || []
  @otu_descendants = boolean_param(params, :otu_descendants)
  @otu_id = params[:otu_id]
  @sled_image_id = (params[:sled_image_id].presence)
  @taxon_name_id = params[:taxon_name_id]
  @validity = boolean_param(params, :validity)

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

Instance Attribute Details

#base_collecting_event_queryObject

All params managed by CollectingEvent filter are available here as well



77
78
79
# File 'lib/queries/field_occurrence/filter.rb', line 77

def base_collecting_event_query
  @base_collecting_event_query
end

#biocuration_class_idArray

Returns of biocuration_class ids.

Returns:

  • (Array)

    of biocuration_class ids



117
118
119
# File 'lib/queries/field_occurrence/filter.rb', line 117

def biocuration_class_id
  @biocuration_class_id
end

#biological_association_idObject

Returns Array Matching records in this BiologicalAssociation.

Returns:

  • Array Matching records in this BiologicalAssociation



66
67
68
# File 'lib/queries/field_occurrence/filter.rb', line 66

def biological_association_id
  @biological_association_id
end

#biological_associationsTrue, ...

Returns true - is used in a bilogical association false - is not used in a biological association nil - not applied.

Returns:

  • (True, False, nil)

    true - is used in a bilogical association false - is not used in a biological association nil - not applied



62
63
64
# File 'lib/queries/field_occurrence/filter.rb', line 62

def biological_associations
  @biological_associations
end

#biological_relationship_idArray

Returns of biological_relationship#id.

Returns:

  • (Array)

    of biological_relationship#id



121
122
123
# File 'lib/queries/field_occurrence/filter.rb', line 121

def biological_relationship_id
  @biological_relationship_id
end

#collecting_event_idObject

Array

only return objects with these collecting event ID



74
75
76
# File 'lib/queries/field_occurrence/filter.rb', line 74

def collecting_event_id
  @collecting_event_id
end

#collectorsBoolean?

Returns true - A collector role exists false - A collector role exists nil - not applied.

Returns:

  • (Boolean, nil)

    true - A collector role exists false - A collector role exists nil - not applied



113
114
115
# File 'lib/queries/field_occurrence/filter.rb', line 113

def collectors
  @collectors
end

#current_determinationsBoolean?

Returns nil = TaxonDeterminations match regardless of current or historical true = TaxonDetermination must be .current false = TaxonDetermination must be .historical.

Returns:

  • (Boolean, nil)

    nil = TaxonDeterminations match regardless of current or historical true = TaxonDetermination must be .current false = TaxonDetermination must be .historical



101
102
103
# File 'lib/queries/field_occurrence/filter.rb', line 101

def current_determinations
  @current_determinations
end

#datesObject

Returns Boolean true - any of start/end date or verbatim date are populated false - none of start/end date or verbatim date are populated nil - ignored.

Returns:

  • Boolean true - any of start/end date or verbatim date are populated false - none of start/end date or verbatim date are populated nil - ignored



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

def dates
  @dates
end

#descendantsObject

Returns the value of attribute descendants.



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

def descendants
  @descendants
end

#determiner_idArray

Parameters:

  • determiner (Array or Person#id)

    one ore more people id

Returns:

  • (Array)


152
153
154
# File 'lib/queries/field_occurrence/filter.rb', line 152

def determiner_id
  @determiner_id
end

#determiner_id_orBoolean

Parameters:

  • determiner_id_or (String, nil)

    ‘false`, nil - treat the ids in determiner_id as “or” ’true’ - treat the ids in determiner_id as “and” (only collection objects with all and only all will match)

Returns:

  • (Boolean)


158
159
160
# File 'lib/queries/field_occurrence/filter.rb', line 158

def determiner_id_or
  @determiner_id_or
end

#determiner_name_regexObject

A PostgreSQL valid regular expression. Note that simple strings evaluate as wildcard matches. !! Probably shouldn’t expose to external API.

Returns:

  • String



164
165
166
# File 'lib/queries/field_occurrence/filter.rb', line 164

def determiner_name_regex
  @determiner_name_regex
end

#determinersBoolean?

Returns true - A determiner role exists false - No determiner role exists nil - not applied.

Returns:

  • (Boolean, nil)

    true - A determiner role exists false - No determiner role exists nil - not applied



107
108
109
# File 'lib/queries/field_occurrence/filter.rb', line 107

def determiners
  @determiners
end

#dwc_indexedTrue, ...

Returns true - index is built false - index is not built nil - not applied.

Returns:

  • (True, False, nil)

    true - index is built false - index is not built nil - not applied



127
128
129
# File 'lib/queries/field_occurrence/filter.rb', line 127

def dwc_indexed
  @dwc_indexed
end

#field_occurrence_idObject

Parameters:

  • Array (String, nil)

    or Integer of FieldOccurrence ids



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

def field_occurrence_id
  @field_occurrence_id
end

#geographic_areaTrue, ...

Returns true - has collecting event that has geographic_area false - does not have collecting event that has geographic area nil - not applied.

Returns:

  • (True, False, nil)

    true - has collecting event that has geographic_area false - does not have collecting event that has geographic area nil - not applied



147
148
149
# File 'lib/queries/field_occurrence/filter.rb', line 147

def geographic_area
  @geographic_area
end

#import_dataset_idArray

Returns of ImportDataset ids.

Returns:

  • (Array)

    of ImportDataset ids



56
57
58
# File 'lib/queries/field_occurrence/filter.rb', line 56

def import_dataset_id
  @import_dataset_id
end

#is_typeArray?

Returns one of ‘holotype`, `lectotype` etc.

nil - not applied.

Returns:

  • (Array, nil)

    one of ‘holotype`, `lectotype` etc.

    nil - not applied
    


132
133
134
# File 'lib/queries/field_occurrence/filter.rb', line 132

def is_type
  @is_type
end

#otu_idArray?

Returns Otu ids, matches on the TaxonDetermination, see also current_determinations.

Parameters:

  • (Array)

Returns:

  • (Array, nil)

    Otu ids, matches on the TaxonDetermination, see also current_determinations



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

def otu_id
  @otu_id
end

#sled_image_idSledImage#id?

Returns:



135
136
137
# File 'lib/queries/field_occurrence/filter.rb', line 135

def sled_image_id
  @sled_image_id
end

#taxon_nameTrue, ...

Returns true - Otu has taxon name false - Otu without taxon name nil - not applied.

Returns:

  • (True, False, nil)

    true - Otu has taxon name false - Otu without taxon name nil - not applied



141
142
143
# File 'lib/queries/field_occurrence/filter.rb', line 141

def taxon_name
  @taxon_name
end

#taxon_name_idArray of Protonym.id?

Return all collection objects determined as an Otu that is self or descendant linked to this TaxonName

Returns:

  • (Array of Protonym.id, nil)

    return all collection objects determined as an Otu that is self or descendant linked to this TaxonName



87
88
89
# File 'lib/queries/field_occurrence/filter.rb', line 87

def taxon_name_id
  @taxon_name_id
end

#validityBoolean?

Returns nil = Match against all ancestors, valid or invalid true = Match against only valid ancestors false = Match against only invalid ancestors.

Returns:

  • (Boolean, nil)

    nil = Match against all ancestors, valid or invalid true = Match against only valid ancestors false = Match against only invalid ancestors



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

def validity
  @validity
end

Instance Method Details

#and_clausesObject



541
542
543
544
545
546
# File 'lib/queries/field_occurrence/filter.rb', line 541

def and_clauses
  [
    collecting_event_id_facet,
    field_occurrence_id_facet,
  ]
end

#base_collecting_event_query_facetObject



444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/queries/field_occurrence/filter.rb', line 444

def base_collecting_event_query_facet
  # Turn project_id off and check for a truly empty query
  base_collecting_event_query.project_id = nil
  return nil if base_collecting_event_query.all(true).nil?

  # Turn project_id back on
  base_collecting_event_query.project_id = project_id

  s = 'WITH query_ce_base_co AS (' + base_collecting_event_query.all.to_sql + ') ' +
    ::FieldOccurrence
    .joins('JOIN query_ce_base_co as query_ce_base_co1 on query_ce_base_co1.id = field_occurrences.collecting_event_id')
    .to_sql

  ::FieldOccurrence.from('(' + s + ') as field_occurrences').distinct
end

#biocuration_facetObject



324
325
326
327
328
# File 'lib/queries/field_occurrence/filter.rb', line 324

def biocuration_facet
  return nil if biocuration_class_id.empty?
  ::FieldOccurrence::BiologicalFieldOccurrence.joins(:biocuration_classifications)
    .where(biocuration_classifications: { biocuration_class_id: })
end

#biological_association_id_facetObject

TODO: turn into UNION!



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

def biological_association_id_facet
  return nil if biological_association_id.empty?
  b = ::BiologicalAssociation.where(id: biological_association_id)
  s = 'WITH query_ba_id_co AS (' + b.all.to_sql + ') ' +
    ::FieldOccurrence
    .joins("LEFT JOIN query_ba_id_co as query_ba_id_co1 on field_occurrences.id = query_ba_id_co1.biological_association_subject_id AND query_ba_id_co1.biological_association_subject_type = 'FieldOccurrence'")
    .joins("LEFT JOIN query_ba_id_co as query_ba_id_co2 on field_occurrences.id = query_ba_id_co2.biological_association_object_id AND query_ba_id_co2.biological_association_object_type = 'FieldOccurrence'")
    .where('(query_ba_id_co1.id) IS NOT NULL OR (query_ba_id_co2.id IS NOT NULL)')
    .to_sql

  ::FieldOccurrence.from('(' + s + ') as field_occurrences').distinct
end

#biological_association_query_facetObject

TODO: turn into UNION!



507
508
509
510
511
512
513
514
515
516
517
# File 'lib/queries/field_occurrence/filter.rb', line 507

def biological_association_query_facet
  return nil if biological_association_query.nil?
  s = 'WITH query_ba_co AS (' + biological_association_query.all.to_sql + ') ' +
    ::FieldOccurrence
    .joins("LEFT JOIN query_ba_co as query_ba_co1 on field_occurrences.id = query_ba_co1.biological_association_subject_id AND query_ba_co1.biological_association_subject_type = 'FieldOccurrence'")
    .joins("LEFT JOIN query_ba_co as query_ba_co2 on field_occurrences.id = query_ba_co2.biological_association_object_id AND query_ba_co2.biological_association_object_type = 'FieldOccurrence'")
    .where('(query_ba_co1.id) IS NOT NULL OR (query_ba_co2.id IS NOT NULL)')
    .to_sql

  ::FieldOccurrence.from('(' + s + ') as field_occurrences').distinct
end

#biological_associations_facetObject



484
485
486
487
488
489
490
# File 'lib/queries/field_occurrence/filter.rb', line 484

def biological_associations_facet
  return nil if biological_associations.nil?
  a = ::FieldOccurrence.joins(:biological_associations)
  b = ::FieldOccurrence.joins(:related_biological_associations)

  referenced_klass_union([a, b])
end

#biological_relationship_id_facetObject



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

def biological_relationship_id_facet
  return nil if biological_relationship_id.empty?
  ::FieldOccurrence.with_biological_relationship_id(biological_relationship_id)
end

#collecting_event_id_facetObject



347
348
349
350
# File 'lib/queries/field_occurrence/filter.rb', line 347

def collecting_event_id_facet
  return nil if collecting_event_id.empty?
  table[:collecting_event_id].in(collecting_event_id)
end

#collecting_event_query_facetObject



519
520
521
522
523
524
525
526
527
# File 'lib/queries/field_occurrence/filter.rb', line 519

def collecting_event_query_facet
  return nil if collecting_event_query.nil?
  s = 'WITH query_ce_co AS (' + collecting_event_query.all.select(:id).to_sql + ') ' +
    ::FieldOccurrence
    .joins('JOIN query_ce_co as query_ce_co1 on query_ce_co1.id = field_occurrences.collecting_event_id')
    .to_sql

  ::FieldOccurrence.from('(' + s + ') as field_occurrences').distinct
end

#collecting_event_tableArel::Table

Returns:

  • (Arel::Table)


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

def collecting_event_table
  ::CollectingEvent.arel_table
end

#dates_facetObject

TODO: Probably just move to CE



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/queries/field_occurrence/filter.rb', line 417

def dates_facet
  return nil if dates.nil?
  if dates
    ::FieldOccurrence.left_joins(:collecting_event).where(
      'start_date_year IS NOT NULL OR ' \
      'start_date_month IS NOT NULL OR ' \
      'start_date_day IS NOT NULL OR ' \
      'end_date_year IS NOT NULL OR ' \
      'end_date_month IS NOT NULL OR ' \
      'end_date_day IS NOT NULL OR ' \
      'verbatim_date IS NOT NULL'
    )
  else
    ::FieldOccurrence.left_joins(:collecting_event).where(
      collecting_event: {
        start_date_year: nil,
        start_date_month: nil,
        start_date_day: nil,
        end_date_year: nil,
        end_date_month: nil,
        end_date_day: nil,
        verbatim_date: nil,
      },
    )
  end
end

#determiner_facetObject

TODO: DRY with Source (author), TaxonName, etc. See Queries::ColletingEvent::Filter for other use



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/queries/field_occurrence/filter.rb', line 279

def determiner_facet
  return nil if determiner_id.empty?
  tt = table

  o = ::TaxonDetermination.arel_table
  r = ::Role.arel_table

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

  c = r.alias('det_r1')

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

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

  b = b.where(e.and(f))
  b = b.group(a['id'])
  b = b.having(a['id'].count.eq(determiner_id.length)) unless determiner_id_or

  b = b.as('det_z1_')

  ::FieldOccurrence.joins(Arel::Nodes::InnerJoin.new(b, Arel::Nodes::On.new(b['taxon_determination_object_id'].eq(tt['id']))))
end

#determiner_name_regex_facetObject



319
320
321
322
# File 'lib/queries/field_occurrence/filter.rb', line 319

def determiner_name_regex_facet
  return nil if determiner_name_regex.nil?
  ::FieldOccurrence.joins(:determiners).where('people.cached ~* ?', determiner_name_regex)
end

#determiners_facetObject



310
311
312
313
314
315
316
317
# File 'lib/queries/field_occurrence/filter.rb', line 310

def determiners_facet
  return nil if determiners.nil?
  if determiners
    ::FieldOccurrence.joins(:determiners)
  else
    ::FieldOccurrence.where.missing(:determiners)
  end
end

#dwc_indexed_facetObject



340
341
342
343
344
345
# File 'lib/queries/field_occurrence/filter.rb', line 340

def dwc_indexed_facet
  return nil if dwc_indexed.nil?
  dwc_indexed ?
    ::FieldOccurrence.dwc_indexed :
    ::FieldOccurrence.dwc_not_indexed
end

#dwc_occurrence_query_facetObject



529
530
531
532
533
534
535
536
537
538
539
# File 'lib/queries/field_occurrence/filter.rb', line 529

def dwc_occurrence_query_facet
  return nil if dwc_occurrence_query.nil?

  s = ::FieldOccurrence
    .with(query_dwc_fo: dwc_occurrence_query.all.select(:dwc_occurrence_object_id, :dwc_occurrence_object_type, :id))
    .joins(:dwc_occurrence)
    .joins('JOIN query_dwc_fo as query_dwc_fo1 on query_dwc_fo1.id = dwc_occurrences.id')
    .to_sql

  ::FieldOccurrence.from('(' + s + ') as field_occurrences').distinct
end

#field_occurrence_id_facetObject



266
267
268
269
# File 'lib/queries/field_occurrence/filter.rb', line 266

def field_occurrence_id_facet
  return nil if field_occurrence_id.empty?
  table[:id].in(field_occurrence_id)
end

#import_dataset_id_facetObject



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

def import_dataset_id_facet
  return nil if import_dataset_id.blank?
  ::FieldOccurrence.joins(:related_origin_relationships)
    .where(origin_relationships: {old_object_id: import_dataset_id, old_object_type: 'ImportDataset'})
end

#merge_clausesObject



548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/queries/field_occurrence/filter.rb', line 548

def merge_clauses
  [
    #  import_dataset_id_facet,
    biological_association_id_facet,
    base_collecting_event_query_facet,
    biological_association_query_facet,
    collecting_event_query_facet,
    dwc_occurrence_query_facet,
    otu_query_facet,
    #  taxon_name_query_facet,
    biocuration_facet,
    biological_associations_facet,
    #  biological_relationship_id_facet,
    dates_facet,
    determiner_facet,
    # determiner_name_regex_facet,
    determiners_facet,
    # dwc_indexed_facet,
    otu_id_facet,
    # sled_image_facet,
    # taxon_name_id_facet,
  ]
end

#otu_id_facetObject



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/queries/field_occurrence/filter.rb', line 352

def otu_id_facet
  return nil if otu_id.empty?

  w = taxon_determination_table[:taxon_determination_object_id].eq(table[:id])
    .and(taxon_determination_table[:otu_id].in(otu_id))

  if current_determinations
    w = w.and(taxon_determination_table[:position].eq(1))
  elsif current_determinations == false
    w = w.and(taxon_determination_table[:position].gt(1))
  end

  ::FieldOccurrence.where(
    ::TaxonDetermination.where(w).arel.exists
  )
end

#otu_query_facetObject



472
473
474
475
476
477
478
479
480
481
482
# File 'lib/queries/field_occurrence/filter.rb', line 472

def otu_query_facet
  return nil if otu_query.nil?
  s = 'WITH query_otu_co AS (' + otu_query.all.to_sql + ') ' +
    ::FieldOccurrence
    .joins(:taxon_determinations)
    .joins('JOIN query_otu_co as query_otu_co1 on query_otu_co1.id = taxon_determinations.otu_id')
    .where(taxon_determinations: { position: 1 })
    .to_sql

  ::FieldOccurrence.from('(' + s + ') as field_occurrences').distinct
end

#otu_tableArel::Table

Returns:

  • (Arel::Table)


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

def otu_table
  ::Otu.arel_table
end

#sled_image_facetObject



330
331
332
333
# File 'lib/queries/field_occurrence/filter.rb', line 330

def sled_image_facet
  return nil if sled_image_id.nil?
  ::FieldOccurrence::BiologicalFieldOccurrence.joins(:depictions).where('depictions.sled_image_id = ?', sled_image_id)
end

#taxon_determination_tableArel::Table

Returns:

  • (Arel::Table)


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

def taxon_determination_table
  ::TaxonDetermination.arel_table
end

#taxon_name_id_facetObject

TODO: use filter proxy



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
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/queries/field_occurrence/filter.rb', line 370

def taxon_name_id_facet
  return nil if taxon_name_id.empty?

  q = nil
  z = nil

  if descendants
    h = Arel::Table.new(:taxon_name_hierarchies)
    t = ::TaxonName.arel_table

    q = table.join(taxon_determination_table, Arel::Nodes::InnerJoin).on(
      table[:id].eq(taxon_determination_table[:taxon_determination_object_id])
    ).join(otu_table, Arel::Nodes::InnerJoin).on(
      taxon_determination_table[:otu_id].eq(otu_table[:id])
    ).join(t, Arel::Nodes::InnerJoin).on(
      otu_table[:taxon_name_id].eq(t[:id])
    ).join(h, Arel::Nodes::InnerJoin).on(
      t[:id].eq(h[:descendant_id])
    )
    z = h[:ancestor_id].in(taxon_name_id)

    if validity == true
      z = z.and(t[:cached_valid_taxon_name_id].eq(t[:id]))
    elsif validity == false
      z = z.and(t[:cached_valid_taxon_name_id].not_eq(t[:id]))
    end

    if current_determinations == true
      z = z.and(taxon_determination_table[:position].eq(1))
    elsif current_determinations == false
      z = z.and(taxon_determination_table[:position].gt(1))
    end
  else
    q = ::FieldOccurrence.joins(taxon_determinations: [:otu])
      .where(otus: { taxon_name_id: })

    if current_determinations
      q = q.where(taxon_determinations: { position: 1 })
    end

    return q
  end

  ::FieldOccurrence.joins(q.join_sources).where(z).distinct
end