Class: Otu::Filter

Inherits:
Queries::Query show all
Defined in:
lib/queries/otu/filter.rb

Overview

TODO: Unify all and filter

Instance Attribute Summary (collapse)

Attributes inherited from Queries::Query

#dynamic_limit, #options, #project_id, #query_string, #terms

Instance Method Summary (collapse)

Methods inherited from Queries::Query

#alphabetic_strings, #autocomplete, #autocomplete_cached, #autocomplete_cached_wildcard_anywhere, #autocomplete_identifier_cached_exact, #autocomplete_identifier_cached_like, #autocomplete_ordered_wildcard_pieces_in_cached, #autocomplete_start_date, #build_terms, #cached, #combine_or_clauses, #end_wildcard, #exactly_named, #fragments, #identifier_table, #integers, #match_ordered_wildcard_pieces_in_cached, #match_wildcard_cached, #named, #no_digits, #only_ids, #only_integers?, #parent, #parent_child_join, #parent_child_where, #pieces, #scope, #simple_date, #start_and_end_wildcard, #start_wildcard, #wildcard_pieces, #wildcard_wrapped_integers, #with_id, #with_identifier, #with_identifier_like, #with_project_id, #with_start_date, #year_letter, #years

Constructor Details

- (Filter) initialize(params)

Returns a new instance of Filter

Parameters:

  • params (Hash)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/queries/otu/filter.rb', line 16

def initialize(params)
  params.reject! { |_k, v| v.blank? }

  @and_or_select  = params[:and_or_select]

  @geographic_area_ids = params[:geographic_area_ids]
  @shape = params[:drawn_area_shape]
  @author_ids = params[:author_ids]
  @verbatim_author = params[:verbatim_author]

  @rank_class = params[:rank_class]
  @descendants = params[:descendants]

  @taxon_name_id = params[:taxon_name_id]
  @taxon_name_ids = params[:taxon_name_ids] || []
  @otu_id = params[:otu_id]
  @otu_ids = params[:otu_ids] || []

  @biological_association_ids = params[:biological_association_ids] || []

  @taxon_name_classification_ids = params[:taxon_name_classification_ids] || []
  @taxon_name_relationship_ids = params[:taxon_name_relationship_ids] || []
  @asserted_distribution_ids = params[:asserted_distribution_ids] || []
end

Instance Attribute Details

- (Object) and_or_select

Returns the value of attribute and_or_select



9
10
11
# File 'lib/queries/otu/filter.rb', line 9

def and_or_select
  @and_or_select
end

- (Object) asserted_distribution_ids

Returns the value of attribute asserted_distribution_ids



12
13
14
# File 'lib/queries/otu/filter.rb', line 12

def asserted_distribution_ids
  @asserted_distribution_ids
end

- (Object) author_ids

Returns the value of attribute author_ids



9
10
11
# File 'lib/queries/otu/filter.rb', line 9

def author_ids
  @author_ids
end

- (Object) biological_association_ids

Returns the value of attribute biological_association_ids



12
13
14
# File 'lib/queries/otu/filter.rb', line 12

def biological_association_ids
  @biological_association_ids
end

- (Object) descendants

Returns the value of attribute descendants



8
9
10
# File 'lib/queries/otu/filter.rb', line 8

def descendants
  @descendants
end

- (Object) geographic_area_ids

Query variables



7
8
9
# File 'lib/queries/otu/filter.rb', line 7

def geographic_area_ids
  @geographic_area_ids
end

- (Object) otu_id

Returns the value of attribute otu_id



12
13
14
# File 'lib/queries/otu/filter.rb', line 12

def otu_id
  @otu_id
end

- (Object) otu_ids

Returns the value of attribute otu_ids



12
13
14
# File 'lib/queries/otu/filter.rb', line 12

def otu_ids
  @otu_ids
end

- (Object) rank_class

Returns the value of attribute rank_class



8
9
10
# File 'lib/queries/otu/filter.rb', line 8

def rank_class
  @rank_class
end

- (Object) shape

Query variables



