Class: Queries::Descriptor::Filter

Inherits:
Query::Filter show all
Includes:
Concerns::Citations, 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, #otu_query, #page, #paginate, #params, #per, #person_query, #project_id, #recent, #taxon_name_query, #venn, #venn_mode

Attributes inherited from Query

#query_string, #terms

Instance Method Summary collapse

Methods included from Helpers

#boolean_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, #deep_permit, included_annotator_facets, instatiated_base_filter, inverted_subqueries, #model_id_facet, #object_global_id_facet, params, #permitted_params, #process_url_into_params, #project_id_facet, query_name, #query_name, #set_nested_queries, #shared_and_clauses, #subquery_vector, #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)


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

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_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)


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

def descriptor_type
  @descriptor_type
end

#observation_matricesBoolean

Parameters:

  • observation_matrices (String, Boolean)

Returns:

  • (Boolean)


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

def observation_matrices
  @observation_matrices
end

#observation_matrix_idArray

Parameters:

  • observation_matrix_id (Array, String, Numeric)

Returns:

  • (Array)


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

def observation_matrix_id
  @observation_matrix_id
end

#observationsBoolean

Parameters:

  • observations (String, Boolean)

Returns:

  • (Boolean)


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

def observations
  @observations
end

#termObject

Parameters:

  • name (String, Symbol)

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



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

def term
  @term
end

#term_exactBoolean

Parameters:

  • term_exact (String, Boolean)

Returns:

  • (Boolean)


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

def term_exact
  @term_exact
end

#term_targetString, Symbol ni

Returns:

  • (String, Symbol ni)
  • Symbol, nil



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

def term_target
  @term_target
end

Instance Method Details

#and_clausesObject



164
165
166
167
168
# File 'lib/queries/descriptor/filter.rb', line 164

def and_clauses
  [ term_facet,
    descriptor_type_facet,
  ]
end

#descriptor_idObject



75
76
77
# File 'lib/queries/descriptor/filter.rb', line 75

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

#descriptor_type_facetObject



120
121
122
123
# File 'lib/queries/descriptor/filter.rb', line 120

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

#merge_clausesObject



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

def merge_clauses
  [
    otu_query_facet,
    observation_query_facet,

    observation_matrices_facet,
    observation_matrix_id_facet,
    observations_facet,
  ]
end

#observation_matrices_facetObject



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

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



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

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



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

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



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

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

#otu_query_facetObject



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

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



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

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