Module: FilterHelper

Included in:
Kuppayam::BaseController
Defined in:
app/helpers/filter_helper.rb

Overview

This module creates basic reporting UI elements like filter drop downs etc

Instance Method Summary collapse

Instance Method Details

#configure_filter_param_mappingObject



45
46
47
# File 'app/helpers/filter_helper.rb', line 45

def configure_filter_param_mapping
  @filter_param_mapping = default_filter_param_mapping
end

#configure_filter_settingsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/helpers/filter_helper.rb', line 9

def configure_filter_settings
  
  @filter_settings = default_filter_settings

  # Variable Filter Example
  # { variable_name: :current_user, filter_name: :current_user, options: {} }

  # Boolean Filter Example
  # { param_name: :sa, filter_name: :super_admin, options: {} }

  # String Filter Example
  # { param_name: :q, filter_name: :query, options: {} }

  # Reference Filter Example
  # { param_name: :b, filter_name: :brand, filter_class: Brand, options: {} }
end

#configure_filter_ui_settingsObject



37
38
39
# File 'app/helpers/filter_helper.rb', line 37

def configure_filter_ui_settings
  @filter_ui_settings = default_filter_ui_settings
end

#configure_filtersObject



4
5
6
7
# File 'app/helpers/filter_helper.rb', line 4

def configure_filters
  configure_filter_settings
  configure_filter_param_mapping
end

#default_filter_param_mappingObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/helpers/filter_helper.rb', line 49

def default_filter_param_mapping
  ActiveSupport::HashWithIndifferentAccess.new(
    query: :q,
    chart_type: :ct,
    status: :st,

    month: :mn,
    year: :yr,
    from_year: :fy,
    to_year: :ty,

    sort_by: :sb,
    order_by: :ob
  )
end

#default_filter_settingsObject



26
27
28
29
30
31
32
33
34
35
# File 'app/helpers/filter_helper.rb', line 26

def default_filter_settings
  {
    variable_filters: [],
    boolean_filters: [],
    string_filters: [
      { param_name: :q, filter_name: :query, options: {} }
    ],
    reference_filters: []
  }
end

#default_filter_ui_settingsObject



41
42
43
# File 'app/helpers/filter_helper.rb', line 41

def default_filter_ui_settings
  {}
end

#filter_param_mappingObject



73
74
75
# File 'app/helpers/filter_helper.rb', line 73

def filter_param_mapping
  return @filter_param_mapping ? @filter_param_mapping : configure_filter_param_mapping
end

#filter_settingsObject



65
66
67
# File 'app/helpers/filter_helper.rb', line 65

def filter_settings
  return @filter_settings ? @filter_settings : configure_filter_settings
end

#filter_ui_settingsObject



69
70
71
# File 'app/helpers/filter_helper.rb', line 69

def filter_ui_settings
  return @filter_ui_settings ? @filter_ui_settings : configure_filter_ui_settings
end

#generate_short_url_filters(filters, filters_to_add, filters_to_remove) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'app/helpers/filter_helper.rb', line 189

def generate_short_url_filters(filters, filters_to_add, filters_to_remove)
  # Clone the existing filters first
  # remove the filters which are to be removed and add which are to be added
  # Also remove the filter which is currently selected
  temp_filters = filters.clone
  temp_filters.merge!(filters_to_add) unless filters_to_add.blank?
  temp_filters.reject!{|k,v| filters_to_remove.include?(k)} unless filters_to_remove.blank?

  # filters is now a hash with keys looking like year, month etc.
  # However, we need a hash with keys in short form like yr, mn
  # creating the new hash which we need
  short_filters = {}
  temp_filters.each do |k,v|
    short_k = @filter_param_mapping[k]
    short_filters[short_k] = v
  end
  short_filters
end

#parse_boolean_filter_from_params(filter_name, **options) ⇒ Object

parse_boolean_filter_from_params(:fd, :featured)



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'app/helpers/filter_helper.rb', line 138

def parse_boolean_filter_from_params(filter_name, **options)
  raise "@filter_param_mapping not initialised" unless @filter_param_mapping
  raise "#{filter_name} is not added to @filter_param_mapping" unless @filter_param_mapping.has_key?(filter_name)

  param_name = @filter_param_mapping[filter_name]

  if params[param_name]
    case params[param_name].strip
    when "true", "t","1","yes","y"
      @filters[filter_name] = true
    when "false", "f","0","no","n"
      @filters[filter_name] = false
    end
    instance_variable_set("@#{filter_name}", @filters[filter_name]) if @filters.has_key?(filter_name) && !@filters[filter_name].nil?
  else
    if options.has_key?(:default)
      obj = options[:default]
      instance_variable_set("@#{filter_name}", options[:default])
      @filters[filter_name] = options[:default]
    end
  end
end

#parse_filter_from_variable(variable_name, filter_name, **options) ⇒ Object

Use this method to create a filter if the value is in a variable e.g: if @current_user has a user object as value i.e <obj: “Mike”> parse_filter_from_variable(:current_company_group, :company_group)



