Class: AppKit::FilterFormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/app_kit/filter_form_builder.rb

Instance Method Summary collapse

Instance Method Details

#filter(field) ⇒ Object



71
72
73
# File 'lib/app_kit/filter_form_builder.rb', line 71

def filter(field)
  label(field) + filter_field(field)
end

#filter_field(field) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/app_kit/filter_form_builder.rb', line 47

def filter_field(field)
  case field.data_type
  when :date, :datetime
    @template. :div, class: 'range-filter date-range' do
      @template.search_field_tag(get_field_name(field,'from'), nil, placeholder: 'from') +
        @template.search_field_tag(get_field_name(field,'to'), nil, placeholder: 'to')
    end
  when :boolean
    value = filter_params[field.name]["value"] rescue nil
    options = @template.options_for_select([['Any', ''],['Yes', 'true'], ['No', 'false']], value)
    @template.select_tag get_field_name(field, 'value'), options
  when :integer
    if field.is_foreign_key?
      @template.collection_select get_field_name(field), nil,
        field.association.klass.all, :id, :to_s,
        prompt: 'Any'
    else
      predicate_filter(field)
    end
  else
    predicate_filter(field)
  end
end

#filter_paramsObject



3
4
5
# File 'lib/app_kit/filter_form_builder.rb', line 3

def filter_params
  @template.params[param_set.to_sym].try(:first)
end

#get_field_name(field, tag = "value") ⇒ Object



10
11
12
# File 'lib/app_kit/filter_form_builder.rb', line 10

def get_field_name(field, tag="value")
  "#{param_set}[][#{field.name}][#{tag}]"
end

#label(field) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/app_kit/filter_form_builder.rb', line 13

def label(field)
  css = ""
  if filter_params
    if filter_params[field.name]["value"].present?
      css << " highlight"
    end
  end
  @template.label param_set, field.name, field.display_name, class: css
end

#param_setObject



7
8
9
# File 'lib/app_kit/filter_form_builder.rb', line 7

def param_set
  "#{@object_name.underscore}_filter"
end

#predicate_filter(field) ⇒ Object



76
77
78
79
80
81
# File 'lib/app_kit/filter_form_builder.rb', line 76

def predicate_filter(field)
  @template. :div, class: 'predicate-filter' do
    @template.(:div, predicate_select(field), class: 'predicate-select') +
      @template.(:div, search_input(field), class: 'value')
  end
end

#predicate_select(field) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/app_kit/filter_form_builder.rb', line 27

def predicate_select(field)
  select_options_array = case field.data_type
                         when :integer, :decimal
                           [
                             ['equals', 'eq'],
                             ['greater than','gt'],
                             ['less than', 'lt'],
                             ['not equal', 'nq']
                           ]
                         else
                           [
                             ['contains', 'cont'],
                             ['doesn\'t contain', 'ncont']
                           ]
                         end
  @template.select_tag get_field_name(field, 'condition'),
    @template.options_for_select(select_options_array)
end

#search_input(field) ⇒ Object



22
23
24
25
# File 'lib/app_kit/filter_form_builder.rb', line 22

def search_input(field)
  value = filter_params[field.name]["value"] rescue nil
  @template.search_field_tag get_field_name(field), value
end