Module: Queries::Concerns::TaxonDetermination

Extended by:
ActiveSupport::Concern
Includes:
Helpers
Included in:
Queries::CollectionObject::Filter, FieldOccurrence::Filter
Defined in:
lib/queries/concerns/taxon_determination.rb

Overview

Helpers and facets for queries against models with a polymorphic, position-ordered TaxonDetermination (as: :taxon_determination_object, see Shared::BiologicalExtensions) - presently CollectionObject and FieldOccurrence.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#boolean_param, #integer_param, #negate_facet, #split_pairs, #split_repeated_pairs, #tri_value_array

Class Method Details

.merge_clauses ⇒ Object



176
177
178
179
180
181
# File 'lib/queries/concerns/taxon_determination.rb', line 176

def self.merge_clauses
  [
    :otu_id_facet,
    :taxon_name_id_facet,
  ]
end

.params ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/queries/concerns/taxon_determination.rb', line 12

def self.params
  [
    :current_determinations,
    :descendants,
    :otu_id,
    :taxon_name_current_determination,
    :taxon_name_id,
    :validity,
    otu_id: [],
    taxon_name_id: []
  ]
end

Instance Method Details

#otu_id_facet ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/queries/concerns/taxon_determination.rb', line 92

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))
    .and(taxon_determination_table[:taxon_determination_object_type].eq(referenced_klass.base_class.name))

  if current_determinations == true
    # current and historical - no position filter
  elsif current_determinations == false
    w = w.and(taxon_determination_table[:position].gt(1))
  else # nil = current only (default)
    w = w.and(taxon_determination_table[:position].eq(1))
  end

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

#otu_table ⇒ Arel::Table

Returns:

  • (Arel::Table)


88
89
90
# File 'lib/queries/concerns/taxon_determination.rb', line 88

def otu_table
  ::Otu.arel_table
end

#set_taxon_determination_params(params) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/queries/concerns/taxon_determination.rb', line 73

def set_taxon_determination_params(params)
  @current_determinations = boolean_param(params, :current_determinations)
  @descendants = boolean_param(params, :descendants)
  @otu_id = params[:otu_id]
  @taxon_name_current_determination = boolean_param(params, :taxon_name_current_determination)
  @taxon_name_id = params[:taxon_name_id]
  @validity = boolean_param(params, :validity)
end

#taxon_determination_table ⇒ Arel::Table

Returns:

  • (Arel::Table)


83
84
85
# File 'lib/queries/concerns/taxon_determination.rb', line 83

def taxon_determination_table
  ::TaxonDetermination.arel_table
end

#taxon_name_id_facet ⇒ Object

TODO: use filter proxy



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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/queries/concerns/taxon_determination.rb', line 113

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])
      .and(taxon_determination_table[:taxon_determination_object_type]).eq(referenced_klass.base_class.name)
    ).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
      # both valid and invalid - no filter
    elsif validity == false
      z = z.and(t[:cached_valid_taxon_name_id].not_eq(t[:id]))
    else # nil = valid only (default)
      z = z.and(t[:cached_valid_taxon_name_id].eq(t[:id]))
    end

    if taxon_name_current_determination == true
      # current and historical - no position filter
    elsif taxon_name_current_determination == false
      z = z.and(taxon_determination_table[:position].gt(1))
    else # nil = current only (default)
      z = z.and(taxon_determination_table[:position].eq(1))
    end
  else # exact
    q = referenced_klass.joins(taxon_determinations: { otu: :taxon_name })
      .where(otus: { taxon_name_id: })

    if validity == true
      # both valid and invalid - no filter
    elsif validity == false
      q = q.where('taxon_names.cached_valid_taxon_name_id != taxon_names.id')
    else # nil = valid only (default)
      q = q.where('taxon_names.cached_valid_taxon_name_id = taxon_names.id')
    end

    if taxon_name_current_determination == true
      # current and historical - no position filter
    elsif taxon_name_current_determination == false
      q = q.where.not(taxon_determinations: { position: 1 })
    else # nil = current only (default)
      q = q.where(taxon_determinations: { position: 1 })
    end

    return q
  end

  referenced_klass.joins(q.join_sources).where(z).distinct
end