107
108
109
110
# File 'app/helpers/filter_helper.rb', line 107

def parse_filter_from_variable(variable_name, filter_name, **options)
  variable = instance_variable_get("@#{variable_name}")
  @filters[filter_name] = variable if variable
end

#parse_filtersObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/helpers/filter_helper.rb', line 77

def parse_filters

  @filters = {} unless @filters

  filter_settings[:variable_filters].each do |vf|
    options = vf[:options] || {}
    parse_filter_from_variable(vf[:variable_name], vf[:filter_name], options)
  end if filter_settings.has_key?(:variable_filters)

  filter_settings[:string_filters].each do |spf|
    options = spf[:options] || {}
    parse_string_filter_from_params(spf[:filter_name], options)
  end if filter_settings.has_key?(:string_filters)

  filter_settings[:boolean_filters].each do |spf|
    options = spf[:options] || {}
    parse_boolean_filter_from_params(spf[:filter_name], options)
  end if filter_settings.has_key?(:boolean_filters)

  filter_settings[:reference_filters].each do |rpf|
    options = rpf[:options] || {}
    parse_reference_filter_from_params(rpf[:filter_name], rpf[:filter_class], options)
  end if filter_settings.has_key?(:reference_filters)

  configure_filter_ui_settings
end

#parse_from_and_to_year_filters(default_year: Date.today.year, last_how_many_years: 6) ⇒ Object



375
376
377
378
# File 'app/helpers/filter_helper.rb', line 375

def parse_from_and_to_year_filters(default_year: Date.today.year, last_how_many_years: 6)
  @to_year = @filters[:to_year] = params[:to_year] ? params[:to_year].to_i : default_year
  @from_year = @filters[:from_year] = params[:from_year] ? params[:from_year].to_i : (@to_year - last_how_many_years)
end

#parse_reference_filter_from_params(filter_name, filter_class, **options) ⇒ Object

Use this method to create a filter if the value is in params as id use parse_reference_filter(:brand, Brand) use parse_reference_filter(:client, Client) This will add 2 filters to @filters hash if id of brand “toyota” is 1 and that of XYZ client is 2 it creates the following @filters parse_reference_filter_from_params(:b, :brand, Brand)



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/helpers/filter_helper.rb', line 168

def parse_reference_filter_from_params(filter_name, filter_class, **options)
  options.reverse_merge!(
    find_by_column: 'id'
  )

  raise "@filter_param_mapping not initialised" unless @filter_param_mapping
  raise "#{filter_name} is not added to @filter_param_mapping" unless @filter_param_mapping.has_key?(filter_name)

  param_name = @filter_param_mapping[filter_name]
  return unless params[param_name]

  if params[param_name].strip.downcase == "null"
    instance_variable_set("@#{filter_name}", "null")
    @filters[filter_name] = "null"
  else
    obj = filter_class.where(options[:find_by_column] => params[param_name].strip).first
    instance_variable_set("@#{filter_name}", obj)
    @filters[filter_name] = obj if obj
  end
end

#parse_string_filter_from_params(filter_name, **options) ⇒ Object

parse_string_filter_from_params(:q, :query)



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/helpers/filter_helper.rb', line 113

def parse_string_filter_from_params(filter_name, **options)
  raise "@filter_param_mapping not initialised" unless @filter_param_mapping
  raise "#{filter_name} is not added to @filter_param_mapping" unless @filter_param_mapping.has_key?(filter_name)

  param_name = @filter_param_mapping[filter_name]
  
  case params[param_name]
  when Array
    obj = params[param_name].uniq.compact
    instance_variable_set("@#{filter_name}", obj)
    @filters[filter_name] = obj if obj  
  when String
    obj = params[param_name].strip
    instance_variable_set("@#{filter_name}", obj)
    @filters[filter_name] = obj if obj  
  when nil
    if options[:default]
      obj = options[:default]
      instance_variable_set("@#{filter_name}", options[:default])
      @filters[filter_name] = options[:default]
    end
  end
end

#report_filter(filter_name, **filter_options) ⇒ Object

Example <%=

  report_filter(
    select_label: "To",
    param_name: 'to_year',
    current_value: @to_year,
    values: Array(2000..(Date.today.year)).reverse,
    current_filters: @filters,
    filters_to_remove: [],
    filters_to_add: {permalink: @current_company_group.permalink, code: @company.code},
    url_method_name: 'company_dashboard_url',
    show_all_filter_on_top: false,
  )
%>


225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'app/helpers/filter_helper.rb', line 225

