Module: Queries::Concerns::Attributes

Extended by:
ActiveSupport::Concern
Includes:
Helpers
Included in:
Queries::CollectingEvent::Filter, DwcOccurrence::Filter, Loan::Filter, Namespace::Filter, Source::Filter
Defined in:
lib/queries/concerns/attributes.rb

Overview

Helpers and facets for queries that loop through ATTRIBUTES

!! Implementing filters must define ATTRIBUTES before the inclusion of this concern, like:

ATTRIBUTES = ::Loan.core_attributes.map(&:to_sym).freeze

TODO: macro a method to explicitly set ATTRIBUTES and eliminate need for definition location dependency.

This may not be worth it as various models have slight exceptions.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

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

Class Method Details

.and_clausesObject



178
179
180
181
182
183
# File 'lib/queries/concerns/attributes.rb', line 178

def self.and_clauses
  [
    :attribute_clauses,
    :attribute_or_clauses
  ]
end

.merge_clausesObject



185
186
187
188
189
# File 'lib/queries/concerns/attributes.rb', line 185

def self.merge_clauses
  [
    :attribute_facet
  ]
end

.paramsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/queries/concerns/attributes.rb', line 14

def self.params
  [
    :attribute_exact_pair,
    :attribute_wildcard_pair,
    :wildcard_attribute,
    :any_value_attribute,
    :no_value_attribute,
    :attribute_name,
    :attribute_value,
    :attribute_value_type,
    :attribute_value_negator,
    :attribute_combine_logic,
    attribute_exact_pair: [],
    attribute_wildcard_pair: [],
    wildcard_attribute: [],
    any_value_attribute: [],
    no_value_attribute: [],
    attribute_name: [],
    attribute_value: [],
    attribute_value_type: [],
    attribute_value_negator: [],
    attribute_combine_logic: [],
  ]
end

Instance Method Details

#attribute_clausesObject



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/queries/concerns/attributes.rb', line 226

def attribute_clauses
  c = []
  self.class::ATTRIBUTES.each do |a|
    if any_value_attribute.include?(a)
      c.push table[a].not_eq(nil)
    elsif no_value_attribute.include?(a)
      c.push table[a].eq(nil)
    else
      v = send(a)
      if v.present?
        if wildcard_attribute.include?(a)
          c.push Arel::Nodes::NamedFunction.new('CAST', [table[a].as('TEXT')]).matches('%' + v.to_s + '%')
        else
          c.push table[a].eq(v)
        end
      end
    end
  end

  a = c.shift
  c.each do |b|
    a = a.and(b)
  end

  a
end

#attribute_facetObject



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/queries/concerns/attributes.rb', line 253

def attribute_facet
  return nil if attribute_name.empty?

  queries = []
  attribute_value_type.map(&:to_sym).each_with_index do |value_type, i|
    name = attribute_name[i]
    next if name.blank?

    attribute = name.to_sym
    next unless self.class::ATTRIBUTES.include?(attribute)

    value = attribute_value[i]
    negator = attribute_value_negator[i]

    q = nil
    case value_type
    when :exact, :wildcard
      value_clause = if value_type == :exact
        table[attribute].eq(value)
      else
        Arel::Nodes::NamedFunction.new('CAST', [table[attribute].as('TEXT')])
          .matches("%#{value}%")
      end

      value_clause = value_clause.not if negator
      q = referenced_klass.where(value_clause)
    when :any, :no
      has_attr = (value_type == :any && !negator) || (value_type == :no && negator)
      value_clause = has_attr ? table[attribute].not_eq(nil) : table[attribute].eq(nil)
      q = referenced_klass.where(value_clause)
    end

    queries << q if q
  end

  return nil if queries.empty?

  q = queries.first
  attribute_combine_logic.each_with_index do |c, i|
    next_q = queries[i + 1]
    break if next_q.nil?

    if c.nil?
      q = referenced_klass_intersection([q, next_q])
    elsif c == true
      q = referenced_klass_union([q, next_q])
    else
      q = q.where.not(id: next_q)
    end
  end

  q
end

#attribute_or_clausesObject



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/queries/concerns/attributes.rb', line 191

def attribute_or_clauses
  return nil if attribute_wildcard_pair.empty? && attribute_exact_pair.empty?

  w = nil
  if attribute_wildcard_pair.present?
    arr = []
    attribute_wildcard_pair.each do |p|
      attr, v = [p[0], p[1]]
      arr.push Arel::Nodes::NamedFunction.new('CAST', [table[attr].as('TEXT')]).matches('%' + v.to_s + '%')
    end

    w = arr.shift
    arr.each do |b|
      w = w.or(b)
    end
  end

  e = nil
  if attribute_exact_pair.present?
    arr = []
    attribute_exact_pair.each do |p|
      attr, v = [p[0], p[1]]
      arr.push table[attr].eq(v)
    end

    e = arr.shift
    arr.each do |b|
      e = e.or(b)
    end
  end

  return w.and(e) if w.present? && e.present?
  w || e
end

#set_attribute_row_params(params) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/queries/concerns/attributes.rb', line 156

def set_attribute_row_params(params)
  row_count = [params[:attribute_name]].flatten.compact.count
  return if (
    row_count == 0 ||
    [params[:attribute_value]].flatten.compact.count != row_count ||
    [params[:attribute_value_type]].flatten.compact.count != row_count ||
    tri_value_array(params[:attribute_value_negator]).length != row_count ||
    tri_value_array(params[:attribute_combine_logic]).length != row_count
  )

  @attribute_name =
    [params[:attribute_name]].flatten.compact
  @attribute_value =
    [params[:attribute_value]].flatten.compact
  @attribute_value_type =
    [params[:attribute_value_type]].flatten.compact
  @attribute_value_negator =
    tri_value_array(params[:attribute_value_negator])
  @attribute_combine_logic =
    tri_value_array(params[:attribute_combine_logic])
end

#set_attributes_params(params) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/queries/concerns/attributes.rb', line 142

def set_attributes_params(params)
  @attribute_exact_pair = params[:attribute_exact_pair]
  @attribute_wildcard_pair = params[:attribute_wildcard_pair]
  @wildcard_attribute = params[:wildcard_attribute]
  @any_value_attribute = params[:any_value_attribute]
  @no_value_attribute = params[:no_value_attribute]

  set_attribute_row_params(params)

  self.class::ATTRIBUTES.each do |a|
    send("#{a}=", params[a.to_sym])
  end
end