Class: Queries::Image::Filter

Inherits:
Query::Filter show all
Includes:
Concerns::Attributions, Concerns::Citations, Concerns::Notes, Concerns::Tags
Defined in:
lib/queries/image/filter.rb

Constant Summary collapse

PARAMS =
[
  :biocuration_class_id,
  :collection_object_id,
  :collection_object_scope,
  :depiction_caption,
  :depiction_caption_exact,
  :depiction_object_type,
  :depictions,
  :field_occurrence_id,
  :field_occurrence_scope,
  :freeform_svg,
  :image_id,
  :metadata_depiction,
  :otu_id,
  :otu_scope,
  :sled_image,
  :sled_image_id,
  :source_id,
  :sqed_image,
  :taxon_name_id,
  :taxon_name_id_target,
  :type_material_depictions,

  biocuration_class_id: [],
  collection_object_id: [],
  collection_object_scope: [],
  depiction_object_type: [],
  field_occurrence_id: [],
  field_occurrence_scope: [],
  image_id: [],
  otu_id: [],
  otu_scope: [],
  sled_image_id: [],
  source_id: [],
  taxon_name_id: [],
].freeze

Constants inherited from Query::Filter

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

Instance Attribute Summary collapse

Attributes inherited from Query::Filter

#anatomical_part_query, #api, #asserted_distribution_query, #asserted_environment_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 inherited from Query::Filter

#all, #all_and_clauses, #all_merge_clauses, #and_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, #model_id_facet, #object_global_id_facet, #only_project?, #only_project_or_less?, #paging_state, params, #permitted_params, #process_url_into_params, #project_id_facet, query_name, #raise_multiple_nested_queries, #raise_unregistered_nested_query, #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:

  • query_params (Hash)


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

def initialize(query_params)
  super

  @biocuration_class_id = params[:biocuration_class_id]
  @collection_object_id = params[:collection_object_id]
  @collection_object_scope = params[:collection_object_scope]
  @depiction_caption = params[:depiction_caption]
  @depiction_caption_exact = boolean_param(params, :depiction_caption_exact)
  @depiction_object_type = params[:depiction_object_type]
  @depictions = boolean_param(params, :depictions)
  @field_occurrence_id = params[:field_occurrence_id]
  @field_occurrence_scope = params[:field_occurrence_scope]
  @freeform_svg = boolean_param(params, :freeform_svg)
  @image_id = params[:image_id]
  @metadata_depiction = boolean_param(params, :metadata_depiction)
  @otu_id = params[:otu_id]
  @otu_scope = params[:otu_scope]
  @sled_image = boolean_param(params, :sled_image)
  @sled_image_id = params[:sled_image_id]
  @source_id = params[:source_id]
  @sqed_depiction_id = params[:sqed_depiction_id]
  @sqed_image = boolean_param(params, :sqed_image)
  @taxon_name_id = params[:taxon_name_id]
  @taxon_name_id_target = params[:taxon_name_id_target]
  @type_material_depictions = boolean_param(params, :type_material_depictions)

  set_attributions_params(params)
  set_citations_params(params)
  set_notes_params(params)
  set_tags_params(params)
end

Instance Attribute Details

#biocuration_class_id ⇒ Array

Returns of biocuration_class ids.

Returns:

  • (Array) —

    of biocuration_class ids



125
126
127
# File 'lib/queries/image/filter.rb', line 125

def biocuration_class_id
  @biocuration_class_id
end

#collection_object_id ⇒ Array

Returns images depicting collecting_object.

Returns:

  • (Array) —

    images depicting collecting_object



48
49
50
# File 'lib/queries/image/filter.rb', line 48

def collection_object_id
  @collection_object_id
end

#collection_object_scope ⇒ Object

Parameters:

  • collection_object_scope —

    One or more of:

    :all (default, includes all below)
    
    :collection_objects (those on the collection_object)
    :observations (those on just the CollectionObject observations)
    :collecting_events (those on the associated collecting event)


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

def collection_object_scope
  @collection_object_scope
end

#depiction_caption ⇒ String