def report_filter(filter_name, **filter_options)
  filter_options.reverse_merge!(
    select_label: "Select Filter",
    display_hash: {},
    current_value: nil,
    values: {},
    current_filters: {},
    filters_to_remove: [],
    filters_to_add: {},
    url_method_name: 'root_path',
    show_all_filter_on_top: true,
    show_null_filter_on_top: false,
    drop_down_options: {}
  )

  raise "@filter_param_mapping not initialised" unless @filter_param_mapping
  raise "#{filter_name} is not added to @filter_param_mapping" unless @filter_param_mapping.has_key?(filter_name)

  param_name = @filter_param_mapping[filter_name]

  # The text to be displayed will be judged based on the arguments
  # If display_hash is passed and is a valid Hash, it will search for the current_value as key
  # for e.g: if display_hash = {1: "Dog", 2: "Cat"} and current_value is 2, it will display "Cat"
  # If display_hash is not passed, it will simple display current_value

  display_hash = filter_options[:display_hash].stringify_keys
  
  if display_hash.empty?
    selected_text = filter_options[:current_value]
  else
    selected_text = display_hash[filter_options[:current_value].to_s]
  end
  selected_text = filter_options[:select_label] unless selected_text

  # The link should populate a url which has all the current filters
  # It should also be removing the filters which are not required for the next level
  # It should also filter case by case.

  # Clone the existing filters first
  # remove the filters which are to be removed and add which are to be added
  # Also remove the filter which is currently selected
  filter_options[:filters_to_remove] << filter_name.to_sym

  # filters is now a hash with keys looking like year, month etc.
  # However, we need a hash with keys in short form like yr, mn
  # creating the new hash which we need
  filters = generate_short_url_filters(filter_options[:current_filters], filter_options[:filters_to_add], filter_options[:filters_to_remove])

  initial_list = {}
  initial_list["All"] = send(filter_options[:url_method_name], filters) if filter_options[:show_all_filter_on_top]
  initial_list["IS NOT SET"] = send(filter_options[:url_method_name], filters.merge({param_name => :null})) if filter_options[:show_null_filter_on_top]

  # Values can be either Array or Hash
  # If Array is passed, it will populate the Hash it want from the Hash
  # For e.g: if values = ["Jan", "Feb"], theh the hash populated would be {"Jan" => "Jan", "Feb" => "Feb"}
  # This is done as it needs one display value and one passing value
  
  if filter_options[:values].is_a?(Array)
    filter_options[:values] = filter_options[:values].inject({}) do |results, item|
      results[item] = item
      results
    end
  end

  options = filter_options[:values].inject(initial_list) do |results, (name, value)| 
    filters.reject!{|k,v| k == filter_name.to_sym }
    results[name] = {param_name => value}.reverse_merge(filters)
    results
  end

  drop_down_filter(selected_text, options, filter_options[:drop_down_options])
end

#report_object_filter(filter_name, **filter_options) ⇒ Object

Example <%=

  report_object_filter(
    select_label: 'Select City',
    param_name: 'city',
    current_value: @city,
    values: City.order(:name).all,
    current_filters: @filters,
    url_method_name: 'company_dashboard_url',
    filters_to_add: {permalink: @current_company_group.permalink, code: @company.code},
  )
%>


311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'app/helpers/filter_helper.rb', line 311

def report_object_filter(filter_name, **filter_options)
  filter_options.reverse_merge!(
    select_label: 'Select Filter',
    current_value: nil,
    values: [],
    current_filters: {},
    url_method_name: 'root_path',
    filters_to_add: {},
    filters_to_remove: [],
    show_all_filter_on_top: true,
    show_null_filter_on_top: false,
    display_method: :display_name
  )

  raise "@filter_param_mapping not initialised" unless @filter_param_mapping
  raise "#{filter_name} is not added to @filter_param_mapping" unless @filter_param_mapping.has_key?(filter_name)

  param_name = @filter_param_mapping[filter_name]

  if filter_options[:current_value].to_s == "null"
    selected_text = "#{filter_name.to_s.titleize} IS NOT SET"
  elsif filter_options.has_key?(:display_method) && filter_options[:current_value]
    selected_text = filter_options[:current_value].send(filter_options[:display_method])
  end
  selected_text = filter_options[:select_label] unless selected_text

  # Clone the existing filters first
  # remove the filters which are to be removed and add which are to be added
  # Also remove the filter which is currently selected

  unless filter_options[:current_filters].blank?
    temp_filters = {}
  else
    temp_filters = filter_options[:current_filters].clone
  end
  temp_filters.merge!(filter_options[:filters_to_add])
  temp_filters.reject!{|k,v| filter_options[:filters_to_remove].include?(k)}
  temp_filters.reject!{|k,v| k == filter_name.to_sym }

  # filters is now a hash with keys looking like year, month etc.
  # However, we need a hash with keys in short form like yr, mn
  # creating the new hash which we need
  filters = {}
  temp_filters.each do |k,v|
    short_k = @filter_param_mapping[k]
    filters[short_k] = v
  end

  initial_list = {}
  initial_list["All"] = send(filter_options[:url_method_name], filters) if filter_options[:show_all_filter_on_top]
  initial_list["IS NOT SET"] = send(filter_options[:url_method_name], filters.merge({param_name => :null})) if filter_options[:show_null_filter_on_top]
  
  options = filter_options[:values].inject(initial_list) do |results, item| 
    display_value = item.send(filter_options[:display_method])
    filters = {param_name => item}.reverse_merge(filters)
    results[display_value] = send(filter_options[:url_method_name], filters)
    results
  end

  drop_down_filter(selected_text, options)
end