Class: Headmin::Filters

Inherits:
Object
  • Object
show all
Defined in:
app/models/headmin/filters.rb

Instance Method Summary collapse

Constructor Details

#initialize(params, param_types) ⇒ Filters

Example:

}).query(Order)



11
12
13
14
# File 'app/models/headmin/filters.rb', line 11

def initialize(params, param_types)
  @params = params
  @param_types = param_types
end

Instance Method Details

#parse(attribute, type, association: nil) ⇒ Object



16
17
18
19
# File 'app/models/headmin/filters.rb', line 16

def parse(attribute, type, association: nil)
  class_name = "Headmin::Filter::#{type.to_s.classify}".constantize
  class_name.new(attribute, @params, association: association)
end

#query(collection) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/headmin/filters.rb', line 21

def query(collection)
  @param_types.each do |attribute, type|
    if type.is_a? Hash
      # We are given attribute filters of an association
      association = attribute

      # By default, we offer a filter of type association
      association_filter = Headmin::Filter::Association.new(attribute, @params, association: nil)
      collection = association_filter.query(collection)

      # Query all the passed attribute filters for this association
      type.each do |new_attribute, new_type|
        filter = parse(new_attribute, new_type, association: association)
        collection = filter.query(collection)
      end
    else
      filter = parse(attribute, type)
      collection = filter.query(collection)
    end
  end
  collection
end