Module: AppKit::FilterHelper

Defined in:
app/helpers/app_kit/filter_helper.rb

Instance Method Summary collapse

Instance Method Details

#boolean_select_options(val = nil) ⇒ Object

Returns select options for boolean filters



54
55
56
# File 'app/helpers/app_kit/filter_helper.rb', line 54

def boolean_select_options(val=nil)
  options_for_select([['Yes', 1], ['No', 0]], val)
end

#filter_field(search_form, field, search_instance) ⇒ Object

create form fields for side bar filtering.

Parameters:

  • search_form (Ransack::SearchForm)

    The search form object.

  • field (AppKit::Field)

    The field to create filters for.

  • The

    search instance from ransack (usually %q)



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/helpers/app_kit/filter_helper.rb', line 7

def filter_field(search_form, field, search_instance)
  case field.data_type
  when :boolean
    search_form.select "#{field.name}_eq",
      boolean_select_options, prompt: 'Any'
  when :datetime
     :div, class: 'range-filter date-range' do
      concat search_form.search_field("#{field.name}_gteq", placeholder: 'from')
      concat search_form.search_field("#{field.name}_lt", placeholder: 'to')
    end
  when :string
    predicate = get_field_predicate(search_instance,field,'cont')
    options = string_predicate_options(search_instance, field)
    predicate_select = select_tag("#{field.name}_predicate",options)
    search_field = filter_search_field(search_form, field, predicate)
     :div, class: 'predicate-filter' do
      concat (:div, predicate_select, class: 'predicate-select')
      concat (:div, search_field, class: 'value')
    end
  when :integer, :decimal
    if field.is_foreign_key?
      search_form.collection_select field.name,
        field.association.klass.all, :id, :to_s,
        prompt: 'Any'
    else
      predicate = get_field_predicate(search_instance, field, 'eq')
      options = number_predicate_options(search_instance, field)
      predicate_select = select_tag("#{field.name}_predicate",options)
      search_field = filter_search_field(search_form, field, predicate)
       :div, class: 'predicate-filter' do
        concat (:div, predicate_select, class: 'predicate-select')
        concat (:div, search_field, class: 'value')
      end
    end
  else
    predicate = get_field_predicate(search_instance,field,'cont')
    options = string_predicate_options(search_instance, field)
    predicate_select = select_tag("#{field.name}_predicate",options)
    search_field = filter_search_field(search_form, field, predicate)
     :div, class: 'predicate-filter' do
      concat (:div, predicate_select, class: 'predicate-select')
      concat (:div, search_field, class: 'value')
    end
  end
end

#filter_form_for(resource, *args, &block) ⇒ Object



101
102
103
104
105
106
107
# File 'app/helpers/app_kit/filter_helper.rb', line 101

def filter_form_for(resource, *args, &block)
  options = args.extract_options!
  options.update(:builder => AppKit::FilterFormBuilder)
  options.update(:url => ak_path(resource.model))
  options.update(:method => 'get')
  form_for(resource.model.name, options, &block)
end

#filter_search_field(search_form, field, predicate) ⇒ Object

Creates the search field for filters.

Parameters:

  • field (AppKit::Field)

    the field to generate a filter for.

  • predicate (String)

    the predicate condition.



93
94
95
96
97
98
99
# File 'app/helpers/app_kit/filter_helper.rb', line 93

def filter_search_field(search_form, field, predicate)
  search_method = "#{field.name}_#{predicate}"
  css = ""
  css = "datepicker" if field.data_type == :date || field.data_type == :datetime
  search_form.search_field  search_method.to_sym,
    "data-field" => field.name.to_s
end

#get_field_predicate(search_instance, field, default) ⇒ Object

Get current search instance predicate or return default value.

Parameters:

  • search_instance (Ransack Search Instance)

    usually %q.

  • field (AppKit::Field)

    field to check.

  • default (String)

    the default value if none is found.



62
63
64
65
66
# File 'app/helpers/app_kit/filter_helper.rb', line 62

def get_field_predicate(search_instance,field,default)
  search_instance.conditions.find {|i|
    i.attributes.first.name == field.name.to_s
  }.try(:predicate).try(:name) || default
end

#number_predicate_options(search_instance, field) ⇒ Object

Condition select options for number based fields.

Parameters:

  • search_instance (Ransack Search Instance)

    usually %q

  • field (AppKit::Field)

    the field to generate a filter for.



80
81
82
83
84
85
86
87
# File 'app/helpers/app_kit/filter_helper.rb', line 80

def number_predicate_options(search_instance,field)
  predicate = get_field_predicate(search_instance,field,'eq')
  options = [
    ['equals', 'eq'],["not equal", 'not_eq'],
    ['greater than', 'gt'], ['less than', 'lt']
  ]
  options_for_select(options,predicate)
end

#string_predicate_options(search_instance, field) ⇒ Object

Condition select options for string based fields.

Parameters:

  • search_instance (Ransack Search Instance)

    usually %q

  • field (AppKit::Field)

    the field to generate a filter for.



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

def string_predicate_options(search_instance, field)
  predicate = get_field_predicate(search_instance,field,'cont')
  options = [['contains', 'cont'],["doesn't contain", 'not_cont']]
  options_for_select(options,predicate)
end