Class: Queries::Image::Filter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Filter

Returns a new instance of Filter.

Parameters:

  • params (Hash)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/queries/image/filter.rb', line 62

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

  # DONE
  @otu_id = params[:otu_id]
  @collection_object_id = params[:collection_object_id]
  @collecting_event_id = params[:collecting_event_id]
  @image_id = params[:image_id]
  @biocuration_class_id = params[:biocuration_class_id]
  @sled_image_id = params[:sled_image_id]
  @sqed_depiction_id = params[:sqed_depiction_id]
  @taxon_name_id = params[:taxon_name_id]
  @ancestor_id_target = params[:ancestor_id_target]

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

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

Instance Attribute Details

#ancestor_id_targetArray

Only used when `ancestor_id` provided

Returns:

  • (Array)

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



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

def ancestor_id_target
  @ancestor_id_target
end

#biocuration_class_idArray

Returns of biocuration_class ids.

Returns:

  • (Array)

    of biocuration_class ids



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

def biocuration_class_id
  @biocuration_class_id
end

#collecting_event_idArray

Returns only return objects with this collecting event ID.

Returns:

  • (Array)

    only return objects with this collecting event ID



10
11
12
# File 'lib/queries/image/filter.rb', line 10

def collecting_event_id
  @collecting_event_id
end

#collection_object_idArray

Returns a list of Collection object ids, matches one ot one only.

Returns:

  • (Array)

    a list of Collection object ids, matches one ot one only



14
15
16
# File 'lib/queries/image/filter.rb', line 14

def collection_object_id
  @collection_object_id
end

#depictionBoolean?

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



39
40
41
# File 'lib/queries/image/filter.rb', line 39

def depiction
  @depiction
end

#image_idArray

Returns:

  • (Array)


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

def image_id
  @image_id
end

#otu_idArray

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

Parameters:

  • otu_id (Array, Integer)

    `otu_id=1` or `otu_id[]=1`

Returns:

  • (Array)

    a list of Otu ids, matches one ot one only



20
21
22
# File 'lib/queries/image/filter.rb', line 20

def otu_id
  @otu_id
end

#sled_image_idArray

Returns:

  • (Array)


30
31
32
# File 'lib/queries/image/filter.rb', line 30

def sled_image_id
  @sled_image_id
end

#sqed_depiction_idArray

Returns:

  • (Array)


33
34
35
# File 'lib/queries/image/filter.rb', line 33

def sqed_depiction_id
  @sqed_depiction_id
end

#taxon_name_idProtonym.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



44
45
46
# File 'lib/queries/image/filter.rb', line 44

def taxon_name_id
  @taxon_name_id
end

Instance Method Details

#allActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/queries/image/filter.rb', line 274

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

  q
end

#ancestors_facetObject



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
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
# File 'lib/queries/image/filter.rb', line 325

def ancestors_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, q1, q2 = nil, nil, nil, nil

  if ancestor_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].eq_any(taxon_name_id)
    q1 = ::Image.joins(j1.join_sources).where(z)
  end

  if ancestor_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[:biological_collection_object_id]) )
      .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].eq_any(taxon_name_id)
    q2 = ::Image.joins(j2.join_sources).where(z)
  end

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

  #  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

end

#and_clausesActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


211
212
213
214
215
216
217
218
219
220
221
# File 'lib/queries/image/filter.rb', line 211

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)


224
225
226
227
228
229
230
231
232
233
234
# File 'lib/queries/image/filter.rb', line 224

def base_and_clauses
  clauses = [
    image_facet
  ]

  clauses += [
    #type_facet,
  ]
  clauses.compact!
  clauses
end

#base_merge_clausesObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/queries/image/filter.rb', line 236

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

  clauses += [
    build_depiction_facet('Otu', otu_id),
    build_depiction_facet('CollectionObject', collection_object_id),
    build_depiction_facet('CollectingEvent', collecting_event_id),
    #    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,
    sqed_depiction_facet,
    sled_image_facet,
    biocuration_facet,
    depiction_facet,
  ]

  clauses.compact!
  clauses
end

#base_queryObject



131
132
133
# File 'lib/queries/image/filter.rb', line 131

def base_query
  ::Image.select('images.*')
end

#biocuration_facetObject



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

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: biocuration_class_id})
  )
end

#build_depiction_facet(kind, ids) ⇒ Object



320
321
322
323
# File 'lib/queries/image/filter.rb', line 320

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

#collecting_event_tableArel::Table

Returns:

  • (Arel::Table)


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

def collecting_event_table
  ::CollectingEvent.arel_table
end

#collection_object_tableArel::Table

Returns:

  • (Arel::Table)


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

def collection_object_table
  ::CollectionObject.arel_table
end

#depiction_facetObject



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

def depiction_facet
  return nil if depiction.nil?
  subquery = ::Image.joins(:depictions).where(table[:id].eq(depiction_table[:image_id])).arel.exists
  ::Image.where(depiction == true ? subquery : subquery.not)
end

#depiction_tableArel::Table

Returns:

  • (Arel::Table)


156
157
158
# File 'lib/queries/image/filter.rb', line 156

def depiction_table
  ::Depiction.arel_table
end

#image_facetObject



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

def image_facet
  return nil if image_id.empty?
  table[:id].eq_any(image_id)
end

#merge_clausesActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


263
264
265
266
267
268
269
270
271
# File 'lib/queries/image/filter.rb', line 263

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

#otu_tableArel::Table

Returns:

  • (Arel::Table)


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

def otu_table
  ::Otu.arel_table
end

#sled_image_facetObject



180
181
182
183
# File 'lib/queries/image/filter.rb', line 180

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

#sqed_depiction_facetObject



185
186
187
188
# File 'lib/queries/image/filter.rb', line 185

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

#tableArel::Table

Returns:

  • (Arel::Table)


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

def table
  ::Image.arel_table
end

#taxon_determination_tableArel::Table

Returns:

  • (Arel::Table)


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

def taxon_determination_table
  ::TaxonDetermination.arel_table
end

#type_facetObject

facet



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

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

#type_material_facetScope

Returns:

  • (Scope)


292
293
294
295
296
297
298
299
300
301
# File 'lib/queries/image/filter.rb', line 292

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

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

#type_material_type_facetScope

Returns:

  • (Scope)


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

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

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

#type_materials_tableArel::Table

Returns:

  • (Arel::Table)


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

def type_materials_table
  ::TypeMaterial.arel_table
end