Module: ActiveRecord::FilterableBy

Defined in:
lib/filterable_by.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.merge(scope, unscoped, hash, name, **opts, &block) ⇒ Object



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

def merge(scope, unscoped, hash, name, **opts, &block)
  key = name
  positive = normalize(hash[key]) if hash.key?(key)
  if positive.present?
    sub = eval_scope(scope, unscoped, positive, **opts, &block)
    return nil unless sub

    scope = scope.merge(sub)
  end

  key = "#{name}_not"
  negative = normalize(hash[key]) if hash.key?(key)
  if negative.present?
    sub = eval_scope(scope, unscoped, negative, **opts, &block)
    return nil unless sub

    scope = scope.merge(invert_where(sub))
  end

  scope
end

.normalize(value) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/filterable_by.rb', line 8

def normalize(value)
  case value
  when String, Numeric
    value
  when Array
    value.select {|v| normalize(v) }
  end
end