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



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

def self.and_clauses
  [ :attribute_clauses ]
end

.paramsObject



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

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



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 69

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.first
  c.each do |b|
    a = a.and(b)
  end
  a
end

#set_attributes_params(params) ⇒ Object



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

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