Module: Queries::Concerns::DataAttributes

Overview

Linking DataAttribute to queries that reference DataAttributes

For filter queries: !! requires ‘set_data_attributes_params` be called in initialize()

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#boolean_param, #integer_param

Class Method Details

.merge_clausesObject

TODO: this as class method is bad,

it prevents us from adding optimizing logic
that restricts the number of clauses


339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/queries/concerns/data_attributes.rb', line 339

def self.merge_clauses
  [
    :data_attribute_import_exact_pair_facet,
    :data_attribute_exact_pair_facet,
    :data_attribute_import_predicate_facet,
    :data_attribute_predicate_id_facet,
    :data_attribute_wildcard_pair_facet,
    :data_attribute_import_wildcard_pair_facet,
    :data_attribute_without_predicate_id_facet,
    :data_attributes_facet,
    :value_facet,
    :import_value_facet
  ]
end

.paramsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/queries/concerns/data_attributes.rb', line 9

def self.params
  [
    :data_attributes,

    :data_attribute_import_exact_pair,
    :data_attribute_import_exact_value,
    :data_attribute_import_predicate,
    :data_attribute_import_wildcard_value,
    :data_attribute_import_wildcard_pair,

    :data_attribute_exact_pair,
    :data_attribute_exact_value,
    :data_attribute_predicate_id,
    :data_attribute_wildcard_pair,
    :data_attribute_wildcard_value,
    :data_attribute_without_predicate_id,

    data_attribute_exact_pair: [],
    data_attribute_exact_value: [],
    data_attribute_predicate_id: [],
    data_attribute_wildcard_pair: [],
    data_attribute_wildcard_value: [],
    data_attribute_without_predicate_id: [],

    data_attribute_import_wildcard_pair: [],
    data_attribute_import_exact_pair: [],
    data_attribute_import_exact_value: [],
    data_attribute_import_predicate: [],
    data_attribute_import_wildcard_value: [],
  ]
end

Instance Method Details

#data_attribute_exact_pair_facetObject



299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/queries/concerns/data_attributes.rb', line 299

def data_attribute_exact_pair_facet
  return nil if data_attribute_exact_pair.blank?
  a = []
  data_attribute_exact_pair.each do |k,v|
    a.push data_attribute_table[:controlled_vocabulary_term_id].eq(k).and( data_attribute_table[:value].eq(v) )
  end
  w = a.shift
  a.each do |c|
    w = w.or(c)
  end

  referenced_klass.joins(:internal_attributes).where(w)
end

#data_attribute_import_exact_pair_facetObject



313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/queries/concerns/data_attributes.rb', line 313

def data_attribute_import_exact_pair_facet
  return nil if data_attribute_import_exact_pair.blank?
  a = []
  data_attribute_import_exact_pair.each do |k,v|
    a.push data_attribute_table[:import_predicate].eq(k).and( data_attribute_table[:value].eq(v) )
  end
  w = a.shift
  a.each do |c|
    w = w.or(c)
  end

  referenced_klass.joins(:import_attributes).where(w)
end

#data_attribute_import_predicate_facetObject



198
199
200
201
# File 'lib/queries/concerns/data_attributes.rb', line 198

def data_attribute_import_predicate_facet
  return nil if data_attribute_import_predicate.empty?
  referenced_klass.joins(:import_attributes).where(data_attributes: {import_predicate: data_attribute_import_predicate})
end

#data_attribute_import_wildcard_pair_facetObject



285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/queries/concerns/data_attributes.rb', line 285

def data_attribute_import_wildcard_pair_facet
  return nil if data_attribute_import_wildcard_pair.blank?
  a = []
  data_attribute_import_wildcard_pair.each do |k,v|
    a.push data_attribute_table[:import_predicate].eq(k).and( data_attribute_table[:value].matches(wildcard_value(v)) )
  end
  w = a.shift
  a.each do |c|
    w = w.or(c)
  end

  referenced_klass.joins(:import_attributes).where(w)
end

#data_attribute_predicate_id_facetObject



193
194
195
196
# File 'lib/queries/concerns/data_attributes.rb', line 193

def data_attribute_predicate_id_facet
  return nil if data_attribute_predicate_id.blank?
  referenced_klass.joins(:internal_attributes).where(data_attributes: {controlled_vocabulary_term_id: data_attribute_predicate_id})
end

#data_attribute_tableArel::Table

Returns:

  • (Arel::Table)


189
190
191
# File 'lib/queries/concerns/data_attributes.rb', line 189

def data_attribute_table
  ::DataAttribute.arel_table
end

#data_attribute_wildcard_pair_facetObject



271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/queries/concerns/data_attributes.rb', line 271

def data_attribute_wildcard_pair_facet
  return nil if data_attribute_wildcard_pair.blank?
  a = []
  data_attribute_wildcard_pair.each do |k,v|
    a.push data_attribute_table[:controlled_vocabulary_term_id].eq(k).and( data_attribute_table[:value].matches(wildcard_value(v)) )
  end
  w = a.shift
  a.each do |c|
    w = w.or(c)
  end

  referenced_klass.joins(:internal_attributes).where(w)
end

#data_attribute_without_predicate_id_facetObject



204
205
206
207
208
209
210
211
212
213
214
# File 'lib/queries/concerns/data_attributes.rb', line 204

def data_attribute_without_predicate_id_facet
  return nil if data_attribute_without_predicate_id.blank?
  not_these = referenced_klass.left_joins(:internal_attributes).where(data_attributes: {controlled_vocabulary_term_id: data_attribute_without_predicate_id})

  # a Not exists without using .exists
  s = 'WITH not_these AS (' + not_these.to_sql + ') ' +
    referenced_klass.joins("LEFT JOIN not_these AS not_these1 ON not_these1.id = #{table.name}.id")
    .where('not_these1.id IS NULL').to_sql

  referenced_klass.from("(#{s}) as #{table.name}")
end

#data_attributes_facetObject



327
328
329
330
331
332
333
334
# File 'lib/queries/concerns/data_attributes.rb', line 327

def data_attributes_facet
  return nil if data_attributes.nil?
  if data_attributes
    referenced_klass.joins(:data_attributes).distinct
  else
    referenced_klass.where.missing(:data_attributes)
  end
end

#import_value_facetObject



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/queries/concerns/data_attributes.rb', line 246

def import_value_facet
  return nil if data_attribute_import_exact_value.blank? && data_attribute_import_wildcard_value.blank?

  a,b = nil, nil

  if data_attribute_import_wildcard_value.present?
    v = data_attribute_import_wildcard_value.collect{|z| wildcard_value(z) } # TODO: should be standardized much earlier on
    a = data_attribute_table[:value].matches_any(v)
  end

  b = data_attribute_table[:value].in(data_attribute_import_exact_value) if data_attribute_import_exact_value.present?

  q = referenced_klass.joins(:import_attributes)

  if a && b
    q.where(a.or(b))
  elsif a
    q.where(a)
  elsif b
    q.where(b)
  else
    nil
  end
end

#set_data_attributes_params(params) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/queries/concerns/data_attributes.rb', line 171

def set_data_attributes_params(params)
  @data_attribute_import_exact_pair = params[:data_attribute_import_exact_pair]
  @data_attribute_import_exact_value = params[:data_attribute_import_exact_value]
  @data_attribute_import_predicate = params[:data_attribute_import_predicate]
  @data_attribute_import_wildcard_value = params[:data_attribute_import_wildcard_value]

  @data_attribute_predicate_id = params[:data_attribute_predicate_id]
  @data_attribute_without_predicate_id = params[:data_attribute_without_predicate_id]
  @data_attribute_exact_value = params[:data_attribute_exact_value]
  @data_attribute_wildcard_value = params[:data_attribute_wildcard_value]

  @data_attribute_exact_pair = params[:data_attribute_exact_pair]
  @data_attribute_wildcard_pair = params[:data_attribute_wildcard_pair]
  @data_attribute_import_wildcard_pair = params[:data_attribute_import_wildcard_pair]
  @data_attributes = boolean_param(params, :data_attributes)
end

#value_facetObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/queries/concerns/data_attributes.rb', line 221

def value_facet
  return nil if data_attribute_exact_value.blank?  && data_attribute_wildcard_value.blank?

  a,b = nil, nil

  if data_attribute_wildcard_value.present?
    v = data_attribute_wildcard_value.collect{|a| wildcard_value(a) } # TODO: should be standardized much earlier on
    a = data_attribute_table[:value].matches_any(v)
  end

  b = data_attribute_table[:value].in(data_attribute_exact_value) if data_attribute_exact_value.present?

  q = referenced_klass.joins(:internal_attributes)

  if a && b
    q.where(a.or(b))
  elsif a
    q.where(a)
  elsif b
    q.where(b)
  else
    nil
  end
end

#wildcard_value(value) ⇒ Object

TODO: get rid of this



217
218
219
# File 'lib/queries/concerns/data_attributes.rb', line 217

def wildcard_value(value)
  '%' + value.to_s.strip.gsub(/\s+/, '%') + '%'
end