7
8
9
# File 'lib/queries/otu/filter.rb', line 7

def shape
  @shape
end

- (Object) taxon_name_classification_ids

Returns the value of attribute taxon_name_classification_ids



12
13
14
# File 'lib/queries/otu/filter.rb', line 12

def taxon_name_classification_ids
  @taxon_name_classification_ids
end

- (Object) taxon_name_id

Returns the value of attribute taxon_name_id



12
13
14
# File 'lib/queries/otu/filter.rb', line 12

def taxon_name_id
  @taxon_name_id
end

- (Object) taxon_name_ids

Returns the value of attribute taxon_name_ids



12
13
14
# File 'lib/queries/otu/filter.rb', line 12

def taxon_name_ids
  @taxon_name_ids
end

- (Object) taxon_name_relationship_ids

Returns the value of attribute taxon_name_relationship_ids



12
13
14
# File 'lib/queries/otu/filter.rb', line 12

def taxon_name_relationship_ids
  @taxon_name_relationship_ids
end

- (Object) verbatim_author

was verbatim_author_string



11
12
13
# File 'lib/queries/otu/filter.rb', line 11

def verbatim_author
  @verbatim_author
end

Instance Method Details

- (ActiveRecord::Relation) all

Returns:

  • (ActiveRecord::Relation)


393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/queries/otu/filter.rb', line 393

def all
  a = and_clauses
  b = merge_clauses
  if a && b
    b.where(a).distinct
  elsif a
    ::Otu.where(a).distinct
  elsif b
    b.distinct      
  else
    ::Otu.none
  end
end

- (ActiveRecord::Relation?) and_clauses

Returns:

  • (ActiveRecord::Relation, nil)


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

def and_clauses
  clauses = [
    matching_taxon_name_ids,
    matching_otu_ids,

    # matching_verbatim_author
    # Queries::Annotator.annotator_params(options, ::Citation),
  ].compact


  return nil if clauses.empty?

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

- (Array) applied_scopes

Returns determine which scopes to apply based on parameters provided

Returns:

  • (Array)

    determine which scopes to apply based on parameters provided



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

def applied_scopes
  scopes = []
  scopes.push :geographic_area_scope if area_set?
  scopes.push :shape_scope if shape_set?
  scopes.push :nomen_scope if nomen_set?
  scopes.push :author_scope if author_set?
  scopes.push :verbatim_scope if verbatim_set?
  scopes
end

- (Boolean) area_set?

Returns:

  • (Boolean)


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

def area_set?
  !geographic_area_ids.nil?
end

- (Scope) author_scope

Returns 1. find all selected taxon name authors

  1. find all taxon_names which are associated with result #1

  2. find all otus which are associated with result #2

Returns:

  • (Scope)
    1. find all selected taxon name authors

    2. find all taxon_names which are associated with result #1

    3. find all otus which are associated with result #2



166
167
168
169
170
171
172
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
# File 'lib/queries/otu/filter.rb', line 166

def author_scope

  r = ::Role.arel_table

  case and_or_select
  when '_or_', nil

    c = r[:person_id].eq_any(author_ids).and(r[:type].eq('TaxonNameAuthor'))
    ::Otu.joins(taxon_name: [:roles]).where(c.to_sql).distinct

  when '_and_'
    table_alias = 'tna' # alias for 'TaxonNameAuthor'

    o = ::Otu.arel_table
    t = ::TaxonName.arel_table

    b = o.project(o[Arel.star]).from(o)
      .join(t)
      .on(t['id'].eq(o['taxon_name_id']))
      .join(r).on(
        r['role_object_id'].eq(t['id']).and(
          r['type'].eq('TaxonNameAuthor')
        )
    )

    author_ids.each_with_index do |person_id, i|
      x = r.alias("#{table_alias}_#{i}")
      b = b.join(x).on(
        x['role_object_id'].eq(t['id']),
        x['type'].eq('TaxonNameAuthor'),
        x['person_id'].eq(person_id)
      )
    end

    b = b.group(o['id']).having(r['person_id'].count.gteq(author_ids.count))
    b = b.as("z_#{table_alias}")

    # noinspection RubyResolve
    ::Otu.joins(Arel::Nodes::InnerJoin.new(b, Arel::Nodes::On.new(b['id'].eq(o['id']))))
  end
