Module: CmAdmin::ViewHelpers::FilterHelper

Included in:
CmAdmin::ViewHelpers
Defined in:
lib/cm_admin/view_helpers/filter_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_date_filter(filter) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/cm_admin/view_helpers/filter_helper.rb', line 114

def add_date_filter(filter)
  value = params.dig(:filters, :date, :"#{filter.db_column_name}")
  concat((:div, class: "position-relative mr-3 #{value ? '' : 'hidden'}") do
    concat filter_chip(value, filter)

    concat((:div, class: 'date-filter-wrapper w-100') do
      concat tag.input class: 'w-100 pb-1', value: "#{value ? value : ''}", data: {behaviour: 'filter', filter_type: "#{filter.filter_type}", db_column: "#{filter.db_column_name}"}
    end)
  end)
  return
end

#add_filters_dropdown(filters) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cm_admin/view_helpers/filter_helper.rb', line 21

def add_filters_dropdown(filters)
  concat((:div, class: 'dropdown add-filter-btn', data: {bs_toggle: 'dropdown'}) do
    tag.span '+ Add filter'
  end)

  concat((:div, class: 'dropdown-menu dropdown-popup') do
    concat((:div, class: 'popup-base') do
      concat((:div, class: 'popup-inner') do
        concat((:div, class: 'search-area') do
          concat tag.input placeholder: 'Search for filter', data: {behaviour: 'dropdown-filter-search'}
        end)
        concat((:div, class: 'list-area') do
          filters.each do |filter|
            concat((:div, class: 'pointer list-item', data: {behaviour: 'filter-option', filter_type: "#{filter.filter_type}", db_column: "#{filter.db_column_name}"}) do
              tag.span filter.db_column_name.to_s.titleize
            end)
          end
        end)
      end)
    end)
  end)
  return
end

#add_multi_select_filter(filter) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/cm_admin/view_helpers/filter_helper.rb', line 162

def add_multi_select_filter(filter)
  value = params.dig(:filters, :"#{filter.filter_type}", :"#{filter.db_column_name}")
  if value && filter.collection[0].class == Array
    value_mapped_text = []
    filter.collection.each do |array|
      value_mapped_text << array[0].titleize if value.include?(array[1].to_s)
    end
  else
    value_mapped_text = value
  end
  
  concat((:div, class: "position-relative mr-3 #{value ? '' : 'hidden'}") do
    concat filter_chip(value_mapped_text, filter)

    concat((:div, class: 'position-absolute mt-2 dropdown-popup hidden') do
      concat((:div, class: 'popup-base') do
        concat((:div, class: 'popup-inner') do
          concat((:div, class: "#{value ? 'search-with-chips' : 'search-area'}") do
            if value_mapped_text
              value_mapped_text.each do |val|
                concat((:div, class: 'chip') do
                  concat tag.span val
                  concat((:span, data: { behaviour: 'selected-chip' }) do
                    tag.i class: 'fa fa-times'
                  end)
                end)
              end
            end
            concat tag.input placeholder: "#{filter.placeholder}", data: {behaviour: 'dropdown-filter-search'}
          end)
          concat((:div, class: 'list-area') do
            filter.collection.each do |val|
              if val.class.eql?(Array)
                filter_value = val[1]
                filter_text = val[0]
              elsif val.class.eql?(String)
                filter_value = filter_text = val
              end
              concat((:div, class: "pointer list-item #{(value && (value.eql?(val) || value.include?(filter_value.to_s))) ? 'selected' : ''}", data: {behaviour: 'select-option', filter_type: "#{filter.filter_type}", db_column: "#{filter.db_column_name}", value: filter_value}) do
                concat tag.input class: 'cm-checkbox', type: 'checkbox', checked: value ? value.include?(filter_value.to_s) : false
                concat tag.label filter_text.to_s.titleize, class: 'pointer'
              end)
            end
          end)
          concat tag.div 'Apply', class: "apply-area #{value ? 'active' : ''}"
        end)
      end)
    end)
  end)
  return
end

#add_range_filter(filter) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/cm_admin/view_helpers/filter_helper.rb', line 101

def add_range_filter(filter)
  value = params.dig(:filters, :range, :"#{filter.db_column_name}")
  concat((:div, class: "position-relative mr-3 #{value ? '' : 'hidden'}") do
    concat filter_chip(value, filter)

    concat((:div, class: 'position-absolute mt-2 range-container hidden') do
      concat tag.input type: 'number', min: '0', step: '1', class: 'range-item', value: "#{value ? value.split(' to ')[0] : ''}", placeholder: 'From', data: {behaviour: 'filter', filter_type: "#{filter.filter_type}", db_column: "#{filter.db_column_name}"}
      concat tag.input type: 'number', min: '0', step: '1', class: 'range-item', value: "#{value ? value.split(' to ')[1] : ''}", placeholder: 'To', data: {behaviour: 'filter', filter_type: "#{filter.filter_type}", db_column: "#{filter.db_column_name}"}
    end)
  end)
  return
