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
-
#filters ⇒ Object
Returns the value of attribute filters.
-
#search ⇒ Object
Returns the value of attribute search.
Instance Method Summary collapse
- #apply_filters(query, http_params) ⇒ Object
-
#filter(column, options = {}, &block) ⇒ Object
Creates a filter.
- #has_filter? ⇒ Boolean
- #load_default_values_in_param(http_params) ⇒ Object
- #ordered_param_value_objects(filter) ⇒ Object
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
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/query_report/filter.rb', line 36 def apply_filters(query, http_params) # apply default filter params = load_default_values_in_param(http_params) #need for ransack filter @search = query.ransack(params[:q]) query = @search.result #this is to fix a bug from ransack, as for ransack when the sorting is done from a joined table it does not sort by default query = query.order(params[:q][:s]) if params[:q][:s] #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
31 32 33 34 |
# File 'lib/query_report/filter.rb', line 31 def filter(column, ={}, &block) @filters ||= [] @filters << Filter.new(@params, column, , &block) end |
#has_filter? ⇒ Boolean
117 118 119 |
# File 'lib/query_report/filter.rb', line 117 def has_filter? filters.present? end |
#load_default_values_in_param(http_params) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/query_report/filter.rb', line 127 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
121 122 123 124 125 |
# File 'lib/query_report/filter.rb', line 121 def ordered_param_value_objects(filter) filter.comparators.collect do |comp| comp.objectified_param_value end end |