end

- (Boolean) author_set?

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
# File 'lib/queries/otu/filter.rb', line 77

def author_set?
  case author_ids
  when nil
    false
  else
    author_ids.count > 0
  end
end

- (Object) biological_associations_table



45
46
47
# File 'lib/queries/otu/filter.rb', line 45

def biological_associations_table
  ::BiologicalAssociation.arel_table
end

- (Scope) geographic_area_scope

This could be simplified if the AJAX selector returned a geographic_item_id rather than a GeographicAreaId n

  1. find all geographic_items in area(s)/shape

  2. find all georeferences which are associated with result #1

  3. find all collecting_events which are associated with result #2

  4. find all collection_objects which are associated with result #3

  5. find all otus which are associated with result #4

Returns:

  • (Scope)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/queries/otu/filter.rb', line 125

def geographic_area_scope
  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

  a = CollectionObject.joins(:geographic_items)
    .where(GeographicItem.contained_by_where_sql(target_geographic_item_ids))
    .distinct

  ::Otu.joins(:collection_objects).where(collection_objects: {id: a})
end

- (Array) ids_for_otu

Returns of otu_id

Returns:

  • (Array)

    of otu_id



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

def ids_for_otu
  ([otu_id] + otu_ids).compact.uniq
end

- (Array) ids_for_taxon_name

Returns of taxon_name.id

Returns:

  • (Array)

    of taxon_name.id



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

def ids_for_taxon_name
  ([taxon_name_id] + taxon_name_ids).compact.uniq
end

- (Object) matching_asserted_distribution_ids



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/queries/otu/filter.rb', line 326

def matching_asserted_distribution_ids
  return nil if asserted_distribution_ids.empty?
  o = table
  ad = ::AssertedDistribution.arel_table

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

  c = ad.alias('ad1')

  b = b.join(c, Arel::Nodes::OuterJoin)
    .on(
      a[:id].eq(c[:otu_id])
  )

  e = c[:otu_id].not_eq(nil)
  f = c[:id].eq_any(asserted_distribution_ids)

  b = b.where(e.and(f))
  b = b.group(a['id'])
  b = b.as('z_')

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

- (Object) matching_biological_association_ids



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/queries/otu/filter.rb', line 265

def matching_biological_association_ids 
  return nil if biological_association_ids.empty?
  o = table
  ba = biological_associations_table

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

  c = ba.alias('b1')
  d = ba.alias('b2')

  b = b.join(c, Arel::Nodes::OuterJoin)
    .on(
      a[:id].eq(c[:biological_association_subject_id])
    .and(c[:biological_association_subject_type].eq('Otu'))
  )

  b = b.join(d, Arel::Nodes::OuterJoin)
    .on(
      a[:id].eq(d[:biological_association_object_id])
    .and(d[:biological_association_object_type].eq('Otu'))
  )

  e = c[:biological_association_subject_id].not_eq(nil)
  f = d[:biological_association_object_id].not_eq(nil)

  g = c[:id].eq_any(biological_association_ids)
  h = d[:id].eq_any(biological_association_ids)

  b = b.where(e.or(f).and(g.or(h)))
  b = b.group(a['id'])
  b = b.as('z_')

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

- (Object) matching_otu_ids



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

def matching_otu_ids
  a = ids_for_otu 
  a.empty? ? nil : table[:id].eq_any(a)      
end

- (Object) matching_taxon_name_classification_ids



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/queries/otu/filter.rb', line 301

