Module: QueryReport::FilterModule

Included in:
Report
Defined in:
lib/query_report/filter.rb,
lib/query_report/comparator.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.



11
12
13
# File 'lib/query_report/filter.rb', line 11

def filters
  @filters
end

#searchObject

Returns the value of attribute search.



11
12
13
# File 'lib/query_report/filter.rb', line 11

def search
  @search
end

Instance Method Details

#apply_filters(query, http_params) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/query_report/filter.rb', line 30

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

  #apply custom filter
  @filters.select(&:custom?).each do |filter|
    ordered_custom_param_values = ordered_param_value_objects(filter)
    has_no_user_input = ordered_custom_param_values.all? { |p| p.nil? or p == '' }
    query = filter.block.call(query, *ordered_custom_param_values) if filter.block and !has_no_user_input
  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] options => if set to true then that filter will not be applied, only will appear and can be used for custom application options => support default filter value, can be one value or for range filter can be array



21
22
23
24
# File 'lib/query_report/filter.rb', line 21

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

#has_filter?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/query_report/filter.rb', line 26

def has_filter?
  filters.present?
end

#load_default_values_in_param(http_params) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/query_report/filter.rb', line 51

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|
    filter.comparators.each do |comparator|
      params[filter.params_key][comparator.search_key] ||= comparator.param_value
    end
  end
  params
end

#ordered_param_value_objects(filter) ⇒ Object



45
46
47
48
49
# File 'lib/query_report/filter.rb', line 45

def ordered_param_value_objects(filter)
  filter.comparators.collect do |comp|
    comp.objectified_param_value
  end
end