Class: JsonApiFilter::FieldFilters::Compare

Inherits:
Base
  • Object
show all
Defined in:
lib/json_api_filter/field_filters/compare.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#association, #scope, #values

Instance Method Summary collapse

Constructor Details

#initialize(scope, values, allowed_searches:) ⇒ Compare

Returns a new instance of Compare.



8
9
10
11
# File 'lib/json_api_filter/field_filters/compare.rb', line 8

def initialize(scope, values, allowed_searches:)
  super(scope, values)
  @allowed_searches = allowed_searches
end

Instance Attribute Details

#allowed_searchesObject (readonly)

Returns the value of attribute allowed_searches.



6
7
8
# File 'lib/json_api_filter/field_filters/compare.rb', line 6

def allowed_searches
  @allowed_searches
end

Instance Method Details

#predicateActiveRecord_Relation

Returns:

  • (ActiveRecord_Relation)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/json_api_filter/field_filters/compare.rb', line 14

def predicate
  column = values.keys.first
  filter = values.first[1]
  filter.map do |key, value|
    if !WHERE_METHODS[key.to_sym].nil?
      compare(column, key, value)
    elsif key == "search"
      next unless allowed_searches[:columns].keys.include?(column.to_sym)
      ::JsonApiFilter::FieldFilters::Searcher.new(
        scope,
        {allowed_searches[:columns][column.to_sym] => value}
      ).predicate
    else
      nil
    end
  end.compact.reduce(&:merge)
end