Parameters:

  • depiction_caption (String) —

    Images match if they have a depiction whose caption matches depiction_caption.

Returns:

  • (String)


171
172
173
# File 'lib/queries/image/filter.rb', line 171

def depiction_caption
  @depiction_caption
end

#depiction_caption_exact ⇒ Boolean

Parameters:

  • depiction_caption_exact (Boolean) —

    depiction_caption must match exactly if true.

Returns:

  • (Boolean)


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

def depiction_caption_exact
  @depiction_caption_exact
end

#depiction_object_type ⇒ Array

Restricts Images to those that depict these classes

Parameters:

  • depiction_object_type (String, Array) —

    one or more names of classes.

Returns:

  • (Array)


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

def depiction_object_type
  @depiction_object_type
end

#depictions ⇒ Boolean?

Returns true - image is used (in a depiction) false - image is not used nil - either.

Returns:

  • (Boolean, nil) —

    true - image is used (in a depiction) false - image is not used nil - either



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

def depictions
  @depictions
end

#field_occurrence_id ⇒ Array

Returns images depicting field_occurrence.

Returns:

  • (Array) —

    images depicting field_occurrence



67
68
69
# File 'lib/queries/image/filter.rb', line 67

def field_occurrence_id
  @field_occurrence_id
end

#field_occurrence_scope ⇒ Object

Parameters:

  • field_occurrence_scope —

    One or more of:

    :all (default, includes all below)
    
    :field_occurrence (those on the field_occurrence)
    :observations (those on just the FieldOccurrence observations)
    :collecting_events (those on the associated collecting event)


76
77
78
# File 'lib/queries/image/filter.rb', line 76

def field_occurrence_scope
  @field_occurrence_scope
end

#freeform_svg ⇒ Boolean?

Returns true - image is used (in a depiction) that has svg false - is not a true image nil - ignored.

Returns:

  • (Boolean, nil) —

    true - image is used (in a depiction) that has svg false - is not a true image nil - ignored



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

def freeform_svg
  @freeform_svg
end

#image_id ⇒ Array

Returns:

  • (Array)


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

def image_id
  @image_id
end

#metadata_depiction ⇒ Boolean?

Returns true - image has depiction(s) where is_metadata_depiction is true false - image has depiction(s) where is_metadata_depiction is not true nil - ignored.

Returns:

  • (Boolean, nil) —

    true - image has depiction(s) where is_metadata_depiction is true false - image has depiction(s) where is_metadata_depiction is not true nil - ignored



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

def 
  @metadata_depiction
end

#otu_id ⇒ Array

Returns images depicting otus.

Returns:

  • (Array) —

    images depicting otus



92
93
94
# File 'lib/queries/image/filter.rb', line 92

def otu_id
  @otu_id
end

#otu_scope ⇒ Array

A sub scope of sorts. Purpose is to gather all images possible under an OTU that are of an OTU, CollectionObject or Observation.

!! Must be used with an otu_id !!

Parameters:

  • otu_scope —

    One or more of:

    :all (default, includes all below)
    
    :otu (those on the OTU)
    :otu_observations (those on just the OTU)
    :collection_object_observations (those on just those determined as the OTU)
    :collection_objects (those on just those on the collection objects)
    :field_occurrences (those on just field occurrences)
    :type_material (those on CollectionObjects that have TaxonName used in OTU)
    :type_material_observations (those on CollectionObjects that have TaxonName used in OTU)
    
    :coordinate_otus  If present adds both.  Use downstream substraction to to diffs of with/out?

Returns:

  • (Array)


118
119
120
# File 'lib/queries/image/filter.rb', line 118

def otu_scope
  @otu_scope
end

#sled_image ⇒ Boolean

Returns true - yes false - is not nil - ignored.

Returns:

  • (Boolean) —

    true - yes false - is not nil - ignored



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

def sled_image
  @sled_image
end

#sled_image_id ⇒ Array

Returns:

  • (Array)


128
129
130
# File 'lib/queries/image/filter.rb', line 128

def sled_image_id
  @sled_image_id
end

#source_id ⇒ Array

