Class: Queries::Conveyance::Filter

Inherits:
Query::Filter show all
Defined in:
lib/queries/conveyance/filter.rb

Constant Summary collapse

PARAMS =
[
  *::Conveyance.related_foreign_keys.map(&:to_sym),
  :conveyance_id,
  :name,
  :conveyance_object_type,
  :conveyance_object_id,
  :sound_id,
  :otu_id,
  sound_id: [],
  conveyance_id: [],
  conveyance_object_id: [],
  conveyance_object_type: [],
  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, #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, #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?, #paging_state, params, #permitted_params, #process_url_into_params, #project_id_facet, query_name, #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.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/queries/conveyance/filter.rb', line 48

def initialize(query_params)
  super

  @conveyance_id = params[:conveyance_id]
  @name = params[:name]
  @conveyance_object_type = params[:conveyance_object_type]
  @conveyance_object_id = params[:conveyance_object_id]
  @sound_id = params[:sound_id]
  @otu_id = params[:otu_id]
  @otu_scope = params[:otu_scope]
end

Instance Attribute Details

#conveyance_idObject

Returns Array.

Returns:

  • Array



22
23
24
# File 'lib/queries/conveyance/filter.rb', line 22

def conveyance_id
  @conveyance_id
end

#conveyance_object_idArray

Returns:

  • (Array)


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

def conveyance_object_id
  @conveyance_object_id
end

#conveyance_object_typeArray

Returns:

  • (Array)


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

def conveyance_object_type
  @conveyance_object_type
end

#nameObject

Parameters:

  • name (String, nil)

    wildcard wrapped, always, to match against ‘name`



26
27
28
# File 'lib/queries/conveyance/filter.rb', line 26

def name
  @name
end

#otu_idArray

Returns:

  • (Array)


42
43
44
# File 'lib/queries/conveyance/filter.rb', line 42

def otu_id
  @otu_id
end

#otu_scopeArray

Returns:

  • (Array)


46
47
48
# File 'lib/queries/conveyance/filter.rb', line 46

def otu_scope
  @otu_scope
end

#sound_idArray

Returns:

  • (Array)


38
39
40
# File 'lib/queries/conveyance/filter.rb', line 38

def sound_id
  @sound_id
end

Instance Method Details

#and_clausesObject



173
174
175
176
177
178
179
180
# File 'lib/queries/conveyance/filter.rb', line 173

def and_clauses
  [
    name_facet,
    conveyance_object_id_facet,
    conveyance_object_type_facet,
    sound_id_facet
  ]
end

#conveyance_object_id_facetObject



94
95
96
97
# File 'lib/queries/conveyance/filter.rb', line 94

def conveyance_object_id_facet
  return nil if conveyance_object_id.empty?
  table[:conveyance_object_id].in(conveyance_object_id)
end

#conveyance_object_type_facetObject



89
90
91
92
# File 'lib/queries/conveyance/filter.rb', line 89

def conveyance_object_type_facet
  return nil if conveyance_object_type.empty?
  table[:conveyance_object_type].in(conveyance_object_type)
end

#merge_clausesObject



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

def merge_clauses
  [
    sound_query_facet,
    otu_facet,
    otu_scope_facet
  ].compact
end

#name_facetObject



84
85
86
87
# File 'lib/queries/conveyance/filter.rb', line 84

def name_facet
  return nil if name.blank?
  table[:name].matches('%' + name + '%')
end

#otu_facetObject



115
116
117
118
# File 'lib/queries/conveyance/filter.rb', line 115

def otu_facet
  return nil if otu_id.empty? || !otu_scope.empty?
  otu_facet_otus(otu_id)
end

#otu_facet_collection_objects(otu_ids) ⇒ Object



120
121
122
123
124
125
126
127
128
# File 'lib/queries/conveyance/filter.rb', line 120

def otu_facet_collection_objects(otu_ids)
  fo = ::CollectionObject.joins(:taxon_determinations)
    .where(taxon_determinations: { otu_id: otu_ids })


  ::Conveyance
    .with(co_query: fo)
    .joins("JOIN co_query on co_query.id = conveyance_object_id and conveyance_object_type = 'CollectionObject'")
end

#otu_facet_field_occurrences(otu_ids) ⇒ Object



130
131
132
133
134
135
136
137
# File 'lib/queries/conveyance/filter.rb', line 130

def otu_facet_field_occurrences(otu_ids)
  fo = ::FieldOccurrence.joins(:taxon_determinations)
    .where(taxon_determinations: { otu_id: otu_ids })

  ::Conveyance
    .with(co_query: fo)
    .joins("JOIN co_query on co_query.id = conveyance_object_id and conveyance_object_type = 'FieldOccurrence'")
end

#otu_facet_otus(otu_ids) ⇒ Object



111
112
113
# File 'lib/queries/conveyance/filter.rb', line 111

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

#otu_scope_facetObject



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

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

  ::Queries.union(::Conveyance, selected.collect{|a| send(a, otu_ids) })
end

#sound_id_facetObject



99
100
101
102
# File 'lib/queries/conveyance/filter.rb', line 99

def sound_id_facet
  return nil if sound_id.empty?
  table[:sound_id].in(sound_id)
end

#sound_query_facetObject



104
105
106
107
108
109
# File 'lib/queries/conveyance/filter.rb', line 104

def sound_query_facet
  return nil if sound_query.nil?
  ::Conveyance.with(sound_query: sound_query.all )
    .joins('JOIN sound_query as sound_query1 on sound_query1.id = conveyances.sound_id')
    .distinct
end