Class: Queries::Sound::Filter

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

Constant Summary collapse

PARAMS =
[
  :conveyance_object_type,
  :conveyances,
  :name_exact,
  :sound_id,
  :name,
  :otu_id,
  :otu_scope,
  :field_occurrence,
  :with_name,

  field_occurrence_id: [],
  collection_object_id: [],
  collecting_event_id: [],
  conveyance_object_type: [],
  sound_id: [],
  otu_id: [],
  otu_scope: [],
].freeze

Constants inherited from Query::Filter

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

Instance Attribute Summary collapse

Attributes inherited from Query::Filter

#api, #asserted_distribution_query, #biological_association_query, #biological_associations_graph_query, #collecting_event_query, #collection_object_query, #content_query, #controlled_vocabulary_term_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, #sound_query, #taxon_name_query, #venn, #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, #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, included_annotator_facets, instantiated_base_filter, inverted_subqueries, #model_id_facet, #object_global_id_facet, #only_project?, params, #permitted_params, #process_url_into_params, #project_id_facet, query_name, #query_name, #set_nested_queries, #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:

  • params (Hash)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/queries/sound/filter.rb', line 69

def initialize(query_params)
  super

  @collecting_event_id = params[:collecting_event_id]
  @collection_object_id = params[:collection_object_id]
  @conveyance_object_type = params[:conveyance_object_type]
  @conveyances = boolean_param(params, :conveyances)
  @field_occurrence_id = params[:field_occurrence_id]
  @name = params[:name]
  @name_exact = boolean_param(params, :name_exact)
  @otu_id = params[:otu_id]
  @otu_scope = params[:otu_scope]
  @sound_id = params[:sound_id]
  @with_name = boolean_param(params, :with_name)

  set_citations_params(params)
  set_tags_params(params)
end

Instance Attribute Details

#collecting_event_idArray

Returns:

  • (Array)


28
29
30
# File 'lib/queries/sound/filter.rb', line 28

def collecting_event_id
  @collecting_event_id
end

#collection_object_idArray

Returns:

  • (Array)


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

def collection_object_id
  @collection_object_id
end

#conveyance_object_typeArray

Parameters:

  • conveyance_object_type

    one or more names of classes.

Returns:

  • (Array)


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

def conveyance_object_type
  @conveyance_object_type
end

#conveyancesBoolean?

Returns true - sound is used (in a conveyance) false - sound is not used nil - either.

Returns:

  • (Boolean, nil)

    true - sound is used (in a conveyance) false - sound is not used nil - either



40
41
42
# File 'lib/queries/sound/filter.rb', line 40

def conveyances
  @conveyances
end

#field_occurrence_idArray

Returns:

  • (Array)


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

def field_occurrence_id
  @field_occurrence_id
end

#nameObject

Returns String.

Returns:

  • String



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

def name
  @name
end

#name_exactBoolean?

Returns:

  • (Boolean, nil)


51
52
53
# File 'lib/queries/sound/filter.rb', line 51

def name_exact
  @name_exact
end

#otu_idArray

Returns:

  • (Array)


54
55
56
# File 'lib/queries/sound/filter.rb', line 54

def otu_id
  @otu_id
end

#otu_scopeArray

Returns:

  • (Array)


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

def otu_scope
  @otu_scope
end

#sound_idArray

Returns:

  • (Array)


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

def sound_id
  @sound_id
end

#with_nameObject

Returns Boolean true - has name false - has no name nil - both.

Returns:

  • Boolean true - has name false - has no name nil - both



66
67
68
# File 'lib/queries/sound/filter.rb', line 66

def with_name
  @with_name
end

Instance Method Details

#and_clausesObject



249
250
251
252
253
# File 'lib/queries/sound/filter.rb', line 249

def and_clauses
  [
    name_facet
  ]
end

#collecting_event_facetObject



171
172
173
174
175
# File 'lib/queries/sound/filter.rb', line 171

def collecting_event_facet
  return nil if collecting_event_id.empty?

  ::Sound.joins(:collecting_events).where(collecting_events: {id: collecting_event_id})
end

#collection_object_facetObject



165
166
167
168
169
# File 'lib/queries/sound/filter.rb', line 165

def collection_object_facet
  return nil if collection_object_id.empty?

  ::Sound.joins(:collection_objects).where(collection_objects: {id: collection_object_id})
end

#collection_object_tableArel::Table

