Class: Queries::CollectionObject::Filter

Inherits:
Object
  • Object
show all
Includes:
Queries::Concerns::Identifiers, Queries::Concerns::Tags, Queries::Concerns::Users
Defined in:
lib/queries/collection_object/filter.rb

Overview

!! does not inherit from base query

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Filter

Returns a new instance of Filter.

Parameters:

  • args (Hash)

    are permitted params



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/queries/collection_object/filter.rb', line 100

def initialize(params)
  params.reject!{ |_k, v| v.blank? } # dump all entries with empty values

  @recent = params[:recent].blank? ? false : true

  @collecting_event_ids = params[:collecting_event_ids] || []

  @otu_ids = params[:otu_ids] || []
  @is_type = params[:is_type] || []

  @otu_descendants = (params[:otu_descendants]&.downcase == 'true' ? true : false) if !params[:otu_descendants].nil?

  @ancestor_id = params[:ancestor_id].blank? ? nil : params[:ancestor_id]
  @collection_object_type = params[:collection_object_type].blank? ? nil : params[:collection_object_type]

  @current_determinations = (params[:current_determinations]&.downcase == 'true' ? true : false) if !params[:current_determinations].nil?
  @validity = (params[:validity]&.downcase == 'true' ? true : false) if !params[:validity].nil?

  @on_loan = (params[:on_loan]&.downcase == 'true' ? true : false) if !params[:on_loan].nil?
  @loaned = (params[:loaned]&.downcase == 'true' ? true : false) if !params[:loaned].nil?
  @never_loaned = (params[:never_loaned]&.downcase == 'true' ? true : false) if !params[:never_loaned].nil?

  @biocuration_class_ids = params[:biocuration_class_ids] || []

  @biological_relationship_ids = params[:biological_relationship_ids] || []



  # This needs to be params[:collecting_event], for now, exclude keyword_ids ... (and!?)
  @collecting_event_query = Queries::CollectingEvent::Filter.new(params.select{|a,b| a.to_sym != :keyword_ids} )

  @dwc_indexed =  (params[:dwc_indexed]&.downcase == 'true' ? true : false) if !params[:dwc_indexed].nil?

  @type_specimen_taxon_name_id = params[:type_specimen_taxon_name_id].blank? ? nil : params[:type_specimen_taxon_name_id]

  @sled_image_id = params[:sled_image_id].blank? ? nil : params[:sled_image_id]
  @depicted = (params[:depicted]&.downcase == 'true' ? true : false) if !params[:depicted].nil?

  @repository_id = params[:repository_id].blank? ? nil : params[:repository_id]

  set_identifier(params)
  set_tags_params(params)
  set_user_dates(params)
end

Instance Attribute Details

#ancestor_idProtonym.id?

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

Returns:

  • (Protonym.id, nil)

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



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

def ancestor_id
  @ancestor_id
end

#biocuration_class_idsArray

Returns of biocuration_class ids.

Returns:

  • (Array)

    of biocuration_class ids



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

def biocuration_class_ids
  @biocuration_class_ids
end

#biological_relationship_idsArray

Returns of biological_relationship_ids.

Returns:

  • (Array)

    of biological_relationship_ids



72
73
74
# File 'lib/queries/collection_object/filter.rb', line 72

def biological_relationship_ids
  @biological_relationship_ids
end

#collecting_event_idsObject

Array

only return objects with this collecting event ID



31
32
33
# File 'lib/queries/collection_object/filter.rb', line 31

def collecting_event_ids
  @collecting_event_ids
end

#collecting_event_queryObject

All params managed by CollectingEvent filter are available here as well



34
35
36
# File 'lib/queries/collection_object/filter.rb', line 34

def collecting_event_query
  @collecting_event_query
end

#collection_object_typeObject

Parameters:

  • one (String, nil)

    of 'Specimen', 'Lot', or 'RangedLot'



21
22
23
# File 'lib/queries/collection_object/filter.rb', line 21

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



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

def current_determinations
  @current_determinations
end

#depictedTrue, ...

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



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

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



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

def dwc_indexed
  @dwc_indexed
end

#is_typeArray

Returns of type_materials.

Returns:

  • (Array)

    of type_materials



94
95
96
# File 'lib/queries/collection_object/filter.rb', line 94

def is_type
  @is_type
end

#loanedTrue?

Returns:

  • (True, nil)


61
62
63
# File 'lib/queries/collection_object/filter.rb', line 61

def loaned
  @loaned
end

#never_loanedTrue?

Returns:

  • (True, nil)


64
65
66
# File 'lib/queries/collection_object/filter.rb', line 64

def never_loaned
  @never_loaned
end

#on_loanTrue?

Returns:

  • (True, nil)


58
59
60
# File 'lib/queries/collection_object/filter.rb', line 58

def on_loan
  @on_loan
end

#otu_idsArray?

Returns a list of Otu ids, matches one ot one only.

