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
#filters ⇒ Object
Returns the value of attribute filters.
11
12
13
|
# File 'lib/query_report/filter.rb', line 11
def filters
@filters
end
|
#search ⇒ Object
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/query_report/filter.rb', line 28
def apply_filters(query, http_params)
params = load_default_values_in_param(http_params) @search = query.search(params[:q])
query = @search.result
@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) unless 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]
19
20
21
22
|
# File 'lib/query_report/filter.rb', line 19
def filter(column, options={}, &block)
@filters ||= []
@filters << Filter.new(@params, column, options, &block)
end
|
#has_filter? ⇒ Boolean
24
25
26
|
# File 'lib/query_report/filter.rb', line 24
def has_filter?
filters.present?
end
|
#load_default_values_in_param(http_params) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/query_report/filter.rb', line 49
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
43
44
45
46
47
|
# File 'lib/query_report/filter.rb', line 43
def ordered_param_value_objects(filter)
filter.comparators.collect do |comp|
comp.objectified_param_value
end
end
|