Module: Queries::Concerns::Attributes

Extended by:
ActiveSupport::Concern
Included in:
Queries::CollectingEvent::Filter, DwcOccurrence::Filter, Loan::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.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.and_clausesObject



64
65
66
# File 'lib/queries/concerns/attributes.rb', line 64

def self.and_clauses
  [ :attribute_clauses ]
end

.paramsObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/queries/concerns/attributes.rb', line 12

def self.params
  [
    :wildcard_attribute,
    :any_value_attribute,
    :no_value_attribute,
    wildcard_attribute: [],
    any_value_attribute: [],
    no_value_attribute: [],
  ]
end

Instance Method Details

#attribute_clausesObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/queries/concerns/attributes.rb', line 68

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
  c

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

#set_attributes_params(params) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/queries/concerns/attributes.rb', line 54

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

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