def matching_taxon_name_classification_ids
  return nil if taxon_name_classification_ids.empty?
  o = table
  tnc = ::TaxonNameClassification.arel_table

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

  c = tnc.alias('tnc1')

  b = b.join(c, Arel::Nodes::OuterJoin)
    .on(
      a[:taxon_name_id].eq(c[:taxon_name_id])
  )

  e = c[:id].not_eq(nil)
  f = c[:id].eq_any(taxon_name_classification_ids)

  b = b.where(e.and(f))
  b = b.group(a['id'])
  b = b.as('z_')

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

- (Object) matching_taxon_name_ids



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

def matching_taxon_name_ids
  a = ids_for_taxon_name 
  a.empty? ? nil : table[:taxon_name_id].eq_any(a)
end

- (Object) matching_taxon_name_relationship_ids



231
232
233
234
235
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
261
262
263
# File 'lib/queries/otu/filter.rb', line 231

def matching_taxon_name_relationship_ids 
  return nil if taxon_name_relationship_ids.empty?
  o = table
  ba = ::TaxonNameRelationship.arel_table 

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

  c = ba.alias('b1')
  d = ba.alias('b2')

  b = b.join(c, Arel::Nodes::OuterJoin)
    .on(
      a[:taxon_name_id].eq(c[:subject_taxon_name_id])
  )

  b = b.join(d, Arel::Nodes::OuterJoin)
    .on(
      a[:id].eq(d[:object_taxon_name_id])
  )

  e = c[:subject_taxon_name_id].not_eq(nil)
  f = d[:object_taxon_name_id].not_eq(nil)

  g = c[:id].eq_any(taxon_name_relationship_ids)
  h = d[:id].eq_any(taxon_name_relationship_ids)

  b = b.where(e.or(f).and(g.or(h)))
  b = b.group(a['id'])
  b = b.as('z_')

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

- (Object) merge_clauses



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/queries/otu/filter.rb', line 371

def merge_clauses

  clauses = [
    matching_biological_association_ids,
    matching_asserted_distribution_ids,
    matching_taxon_name_classification_ids,
    matching_taxon_name_relationship_ids

    # matching_verbatim_author
    # Queries::Annotator.annotator_params(options, ::Citation),
  ].compact

  return nil if clauses.empty?

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

- (Scope) nomen_scope

Returns:

  • (Scope)


148
149
150
151
152
153
154
155
# File 'lib/queries/otu/filter.rb', line 148

def nomen_scope
  scope1 = ::Otu.joins(:taxon_name).where(taxon_name_id: taxon_name_id)
  scope  = scope1
  if scope1.any?
    scope = ::Otu.self_and_descendants_of(scope1.first.id, rank_class) if with_descendants?
  end
  scope
end

- (Boolean) nomen_set?

Returns:

  • (Boolean)


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

def nomen_set?
  !taxon_name_id.nil?
end

- (Scope) result

Returns:

  • (Scope)


107
108
109
110
111
112
113
114
# File 'lib/queries/otu/filter.rb', line 107

def result
  return ::Otu.none if applied_scopes.empty?
  a = ::Otu.all
  applied_scopes.each do |scope|
    a = a.merge(self.send(scope))
  end
  a
end

- (Scope) shape_scope

Returns:

  • (Scope)


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

def shape_scope
  r42i = GeographicItem.gather_map_data(shape, 'CollectionObject').distinct
  ::Otu.joins(:collection_objects).where(collection_objects: {id: r42i})
end

- (Boolean) shape_set?

Returns:

  • (Boolean)


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

def shape_set?
  !shape.nil?
end

- (Object) table



41
42
43
# File 'lib/queries/otu/filter.rb', line 41

def table
  ::Otu.arel_table
end

- (Scope) verbatim_scope

Returns:

  • (Scope)


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

def verbatim_scope
  ::Otu.joins(:taxon_name).where('taxon_names.cached_author_year ILIKE ?', "%#{verbatim_author}%")
end

- (Boolean) verbatim_set?

Returns:

  • (Boolean)


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

def verbatim_set?
  !verbatim_author.blank?
end

- (Boolean) with_descendants?

Returns:

  • (Boolean)


102
103
104
# File 'lib/queries/otu/filter.rb', line 102

def with_descendants?
  !descendants.nil?
end