Parameters:

  • source_id (Array or Source#id) —

    One or more source_ids cited by matching images. Note that a given image may only have one citation, so the 'all' option doesn't exist here.

Returns:

  • (Array)


159
160
161
# File 'lib/queries/image/filter.rb', line 159

def source_id
  @source_id
end

#sqed_depiction_id ⇒ Array

Returns:

  • (Array)


137
138
139
# File 'lib/queries/image/filter.rb', line 137

def sqed_depiction_id
  @sqed_depiction_id
end

#sqed_image ⇒ Boolean

Returns true - yes false - is not nil - ignored.

Returns:

  • (Boolean) —

    true - yes false - is not nil - ignored



143
144
145
# File 'lib/queries/image/filter.rb', line 143

def sqed_image
  @sqed_image
end

#taxon_name_id ⇒ Protonym.id?

Return all images depicting an Otu that is self or descendant linked to this TaxonName

Returns:

  • (Protonym.id, nil) —

    return all images depicting an Otu that is self or descendant linked to this TaxonName



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

def taxon_name_id
  @taxon_name_id
end

#taxon_name_id_target ⇒ Array

Only used when taxon_name_id provided

Returns:

  • (Array) —

    one or all of 'Otu', 'CollectionObject', 'FieldOccurrence', defaults to all if nothing provided



148
149
150
# File 'lib/queries/image/filter.rb', line 148

def taxon_name_id_target
  @taxon_name_id_target
end

#type_material_depictions ⇒ Array

Returns depicts some that is a type specimen.

Returns:

  • (Array) —

    depicts some that is a type specimen



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

def type_material_depictions
  @type_material_depictions
end

Instance Method Details

#biocuration_facet ⇒ Object



299
300
301
302
303
304
305
# File 'lib/queries/image/filter.rb', line 299

def biocuration_facet
  return nil if biocuration_class_id.empty?
  ::Image.joins(collection_objects: [:depictions]).merge(
    ::CollectionObject::BiologicalCollectionObject.joins(:biocuration_classifications)
    .where(biocuration_classifications: {biocuration_class_id:})
  )
end

#build_depiction_facet(kind, ids) ⇒ Object



559
560
561
562
# File 'lib/queries/image/filter.rb', line 559

def build_depiction_facet(kind, ids)
  return nil if ids.empty?
  ::Image.joins(:depictions).where(depictions: {depiction_object_id: ids, depiction_object_type: kind})
end

#collection_object_scope_facet ⇒ Object



416
417
418
419
420
421
422
# File 'lib/queries/image/filter.rb', line 416

def collection_object_scope_facet
  return nil if collection_object_id.empty?

  scope_facet_for(
    :collection_objects, collection_object_id, collection_object_scope
  )
end

#collection_object_table ⇒ Arel::Table

Returns:

  • (Arel::Table)


280
281
282
# File 'lib/queries/image/filter.rb', line 280

def collection_object_table
  ::CollectionObject.arel_table
end

#depiction_caption_facet ⇒ Object



468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/queries/image/filter.rb', line 468

def depiction_caption_facet
  return nil if depiction_caption.nil?

  if depiction_caption_exact
    ::Image
      .joins(:depictions)
      .where('depictions.caption = ?', depiction_caption)
  else
    caption = "%#{depiction_caption.strip.gsub(/\s+/, '%')}%"
    ::Image
      .joins(:depictions)
      .where('depictions.caption ILIKE ?', caption)
  end
end

#depiction_object_type_facet ⇒ Object



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

def depiction_object_type_facet
  return nil if depiction_object_type.empty?
  ::Image.joins(:depictions).where(depictions: {depiction_object_type:})
end

#depiction_table ⇒ Arel::Table

Returns:

  • (Arel::Table)


295
296
297
# File 'lib/queries/image/filter.rb', line 295

def depiction_table
  ::Depiction.arel_table
end

#depictions_facet ⇒ Object



321
322
323
324
325
326
327
328
# File 'lib/queries/image/filter.rb', line 321

def depictions_facet
  return nil if depictions.nil?
  if depictions
    ::Image.joins(:depictions)
  else
    ::Image.where.missing(:depictions)
  end
end

#field_occurrence_scope_facet ⇒ Object



424
425
426
427
428
429
430
# File 'lib/queries/image/filter.rb', line 424

def field_occurrence_scope_facet
  return nil if field_occurrence_id.empty?

  scope_facet_for(
    :field_occurrences, field_occurrence_id, field_occurrence_scope
  )
end

#field_occurrence_table ⇒ Arel::Table

Returns:

  • (Arel::Table)


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

def field_occurrence_table
  ::FieldOccurrence.arel_table
end

#freeform_svg_facet ⇒ Object



330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/queries/image/filter.rb', line 330

def freeform_svg_facet
  return nil if freeform_svg.nil?

  q = ::Image.left_joins(depictions: [:sled_image, :sqed_depiction])
    .where.missing(:sled_image)
    .where.not(depictions: {svg_clip: nil}).distinct

  if freeform_svg
    q
  else
    ::Queries.except(::Image.where(project_id:), q)
  end
end

#images_on(t, t_id) ⇒ Object



483
484
485
# File 'lib/queries/image/filter.rb', line 483

def images_on(t, t_id)
  ::Image.joins(t).where(**{t => {id: t_id}})
end

#images_on_collecting_events_via(t, t_id) ⇒ Object



495
496
497
498
499
# File 'lib/queries/image/filter.rb', line 495

def images_on_collecting_events_via(t, t_id)
  ::Image
    .joins(collecting_events: t)
    .where("#{t}.id IN (?)", t_id)
end

#images_on_observations_linked_to(t, t_id) ⇒ Object



487
488
489
490
491
492
493
# File 'lib/queries/image/filter.rb', line 487

def images_on_observations_linked_to(t, t_id)
  ::Image.joins(:observations)
    .where(observations: {
      observation_object_type: t.to_s.classify,
      observation_object_id: t_id
    })
end

#merge_clauses ⇒ Object



657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
# File 'lib/queries/image/filter.rb', line 657

def merge_clauses
  s = ::Queries::Query::Filter::SUBQUERIES.select{|k,v| v.include?(:image)}.keys.map(&:to_s) - ['source']
  [
    *s.collect{|m| query_facets_facet(m)}, # Reference all the Image referencing SUBQUERIES
    biocuration_facet,
    collection_object_scope_facet,
    depiction_caption_facet,
    depiction_object_type_facet,
    depictions_facet,
    field_occurrence_scope_facet,
    freeform_svg_facet,
    ,
    otu_id_facet,
    otu_scope_facet,
    sled_image_facet,
    sled_image_id_facet,
    source_facet,
    sqed_image_facet,
    sqed_depiction_id_facet,
    sqed_image_facet,
    taxon_name_id_facet,
    type_material_depictions_facet,
  ]
end

#metadata_depiction_facet ⇒ Object



307
308
309
310
311
312
313
314
# File 'lib/queries/image/filter.rb', line 307

def 
  return nil if .nil?
  if 
    ::Image.joins(:depictions).where(depictions: {is_metadata_depiction: true}).distinct
  else
    ::Image.joins(:depictions).where.not(depictions: {is_metadata_depiction: true}).distinct
  end
end

#otu_facet_collection_object_observations(otu_ids) ⇒ Object



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

def otu_facet_collection_object_observations(otu_ids)
  ::Image.joins(:observations)
    .joins("INNER JOIN taxon_determinations on taxon_determinations.taxon_determination_object_id = observations.observation_object_id AND taxon_determinations.taxon_determination_object_type = 'CollectionObject'")
    .where(taxon_determinations: {otu_id: otu_ids}, observations: {observation_object_type: 'CollectionObject'})
end

#otu_facet_collection_objects(otu_ids) ⇒ Object



532
533
534
535
# File 'lib/queries/image/filter.rb', line 532

def otu_facet_collection_objects(otu_ids)
  ::Image.joins(collection_objects: [:taxon_determinations])
    .where(taxon_determinations: {otu_id: otu_ids})
end

#otu_facet_field_occurrences(otu_ids) ⇒ Object



537
538
539
540
# File 'lib/queries/image/filter.rb', line 537

def otu_facet_field_occurrences(otu_ids)
  ::Image.joins(field_occurrences: [:taxon_determinations])
    .where(taxon_determinations: {otu_id: otu_ids})
end

#otu_facet_otu_observations(otu_ids) ⇒ Object



548
549
550
551
# File 'lib/queries/image/filter.rb', line 548

def otu_facet_otu_observations(otu_ids)
  ::Image.joins(:observations)
    .where(observations: {observation_object_id: otu_ids, observation_object_type: 'Otu'})
end

#otu_facet_otus(otu_ids) ⇒ Object



528
529
530
# File 'lib/queries/image/filter.rb', line 528

def otu_facet_otus(otu_ids)
  ::Image.joins(:depictions).where(depictions: {depiction_object_type: 'Otu', depiction_object_id: otu_ids})
end

#otu_facet_type_material(otu_ids) ⇒ Object

Find all TaxonNames, and their synonyms



509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/queries/image/filter.rb', line 509

def otu_facet_type_material(otu_ids)

  # Double check that there are otu_ids,
  #  this check exists in calling methods, but re-inforce here.
  protonyms = if otu_ids.any?
                ::Queries::TaxonName::Filter.new(
                  otu_query: { otu_id: otu_ids},
                  synonymify: true,
                  project_id:
                ).all.where(type: 'Protonym')
              else
                TaxonName.none
              end



  ::Image.joins(collection_objects: [type_materials: [:protonym]]).where(collection_objects: {type_materials: {protonym: protonyms}})
end

#otu_facet_type_material_observations(otu_ids) ⇒ Object



501
502
503
504
505
506
# File 'lib/queries/image/filter.rb', line 501

def otu_facet_type_material_observations(otu_ids)
  ::Image.joins(:observations)
    .joins("INNER JOIN type_materials on type_materials.collection_object_id = observations.observation_object_id AND observations.observation_object_type = 'CollectionObject'")
    .joins('INNER JOIN otus on otus.taxon_name_id = type_materials.protonym_id')
    .where(otus: {id: otu_ids})
end

#otu_id_facet ⇒ Object



372
373
374
375
376
377
# File 'lib/queries/image/filter.rb', line 372

def otu_id_facet
  # only run this when scope not provided
  return nil if otu_id.empty? || !otu_scope.empty?

  ::Image.joins(:otus).where(otus: {id: otu_id})
end

#otu_scope_facet ⇒ Object



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/image/filter.rb', line 379

def otu_scope_facet
  return nil if otu_id.empty? || otu_scope.empty?
  otu_ids = otu_id

  otu_ids += ::Otu.coordinate_otu_ids(otu_id) if otu_scope.include?(:coordinate_otus)
  otu_ids.uniq!

  selected = []

  if otu_scope.include?(:all) # unused
    selected = [
      :otu_facet_otus,
      :otu_facet_collection_objects,
      :otu_facet_field_occurrences,
      :otu_facet_otu_observations,
      :otu_facet_collection_object_observations,
      :otu_facet_type_material,
      :otu_facet_type_material_observations
    ]
  elsif otu_scope.empty?
    selected = [:otu_facet_otus]
  else
    selected.push :otu_facet_otus if otu_scope.include?(:otus)
    selected.push :otu_facet_collection_objects if otu_scope.include?(:collection_objects)
    selected.push :otu_facet_collection_object_observations if otu_scope.include?(:collection_object_observations)
    selected.push :otu_facet_field_occurrences if otu_scope.include?(:field_occurrences)
    selected.push :otu_facet_otu_observations if otu_scope.include?(:otu_observations)
    selected.push :otu_facet_type_material if otu_scope.include?(:type_material)
    selected.push :otu_facet_type_material_observations if otu_scope.include?(:type_material_observations)
  end

  q = selected.collect{|a| '(' + send(a, otu_ids).to_sql + ')'}.join(' UNION ')

  d = ::Image.from('(' + q + ')' + ' as images')
  d
end

#otu_table ⇒ Arel::Table

Returns:

  • (Arel::Table)


275
276
277
# File 'lib/queries/image/filter.rb', line 275

def otu_table
  ::Otu.arel_table
end

#query_facets_facet(name = nil) ⇒ Object



639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
# File 'lib/queries/image/filter.rb', line 639

def query_facets_facet(name = nil)
  return nil if name.nil?

  q = send((name + '_query').to_sym)

  return nil if q.nil?

  n = "query_#{name}_img"

  s = "WITH #{n} AS (" + q.all.to_sql + ') ' +
    ::Image
    .joins(:depictions)
    .joins("JOIN #{n} as #{n}1 on depictions.depiction_object_id = #{n}1.id AND depictions.depiction_object_type = '#{name.treetop_camelize}'")
    .to_sql

  ::Image.from('(' + s + ') as images').distinct
end

#scope_facet_for(t, t_id, t_scope) ⇒ Object



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/queries/image/filter.rb', line 432

def scope_facet_for(t, t_id, t_scope)
  selected = []
  if t_scope.include?(:all) # unused
    selected = [
      images_on(t, t_id).to_sql,
      images_on_observations_linked_to(t, t_id).to_sql,
      images_on_collecting_events_via_observed(t, t_id).to_sql
    ]
  elsif t_scope.empty?
    selected =
      [images_on(t, t_id).to_sql]
  else
    selected.push(
      images_on(t, t_id).to_sql
    ) if t_scope.include?(t)

    selected.push(
      images_on_observations_linked_to(t, t_id).to_sql
    ) if t_scope.include?(:observations)

    selected.push(
      images_on_collecting_events_via(t, t_id).to_sql
    ) if t_scope.include?(:collecting_events)
  end

  q = selected.map{ |q| '(' + q + ')' }.join(' UNION ')

  ::Image.from('(' + q + ')' + ' as images')
end

#sled_image_facet ⇒ Object



353
354
355
356
357
358
359
360
# File 'lib/queries/image/filter.rb', line 353

def sled_image_facet
  return nil if sled_image.nil?
  if sled_image
    ::Image.joins(:sled_image).distinct
  else
    ::Image.where.missing(:sled_image).distinct
  end
end

#sled_image_id_facet ⇒ Object



362
363
364
365
# File 'lib/queries/image/filter.rb', line 362

def sled_image_id_facet
  return nil if sled_image_id.empty?
  ::Image.joins(:sled_image).where(sled_images: {id: sled_image_id})
end

#source_facet ⇒ Object



462
463
464
465
466
# File 'lib/queries/image/filter.rb', line 462

def source_facet
  return nil if source_id.empty?

  ::Image.joins(:citations).where(citations: { source_id: })
end

#sqed_depiction_id_facet ⇒ Object



367
368
369
370
# File 'lib/queries/image/filter.rb', line 367

def sqed_depiction_id_facet
  return nil if sqed_depiction_id.empty?
  ::Image.joins(depictions: [:sqed_depiction]).where(sqed_depictions: {id: sqed_depiction_id})
end

#sqed_image_facet ⇒ Object



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

def sqed_image_facet
  return nil if sqed_image.nil?
  if sqed_image
    ::Image.joins(depictions: [:sqed_depiction]).distinct
  else
    ::Image.left_joins(depictions: [:sqed_depiction]).where(sqed_depiction: {id: nil}).distinct
  end
end

#taxon_determination_table ⇒ Arel::Table

Returns:

  • (Arel::Table)


270
271
272
# File 'lib/queries/image/filter.rb', line 270

def taxon_determination_table
  ::TaxonDetermination.arel_table
end

#taxon_name_id_facet ⇒ Object



564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
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
633
634
635
636
637
# File 'lib/queries/image/filter.rb', line 564

def taxon_name_id_facet
  #  Image -> Depictions -> Otu -> TaxonName -> Ancestors
  return nil if taxon_name_id.empty?

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

  j1, j2, j3, q1, q2, q3 = nil, nil, nil, nil, nil, nil

  if taxon_name_id_target.include?('Otu')
    a = otu_table.alias('oj1')
    b = t.alias('tj1')
    h_alias = h.alias('th1')

    j1 = table
      .join(depiction_table, Arel::Nodes::InnerJoin).on(table[:id].eq(depiction_table[:image_id]))
      .join(a, Arel::Nodes::InnerJoin).on( depiction_table[:depiction_object_id].eq(a[:id]).and( depiction_table[:depiction_object_type].eq('Otu') ))
      .join(b, Arel::Nodes::InnerJoin).on( a[:taxon_name_id].eq(b[:id]))
      .join(h_alias, Arel::Nodes::InnerJoin).on(b[:id].eq(h_alias[:descendant_id]))

    z = h_alias[:ancestor_id].in(taxon_name_id)
    q1 = ::Image.joins(j1.join_sources).where(z)
  end

  if taxon_name_id_target.include?('CollectionObject')
    a = otu_table.alias('oj2')
    b = t.alias('tj2')
    h_alias = h.alias('th2')

    j2 = table
      .join(depiction_table, Arel::Nodes::InnerJoin).on(table[:id].eq(depiction_table[:image_id]))
      .join(collection_object_table, Arel::Nodes::InnerJoin).on( depiction_table[:depiction_object_id].eq(collection_object_table[:id]).and( depiction_table[:depiction_object_type].eq('CollectionObject') ))
      .join(taxon_determination_table, Arel::Nodes::InnerJoin).on(
        collection_object_table[:id].eq(taxon_determination_table[:taxon_determination_object_id])
      .and(taxon_determination_table[:taxon_determination_object_type].eq('CollectionObject'))
      )
        .join(a, Arel::Nodes::InnerJoin).on(  taxon_determination_table[:otu_id].eq(a[:id]) )
        .join(b, Arel::Nodes::InnerJoin).on( a[:taxon_name_id].eq(b[:id]))
        .join(h_alias, Arel::Nodes::InnerJoin).on(b[:id].eq(h_alias[:descendant_id]))

      z = h_alias[:ancestor_id].in(taxon_name_id)
      q2 = ::Image.joins(j2.join_sources).where(z)
  end

  if taxon_name_id_target.include?('FieldOccurrence')
    a = otu_table.alias('oj3')
    b = t.alias('tj3')
    h_alias = h.alias('th3')

    j3 = table
      .join(depiction_table, Arel::Nodes::InnerJoin).on(table[:id].eq(depiction_table[:image_id]))
      .join(field_occurrence_table, Arel::Nodes::InnerJoin).on( depiction_table[:depiction_object_id].eq(field_occurrence_table[:id]).and( depiction_table[:depiction_object_type].eq('FieldOccurrence') ))
      .join(taxon_determination_table, Arel::Nodes::InnerJoin).on(
        field_occurrence_table[:id].eq(taxon_determination_table[:taxon_determination_object_id])
      .and(taxon_determination_table[:taxon_determination_object_type].eq('FieldOccurrence'))
      )
        .join(a, Arel::Nodes::InnerJoin).on(  taxon_determination_table[:otu_id].eq(a[:id]) )
        .join(b, Arel::Nodes::InnerJoin).on( a[:taxon_name_id].eq(b[:id]))
        .join(h_alias, Arel::Nodes::InnerJoin).on(b[:id].eq(h_alias[:descendant_id]))

      z = h_alias[:ancestor_id].in(taxon_name_id)
      q3 = ::Image.joins(j3.join_sources).where(z)
  end

  if q1 && q2 && q3
    ::Image.from("((#{q1.to_sql}) UNION (#{q2.to_sql}) UNION (#{q3.to_sql})) AS images")
  elsif q1
    q1.distinct
  elsif q2
    q2.distinct
  else
    q3.distinct
  end
end

#type_material_depictions_facet ⇒ Scope

Returns:

  • (Scope)


554
555
556
557
# File 'lib/queries/image/filter.rb', line 554

def type_material_depictions_facet
  return nil if type_material_depictions.nil?
  ::Image.joins(collection_objects: [:type_materials])
end

#type_materials_table ⇒ Arel::Table

Returns:

  • (Arel::Table)


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

def type_materials_table
  ::TypeMaterial.arel_table
end