Module: Noventius::FiltersHelper

Includes:
FilterWrappers
Defined in:
app/helpers/noventius/filters_helper.rb

Constant Summary collapse

RESERVED_OPTIONS =
[:dependent]

Constants included from FilterWrappers

Noventius::FilterWrappers::DEFAULT_CHECK_BOX_VALUE, Noventius::FilterWrappers::DEFAULT_RADIO_BUTTON_VALUE

Instance Method Summary collapse

Methods included from FilterWrappers

#check_box_filter_tag, #color_filter_tag, #date_filter_tag, #datetime_filter_tag, #email_filter_tag, included, #month_filter_tag, #number_filter_tag, #phone_filter_tag, #radio_button_filter_tag, #range_filter_tag, #search_filter_tag, #select_filter_tag, #telephone_filter_tag, #text_area_filter_tag, #text_filter_tag, #time_filter_tag, #url_filter_tag, #week_filter_tag

Instance Method Details

#class_for_filter(filter) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'app/helpers/noventius/filters_helper.rb', line 28

def class_for_filter(filter)
  classes = ['noventius-filter']

  unless filter.type == :select
    classes << 'form-control'
  end

  classes.join(' ')
end

#class_for_filter_wrapper(filter) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'app/helpers/noventius/filters_helper.rb', line 18

def class_for_filter_wrapper(filter)
  classes = ['form-group']

  if filter.type == :select
    classes << 'select-filter-wrapper'
  end

  classes.join(' ')
end

#compile_select_option_tags(filter, report) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/helpers/noventius/filters_helper.rb', line 58

def compile_select_option_tags(filter, report)
  option_tags = filter.args[:option_tags]
  option_tags = option_tags.is_a?(Symbol) ? report.send(option_tags) : option_tags
  current_value = report.filter_params[filter.name]

  if filter.args[:dependent].present? && option_tags.is_a?(Hash)
    return ''
  elsif filter.args[:dependent].present?
    fail ArgumentError, 'when a dependent select option_tags can only be a Hash.'
  end

  if option_tags.is_a?(String)
    option_tags.html_safe
  elsif option_tags.is_a?(Array)
    if option_tags.size == 1 || option_tags.size == 2
      if option_tags.size == 2
        option_tags[1] = current_value || option_tags[1]
      else
        option_tags << current_value
      end
      options_for_select(*option_tags)
    elsif option_tags.size == 3 || option_tags.size == 4
      if option_tags.size == 4
        option_tags[3] = current_value || option_tags[3]
      else
        option_tags << current_value
      end
      options_from_collection_for_select(*option_tags)
    else
      fail ArgumentError, 'option_tags can only be a String, an Array(max size 4) or a Symbol.'
    end
  else
    fail ArgumentError, 'option_tags can only be a String, an Array(max size 4) or a Symbol.'
  end
end

#filter_tag(filter, report, options = {}) ⇒ Object



10
11
12
13
14
15
16
# File 'app/helpers/noventius/filters_helper.rb', line 10

def filter_tag(filter, report, options = {})
  tag_options = set_current_filter_value(filter, report)
  tag_options = merge_filter_options(filter, options, tag_options)
  tag_options = add_filter_options(filter, report, tag_options)

  send(:"#{filter.type}_filter_tag", scope_name(filter.name), tag_options)
end

#set_current_filter_value(filter, report) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/helpers/noventius/filters_helper.rb', line 38

def set_current_filter_value(filter, report)
  tag_options = filter.args.dup
  current_value = report.filter_params[filter.name]

  case filter.type
  when :select
    tag_options[:option_tags] = compile_select_option_tags(filter, report)
  when :check_box
    tag_options[:checked] = current_value == (tag_options[:value] || DEFAULT_CHECK_BOX_VALUE)
  when :radio_button
    tag_options[:checked] = current_value == (tag_options[:value] || DEFAULT_RADIO_BUTTON_VALUE)
  when :text_area
    tag_options[:content] = current_value || tag_options[:content]
  else
    tag_options[:value] = current_value || tag_options[:value]
  end

  tag_options
end