Returns:

  • (Array, nil)

    a list of Otu ids, matches one ot one only



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

def otu_ids
  @otu_ids
end

#recentBoolen?

Parameters:

  • 'true' (String, nil)
    • order by updated_at

    'false', nil - do not apply ordering

Returns:

  • (Boolen, nil)


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

def recent
  @recent
end

#repository_idRepository#id?

Returns:



90
91
92
# File 'lib/queries/collection_object/filter.rb', line 90

def repository_id
  @repository_id
end

#sled_image_idSledImage#id?

Returns:



97
98
99
# File 'lib/queries/collection_object/filter.rb', line 97

def sled_image_id
  @sled_image_id
end

#type_specimen_taxon_name_idProtonym#id?

Returns:



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

def type_specimen_taxon_name_id
  @type_specimen_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



49
50
51
# File 'lib/queries/collection_object/filter.rb', line 49

def validity
  @validity
end

Instance Method Details

#allActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/queries/collection_object/filter.rb', line 323

def all
  a = and_clauses
  b = merge_clauses
  # q = nil
  if a && b
    q = b.where(a).distinct
  elsif a
    q = ::CollectionObject.where(a).distinct
  elsif b
    q = b.distinct
  else
    q = ::CollectionObject.all
  end

  q = q.order(updated_at: :desc) if recent
  q
end

#ancestors_facetObject



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

