Class: Queries::Descriptor::Filter

Inherits:
Query::Filter show all
Includes:
Concerns::Citations, Concerns::Confidences, Concerns::Depictions, Concerns::Notes, Concerns::Tags, Helpers
Defined in:
lib/queries/descriptor/filter.rb

Constant Summary collapse

PARAMS =
[
  :descriptor_id,
  :descriptor_type,
  :observation_matrices,
  :observation_matrix_id,
  :observations,
  :term,
  :term_exact,
  :term_target,
  descriptor_id: [],
  descriptor_type: [],
  observation_matrix_id: [],
].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, #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, #taxon_name_query, #venn, #venn_mode

Attributes inherited from Query

#query_string, #terms

Instance Method Summary collapse

Methods included from Helpers

#boolean_param, #integer_param

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)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/queries/descriptor/filter.rb', line 58

def initialize(query_params)
  super

  @descriptor_id = params[:descriptor_id]
  @descriptor_type = params[:descriptor_type]
  @observation_matrices = boolean_param(params, :observation_matrices)
  @observation_matrix_id = params[:observation_matrix_id]
  @observations = boolean_param(params, :observations)
  @term = params[:term]
  @term_exact = boolean_param(params, :term_exact)
  @term_target = params[:term_target]

  set_confidences_params(params)
  set_citations_params(params)
  set_notes_params(params)
  set_tags_params(params)
  set_user_dates(params)
end

Instance Attribute Details

#descriptor_typeArray?

Parameters:

  • descriptor_type (String)

    a full type like Descriptor::Continuous

Returns:

  • (Array, nil)


47
48
49
# File 'lib/queries/descriptor/filter.rb', line 47

def descriptor_type
  @descriptor_type
end

#observation_matricesBoolean

Parameters:

  • observation_matrices (String, Boolean)

Returns:

  • (Boolean)


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

def observation_matrices
  @observation_matrices
end

#observation_matrix_idArray

Parameters:

  • observation_matrix_id (Array, String, Numeric)

Returns:

  • (Array)


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

def observation_matrix_id
  @observation_matrix_id
end

#observationsBoolean

Parameters:

  • observations (String, Boolean)

Returns:

  • (Boolean)


55
56
57
# File 'lib/queries/descriptor/filter.rb', line 55

def observations
  @observations
end

#termObject

Parameters:

  • name (String, Symbol)

    matches against name, short_name, description, description_name, key_name See ‘term_target`, `term_exact`



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

def term
  @term
end

#term_exactBoolean

Parameters:

  • term_exact (String, Boolean)

Returns:

  • (Boolean)


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

def term_exact
  @term_exact
end

#term_targetString, Symbol ni

Returns:

  • (String, Symbol ni)
  • Symbol, nil



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

def term_target
  @term_target
end

Instance Method Details

#and_clausesObject



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

def and_clauses
  [ term_facet,
    descriptor_type_facet,
  ]
end

#descriptor_idObject



77
78
79
# File 'lib/queries/descriptor/filter.rb', line 77

def descriptor_id
  [@descriptor_id].flatten.compact.uniq
end

#descriptor_type_facetObject



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

def descriptor_type_facet
  return nil if descriptor_type.blank?
  table[:type].in(descriptor_type)
end

#merge_clausesObject



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

def merge_clauses
  [
    otu_query_facet,
    observation_query_facet,

    observation_matrices_facet,
    observation_matrix_id_facet,
    observations_facet,
  ]
end

#observation_matrices_facetObject



127
128
129
130
131
132
133
134
# File 'lib/queries/descriptor/filter.rb', line 127

def observation_matrices_facet
  return nil if observation_matrices.nil?
  if observation_matrices
    ::Descriptor.joins(:observation_matrices).distinct
  else
    ::Descriptor.where.missing(:observation_matrices).distinct
  end
end

#observation_matrix_id_facetObject



116
117
118
119
120
# File 'lib/queries/descriptor/filter.rb', line 116

def observation_matrix_id_facet
  return nil if observation_matrix_id.empty?
  ::Descriptor.joins(:observation_matrices)
    .where(observation_matrices: { id: observation_matrix_id }).distinct
end

#observation_query_facetObject



145
146
147
148
149
150
151
152
153
# File 'lib/queries/descriptor/filter.rb', line 145

def observation_query_facet
  return nil if observation_query.nil?
  s = 'WITH query_ob_d AS (' + observation_query.all.to_sql + ') ' +
    ::Descriptor
    .joins('JOIN query_ob_d as query_ob_d1 on query_ob_d1.descriptor_id = descriptors.id')
    .to_sql

  ::Descriptor.from('(' + s + ') as descriptors').distinct
end

#observations_facetObject



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

def observations_facet
  return nil if observations.nil?
  if observations
    ::Descriptor.joins(:observations).distinct
  else
    ::Descriptor.where.missing(:observations)
  end
end

#otu_query_facetObject



155
156
157
158
159
160
161
162
163
164
# File 'lib/queries/descriptor/filter.rb', line 155

def otu_query_facet
  return nil if otu_query.nil?
  s = 'WITH query_otu_ob AS (' + otu_query.all.to_sql + ') ' +
    ::Descriptor
    .joins(:observations)
    .joins("JOIN query_otu_ob AS query_otu_ob1 ON observations.observation_object_id = query_otu_ob1.id AND observations.observation_object_type = 'Otu'")
    .to_sql

  ::Descriptor.from('(' + s + ') as descriptors').distinct
end

#term_facetObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/queries/descriptor/filter.rb', line 89

def term_facet
  return nil if term.blank?
  w =  '%' + term.gsub(/\s+/, '%') + '%'

  if term_exact
    if term_target.nil?
      table[:name].eq(term)
        .or(table[:short_name].eq(term))
        .or(table[:description].eq(term))
        .or(table[:description_name].eq(term))
        .or(table[:key_name].eq(term))
    else
      table[term_target].eq(term)
    end
  else
    if term_target.nil?
      table[:name].matches(w)
        .or(table[:short_name].matches(w))
        .or(table[:description].matches(w))
        .or(table[:description_name].matches(w))
        .or(table[:key_name].matches(w))
    else
      table[term_target].matches(tw)
    end
  end
end