Returns:

  • (Arel::Table)


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

def collection_object_table
  ::CollectionObject.arel_table
end

#conveyance_object_type_facetObject



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

def conveyance_object_type_facet
  return nil if conveyance_object_type.empty?
  ::Sound.joins(:conveyances).where(conveyances: {conveyance_object_type:}).distinct
end

#conveyance_tableArel::Table

Returns:

  • (Arel::Table)


132
133
134
# File 'lib/queries/sound/filter.rb', line 132

def conveyance_table
  ::Conveyance.arel_table
end

#conveyances_facetObject



150
151
152
153
154
155
156
157
# File 'lib/queries/sound/filter.rb', line 150

def conveyances_facet
  return nil if conveyances.nil?
  if conveyances
    ::Sound.joins(:conveyances)
  else
    ::Sound.where.missing(:conveyances)
  end
end

#field_occurrence_facetObject



177
178
179
180
181
# File 'lib/queries/sound/filter.rb', line 177

def field_occurrence_facet
  return nil if field_occurrence_id.empty?

  ::Sound.joins(:field_occurrences).where(field_occurrences: {id: field_occurrence_id})
end

#field_occurrence_tableArel::Table

Returns:

  • (Arel::Table)


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

def field_occurrence_table
  ::FieldOccurrence.arel_table
end

#merge_clausesObject



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

def merge_clauses
  s = ::Queries::Query::Filter::SUBQUERIES.select{|k,v| v.include?(:sound)}.keys.map(&:to_s) - ['source']
  [
    *s.collect{|m| query_facets_facet(m)}, # Reference all the Sound referencing SUBQUERIES
    conveyance_object_type_facet,
    conveyances_facet,
    otu_id_facet,
    otu_scope_facet,
    collecting_event_facet,
    collection_object_facet,
    field_occurrence_facet,
    with_name_facet
  ]
end

#name_facetObject



136
137
138
139
140
141
142
143
# File 'lib/queries/sound/filter.rb', line 136

def name_facet
  return nil if name.blank?
  if name_exact
    table[:name].eq(name.strip)
  else
    table[:name].matches('%' + name.strip.gsub(/\s/, '%') + '%')
  end
end

#otu_facet_collection_objects(otu_ids) ⇒ Object



188
189
190
191
# File 'lib/queries/sound/filter.rb', line 188

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

#otu_facet_field_occurrence(otu_ids) ⇒ Object



183
184
185
186
# File 'lib/queries/sound/filter.rb', line 183

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

#otu_facet_otus(otu_ids) ⇒ Object



193
194
195
# File 'lib/queries/sound/filter.rb', line 193

def otu_facet_otus(otu_ids)
  ::Sound.joins(:conveyances).where(conveyances: { conveyance_object_type: 'Otu', conveyance_object_id: otu_ids })
end

#otu_id_facetObject



159
160
161
162
163
# File 'lib/queries/sound/filter.rb', line 159

def otu_id_facet
  return nil if otu_id.empty? || !otu_scope.empty?

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

#otu_scope_facetObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/queries/sound/filter.rb', line 197

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

  selected = []

  if otu_scope.include?(:all)
    selected = [
      :otu_facet_otus,
      :otu_facet_collection_objects,
      :otu_facet_field_occurrence,
    ]
  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_field_occurrence if otu_scope.include?(:field_occurrences)
  end

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

  d = ::Sound.from('(' + q + ')' + ' as sounds')
  d
end

#otu_tableArel::Table

Returns:

  • (Arel::Table)


117
118
119
# File 'lib/queries/sound/filter.rb', line 117

def otu_table
  ::Otu.arel_table
end

#query_facets_facet(name = nil) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/queries/sound/filter.rb', line 231

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}_snd"

  s = "WITH #{n} AS (" + q.all.to_sql + ') ' +
    ::Sound
    .joins(:conveyances)
    .joins("JOIN #{n} as #{n}1 on conveyances.conveyance_object_id = #{n}1.id AND conveyances.conveyance_object_type = '#{name.treetop_camelize}'")
    .to_sql

  ::Sound.from('(' + s + ') as sounds').distinct
end

#with_name_facetObject



222
223
224
225
226
227
228
229
# File 'lib/queries/sound/filter.rb', line 222

def with_name_facet
  return nil if with_name.nil?
  if with_name
    ::Sound.where.not(name: nil)
  else
    ::Sound.where(name: nil)
  end
end