end

#add_search_filter(filter) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cm_admin/view_helpers/filter_helper.rb', line 87

def add_search_filter(filter)
  tag.div class: 'filter-search mr-3' do
    tag.div class: 'form-field' do
      tag.div class: 'field-input-wrapper' do
        concat((:input, class: 'search-input', value: "#{params.dig(:filters, :search)}", placeholder: "#{filter.placeholder}", data: {behaviour: 'input-search'}) do
          tag.span class: 'search-input-icon' do
            tag.i class: 'fa fa-search'
          end
        end)
      end
    end
  end
end

#add_single_select_filter(filter) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/cm_admin/view_helpers/filter_helper.rb', line 126

def add_single_select_filter(filter)
  value = params.dig(:filters, :"#{filter.filter_type}", :"#{filter.db_column_name}")
  concat((:div, class: "position-relative mr-3 #{value ? '' : 'hidden'}") do
    if value && filter.collection[0].class == Array
      selected_value_text = filter.collection.map{|collection| collection[0] if collection[1].to_s.eql?(value) }.compact.join(', ')
    else
      selected_value_text = value
    end
    concat filter_chip(selected_value_text, filter)

    concat((:div, class: 'dropdown-menu dropdown-popup') do
      concat((:div, class: 'popup-base') do
        concat((:div, class: 'popup-inner') do
          concat((:div, class: 'search-area') do
            concat tag.input placeholder: "#{filter.placeholder}", data: {behaviour: 'dropdown-filter-search'}
          end)
          concat((:div, class: 'list-area') do
            filter.collection.each do |val|
              if val.class.eql?(Array)
                filter_value = val[1]
                filter_text = val[0]
              elsif val.class.eql?(String)
                filter_value = filter_text = val
              end
              concat((:div, class: "pointer list-item #{(value.present? && value.eql?(filter_value.to_s)) ? 'selected' : ''}", data: {behaviour: 'select-option', filter_type: "#{filter.filter_type}", db_column: "#{filter.db_column_name}", value: filter_value}) do
                concat tag.span filter_text.to_s
              end)
            end
          end)
        end)
      end)
    end)
  end)
  return
end

#clear_filtersObject



45
46
47
48
49
50
# File 'lib/cm_admin/view_helpers/filter_helper.rb', line 45

def clear_filters
  concat((:div, class: "clear-btn #{params.dig(:filters) ? '' : 'hidden'}") do
    tag.span 'Clear all'
  end)
  return
end

#filter_chip(value, filter) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cm_admin/view_helpers/filter_helper.rb', line 68

def filter_chip(value, filter)
  data_hash = {behaviour: 'filter-input', filter_type: "#{filter.filter_type}", db_column: "#{filter.db_column_name}"}
  data_hash.merge!(bs_toggle: 'dropdown') if filter.filter_type.to_s.eql?('single_select')

  if value && filter.filter_type.to_s.eql?('multi_select')
    truncated_value = value[0]
    truncated_value += " + #{value.size - 1} more" if value.size > 1
  end

  concat((:div, class: "filter-chip #{filter.filter_type.to_s.eql?('single_select') ? 'dropdown' : ''}", data: data_hash) do
    concat tag.span "#{filter.db_column_name.to_s.titleize} is "
    concat tag.span "#{filter.filter_type.to_s.eql?('multi_select') ? truncated_value : value}"
    concat((:div, class: "filter-chip-remove #{value ? '' : 'hidden'}") do
      tag.i class: 'fa fa-times bolder'
    end)
  end)
  return
end

#filter_ui(filters) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cm_admin/view_helpers/filter_helper.rb', line 52

def filter_ui(filters)
  filters.each do |filter|
    case filter.filter_type
    when :date
      concat add_date_filter(filter)
    when :range
      concat add_range_filter(filter)
    when :single_select
      concat add_single_select_filter(filter)
    when :multi_select
      concat add_multi_select_filter(filter)
    end
  end
  return
end

#generate_filters(filters) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/cm_admin/view_helpers/filter_helper.rb', line 5

def generate_filters(filters)
  search_filter = filters.select{ |x| x.filter_type.eql?(:search) }.last
  other_filters = filters.reject{ |x| x.filter_type.eql?(:search) }
  concat((:div, class: 'cm-filters-v2') do
    concat((:div, class: 'cm-filters-v2__inner') do
      concat add_search_filter(search_filter) if search_filter
      if other_filters.any?
        concat filter_ui(other_filters)
        concat add_filters_dropdown(other_filters)
      end
      concat clear_filters
    end)
  end)
  return
end