def ancestors_facet
  return nil if ancestor_id.nil?
  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[:biological_collection_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].eq(ancestor_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

  ::CollectionObject.joins(q.join_sources).where(z)
end

#and_clausesActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


258
259
260
261
262
263
264
265
266
267
268
# File 'lib/queries/collection_object/filter.rb', line 258

def and_clauses
  clauses = base_and_clauses

  return nil if clauses.empty?

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

#base_and_clausesArray

Returns:

  • (Array)


271
272
273
274
275
276
277
278
279
280
281
# File 'lib/queries/collection_object/filter.rb', line 271

def base_and_clauses
  clauses = []

  clauses += [
    collecting_event_ids_facet,
    type_facet,
    repository_facet
  ]
  clauses.compact!
  clauses
end

#base_merge_clausesObject



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
309
# File 'lib/queries/collection_object/filter.rb', line 283

def base_merge_clauses
  clauses = []
  clauses += collecting_event_merge_clauses + collecting_event_and_clauses

  clauses += [
    otus_facet,
    type_material_facet,
    type_material_type_facet,
    ancestors_facet,
    matching_keyword_ids,   # See Queries::Concerns::Tags
    created_updated_facet, # See Queries::Concerns::Users
    identifier_between_facet,
    identifier_facet,
    identifier_namespace_facet,
    loaned_facet,
    on_loan_facet,
    dwc_indexed_facet,
    never_loaned_facet,
    biocuration_facet,
    biological_relationship_ids_facet,
    sled_image_facet,
    depicted_facet,
  ]

  clauses.compact!
  clauses
end

#base_queryObject



150
151
152
# File 'lib/queries/collection_object/filter.rb', line 150

def base_query
  ::CollectionObject.select('collection_objects.*')
end

#biocuration_facetObject



179
180
181
182
# File 'lib/queries/collection_object/filter.rb', line 179

def biocuration_facet
  return nil if biocuration_class_ids.empty?
  ::CollectionObject::BiologicalCollectionObject.joins(:biocuration_classifications).where(biocuration_classifications: {biocuration_class_id: biocuration_class_ids}) 
end

#biological_relationship_ids_facetObject



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

def biological_relationship_ids_facet
  return nil if biological_relationship_ids.empty?
  ::CollectionObject.with_biological_relationship_ids(biological_relationship_ids)
end

#collecting_event_and_clausesObject



247
248
249
250
251
252
253
254
255
# File 'lib/queries/collection_object/filter.rb', line 247

def collecting_event_and_clauses
  c = []

  # Convert base and clauses to merge clauses
  collecting_event_query.base_and_clauses.each do |i|
    c.push ::CollectionObject.joins(:collecting_event).where( i ) 
  end
  c
end

#collecting_event_ids_facetObject

Returns Scope.

Returns:

  • Scope



227
228
229
230
# File 'lib/queries/collection_object/filter.rb', line 227

def collecting_event_ids_facet
  return nil if collecting_event_ids.empty?
  table[:collecting_event_id].eq_any(collecting_event_ids)
end

#collecting_event_merge_clausesObject



237
238
239
240
241
242
243
244
245
# File 'lib/queries/collection_object/filter.rb', line 237

def collecting_event_merge_clauses
  c = []

  # Convert base and clauses to merge clauses
  collecting_event_query.base_merge_clauses.each do |i|
    c.push ::CollectionObject.joins(:collecting_event).merge( i ) 
  end
  c
end

#collecting_event_tableArel::Table

Returns:

  • (Arel::Table)


155
156
157
# File 'lib/queries/collection_object/filter.rb', line 155

def collecting_event_table 
  ::CollectingEvent.arel_table
end

#depicted_facetObject



189
190
191
192
# File 'lib/queries/collection_object/filter.rb', line 189

def depicted_facet 
  return nil if !depicted
  ::CollectionObject::BiologicalCollectionObject.joins(:depictions) 
end

#depiction_tableArel::Table

Returns:

  • (Arel::Table)


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

def depiction_table 
  ::Depiction.arel_table
end

#dwc_indexed_facetObject



219
220
221
222
223
224
# File 'lib/queries/collection_object/filter.rb', line 219

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

#geographic_area_scopeScope

Returns:

  • (Scope)


416
417
418
419
420
421
422
423
424
425
426
# File 'lib/queries/collection_object/filter.rb', line 416

def geographic_area_scope
  # This could be simplified if the AJAX selector returned a geographic_item_id rather than a GeographicAreaId
  target_geographic_item_ids = []
  geographic_area_ids.each do |ga_id|
    target_geographic_item_ids.push(::GeographicArea.joins(:geographic_items)
      .find(ga_id)
      .default_geographic_item.id)
  end
  ::CollectionObject.joins(:geographic_items)
    .where(::GeographicItem.contained_by_where_sql(target_geographic_item_ids))
end

#loaned_facetObject



204
205
206
207
# File 'lib/queries/collection_object/filter.rb', line 204

def loaned_facet
  return nil unless loaned 
  ::CollectionObject.loaned
end

#merge_clausesActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


312
313
314
315
316
317
318
319
320
# File 'lib/queries/collection_object/filter.rb', line 312

def merge_clauses
  clauses = base_merge_clauses        
  return nil if clauses.empty?
  a = clauses.shift
  clauses.each do |b|
    a = a.merge(b)
  end
  a
end

#never_loaned_facetObject



209
210
211
212
# File 'lib/queries/collection_object/filter.rb', line 209

def never_loaned_facet
  return nil unless never_loaned 
  ::CollectionObject.never_loaned
end

#on_loan_facetObject



214
215
216
217
# File 'lib/queries/collection_object/filter.rb', line 214

def on_loan_facet
  return nil unless on_loan
  ::CollectionObject.on_loan
end

#otu_tableArel::Table

Returns:

  • (Arel::Table)


160
161
162
# File 'lib/queries/collection_object/filter.rb', line 160

def otu_table 
  ::Otu.arel_table
end

#otus_facetScope

Returns:

  • (Scope)


366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/queries/collection_object/filter.rb', line 366

def otus_facet
  return nil if otu_ids.empty?

  w = taxon_determination_table[:biological_collection_object_id].eq(table[:id])
    .and( taxon_determination_table[:otu_id].eq_any(otu_ids) )

  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

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

#repository_facetObject



232
233
234
235
# File 'lib/queries/collection_object/filter.rb', line 232

def repository_facet
  return nil if repository_id.blank?
  table[:repository_id].eq(repository_id)
end

#sled_image_facetObject



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

def sled_image_facet 
  return nil if sled_image_id.nil?
  ::CollectionObject::BiologicalCollectionObject.joins(:depictions).where("depictions.sled_image_id = ?", sled_image_id)
end

#tableArel::Table

Returns:

  • (Arel::Table)


146
147
148
# File 'lib/queries/collection_object/filter.rb', line 146

def table
  ::CollectionObject.arel_table
end

#taxon_determination_tableArel::Table

Returns:

  • (Arel::Table)


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

def taxon_determination_table 
  ::TaxonDetermination.arel_table
end

#type_facetObject



184
185
186
187
# File 'lib/queries/collection_object/filter.rb', line 184

def type_facet 
  return nil if collection_object_type.nil?
  table[:type].eq(collection_object_type)
end

#type_material_facetScope

Returns:

  • (Scope)


342
343
344
345
346
347
348
349
350
351
# File 'lib/queries/collection_object/filter.rb', line 342

def type_material_facet
  return nil if type_specimen_taxon_name_id.nil?

  w = type_materials_table[:collection_object_id].eq(table[:id])
    .and( type_materials_table[:protonym_id].eq(type_specimen_taxon_name_id) )

  ::CollectionObject.where(
    ::TypeMaterial.where(w).arel.exists
  )
end

#type_material_type_facetScope

Returns:

  • (Scope)


354
355
356
357
358
359
360
361
362
363
# File 'lib/queries/collection_object/filter.rb', line 354

def type_material_type_facet
  return nil if is_type.empty?

  w = type_materials_table[:collection_object_id].eq(table[:id])
    .and( type_materials_table[:type_type].eq_any(is_type) )

  ::CollectionObject.where(
    ::TypeMaterial.where(w).arel.exists
  )
end

#type_materials_tableArel::Table

Returns:

  • (Arel::Table)


165
166
167
# File 'lib/queries/collection_object/filter.rb', line 165

def type_materials_table 
  ::TypeMaterial.arel_table
end