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.

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.and_clausesObject



95
96
97
98
99
100
# File 'lib/queries/concerns/attributes.rb', line 95

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

.paramsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/queries/concerns/attributes.rb', line 13

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

Instance Method Details

#attribute_clausesObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/queries/concerns/attributes.rb', line 137

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_or_clausesObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/queries/concerns/attributes.rb', line 102

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_attributes_params(params) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/queries/concerns/attributes.rb', line 83

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]

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