Module: QueryReport::FilterModule

Included in:
Report
Defined in:
lib/query_report/filter.rb

Defined Under Namespace

Classes: Comparator, Filter

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filtersObject

Returns the value of attribute filters.



9
10
11
# File 'lib/query_report/filter.rb', line 9

def filters
  @filters
end

#searchObject

Returns the value of attribute search.



9
10
11
# File 'lib/query_report/filter.rb', line 9

def search
  @search
end

Instance Method Details

#apply_filters(query, http_params) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/query_report/filter.rb', line 22

def apply_filters(query, http_params)
  # apply default filter
  params = load_default_values_in_param(http_params) #need for ransack filter

  @search = query.search(params[:q])
  query = @search.result

  @filters.each do |filter|
    if filter.custom?
      ordered_custom_param_values = filter.search_keys.collect do |key|
        if filter.boolean?
          params[:custom_search][key].present? ? params[:custom_search][key] == 'true' : nil
        else
          params[:custom_search][key]
        end
      end
      #filter only if there is a given input
      query = filter.block.call(query, *ordered_custom_param_values) unless ordered_custom_param_values.all? { |p| p.nil? or p == '' }
    end
  end
  query
end

#filter(column, options = {}, &block) ⇒ Object

Creates a filter and adds to the filters Params:

column

the column on which the filter is done on

options

Options can have the following, options => date | text | whatever options => the comparators used for ransack search, [:gteq, :lteq]



17
18
19
20
# File 'lib/query_report/filter.rb', line 17

def filter(column, options={}, &block)
  @filters ||= []
  @filters << Filter.new(@params, column, options, &block)
end

#load_default_values_in_param(http_params) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/query_report/filter.rb', line 45

def load_default_values_in_param(http_params)
  params = http_params.clone
  params = params.merge(q: {}) unless params[:q]
  params = params.merge(custom_search: {}) unless params[:custom_search]
  @filters.each do |filter|
    if filter.has_default?
      filter.comparators.each do |comparator|
        params[filter.params_key][comparator.search_key] ||= comparator.default.to_s
      end
    end
  end
  params
end