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



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

Examples:

Custom type

filter :invoiced_to_id, type: :user

# In a helper file define method
def query_report_user_filter(name, user_id, options={})
  user = User.find(user_id)
  concat hidden_field_tag name, user_id, options
  text_field_tag "#{name}", user.name, class: 'user_search' #implement the filter, it can be autocomplete
end

Parameters:

  • column

    the column on which the filter is done on, for manual filter the column name can be anything

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :type (Symbol)

    date | text | whatever

  • :comp (Array)

    the comparators used for ransack search, [:gteq, :lteq]

  • :manual (Boolean)

    if set to true then that filter will not be applied, only will appear and can be used for custom application

  • :default (Object)

    support default filter value, can be one value or for range filter can be array



31
32
33
34
# File 'lib/query_report/filter.rb', line 31

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

#has_filter?Boolean

Returns:

  • (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