Class: ActiveAdmin::FilterFormBuilder

Inherits:
FormBuilder show all
Defined in:
lib/active_admin/view_helpers/filter_form_helper.rb

Overview

This form builder defines methods to build filter forms such as the one found in the sidebar of the index page of a standard resource.

Instance Attribute Summary

Attributes inherited from FormBuilder

#form_buffers

Instance Method Summary collapse

Methods inherited from FormBuilder

#buttons, #cancel_link, #commit_button, #commit_button_with_cancel_link, #datepicker_input, #has_many, #initialize, #input, #inputs

Constructor Details

This class inherits a constructor from ActiveAdmin::FormBuilder

Instance Method Details

#column_for(method) ⇒ Object (protected)

Returns the column for an attribute on the object being searched if it exists. Otherwise returns nil



176
177
178
# File 'lib/active_admin/view_helpers/filter_form_helper.rb', line 176

def column_for(method)
  @object.base.columns_hash[method.to_s] if @object.base.respond_to?(:columns_hash)
end

#current_numeric_scope(filters) ⇒ Object (protected)

Returns the scope for which we are currently searching. If no search is available it returns the first scope



98
99
100
# File 'lib/active_admin/view_helpers/filter_form_helper.rb', line 98

def current_numeric_scope(filters)
  filters[1..-1].inject(filters.first){|a,b| object.send(b[1].to_sym) ? b : a }[1]
end

#default_filter_label(method) ⇒ Object (protected)

Returns the default label for a given attribute Will use ActiveModel I18n if possible



166
167
168
169
170
171
172
# File 'lib/active_admin/view_helpers/filter_form_helper.rb', line 166

def default_filter_label(method)
  if @object.base.respond_to?(:human_attribute_name)
    @object.base.human_attribute_name(method)
  else
    method.to_s.titlecase
  end
end

#default_filter_type(method) ⇒ Object (protected)

Returns the default filter type for a given attribute



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/active_admin/view_helpers/filter_form_helper.rb', line 144

def default_filter_type(method)
  if column = column_for(method)
    case column.type
    when :date, :datetime
      return :date_range
    when :string, :text
      return :string
    when :integer
      return :select if reflection_for(method.to_s.gsub('_id','').to_sym)
      return :numeric
    when :float, :decimal
      return :numeric
    end
  end

  if reflection = reflection_for(method)
    return :select if reflection.macro == :belongs_to && !reflection.options[:polymorphic]
  end
end

#default_numeric_filtersObject (protected)



102
103
104
# File 'lib/active_admin/view_helpers/filter_form_helper.rb', line 102

def default_numeric_filters
  [[I18n.t('active_admin.equal_to'), 'eq'], [I18n.t('active_admin.greater_than'), 'gt'], [I18n.t('active_admin.less_than'), 'lt']]
end

#filter(method, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/active_admin/view_helpers/filter_form_helper.rb', line 40

def filter(method, options = {})
  return "" if method.nil? || method == ""
  options[:as] ||= default_filter_type(method)
  return "" unless options[:as]
  field_type = options.delete(:as)
  options[:label] ||= default_filter_label(method)
  content = with_new_form_buffer do
    send("filter_#{field_type}_input", method, options)
  end
  @form_buffers.last << template.(:div, content, :class => "filter_form_field filter_#{field_type}")
end

#filter_check_boxes_input(method, options = {}) ⇒ Object (protected)



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/active_admin/view_helpers/filter_form_helper.rb', line 116

def filter_check_boxes_input(method, options = {})
  input_name = (generate_association_input_name(method).to_s + "_in").to_sym
  collection = find_collection_for_column(method, options)
  selected_values = @object.send(input_name) || []
  checkboxes = template. :div, :class => "check_boxes_wrapper" do
    collection.map do |c|
      label = c.is_a?(Array) ? c.first : c
      value = c.is_a?(Array) ? c.last : c
      "<label><input type=\"checkbox\" name=\"q[#{input_name}][]\" value=\"#{value}\" #{selected_values.include?(value) ? "checked" : ""}/> #{label}</label>"
    end.join("\n").html_safe
  end

  [ label(input_name, options[:label]),
    checkboxes
  ].join("\n").html_safe
end

#filter_date_range_input(method, options = {}) ⇒ Object (protected)



62
63
64
65
66
67
68
69
70
71
# File 'lib/active_admin/view_helpers/filter_form_helper.rb', line 62

def filter_date_range_input(method, options = {})
  gt_field_name = "#{method}_gte"
  lt_field_name = "#{method}_lte"

  [ label(gt_field_name, options[:label]),
    filter_date_text_field(gt_field_name),
    template.(:span, "-", :class => "seperator"),
    filter_date_text_field(lt_field_name)
  ].join("\n").html_safe
end

#filter_date_text_field(method) ⇒ Object (protected)



73
74
75
76
# File 'lib/active_admin/view_helpers/filter_form_helper.rb', line 73

def filter_date_text_field(method)
  current_value = @object.send(method)
  text_field(method, :size => 12, :class => "datepicker", :max => 10, :value => current_value.respond_to?(:strftime) ? current_value.strftime("%Y-%m-%d") : "")
end

#filter_numeric_input(method, options = {}) ⇒ Object (protected)



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/active_admin/view_helpers/filter_form_helper.rb', line 78

def filter_numeric_input(method, options = {})
  filters = numeric_filters_for_method(method, options.delete(:filters) || default_numeric_filters)
  current_filter = current_numeric_scope(filters)
  filter_select = @template.select_tag '', @template.options_for_select(filters, current_filter),
                              :onchange => "document.getElementById('#{method}_numeric').name = 'q[' + this.value + ']';"
  filter_input = text_field current_filter, :size => 10, :id => "#{method}_numeric"

  [ label(method),
    filter_select,
    " ",
    filter_input
  ].join("\n").html_safe
end

#filter_select_input(method, options = {}) ⇒ Object (protected)



106
107
108
109
110
111
112
113
114
# File 'lib/active_admin/view_helpers/filter_form_helper.rb', line 106

def filter_select_input(method, options = {})
  association_name = method.to_s.gsub(/_id$/, '').to_sym
  input_name = (generate_association_input_name(method).to_s + "_eq").to_sym
  collection = find_collection_for_column(association_name, options)

  [ label(input_name, options[:label]),
    select(input_name, collection, options.merge(:include_blank => I18n.t('active_admin.any')))
  ].join("\n").html_safe
end

#filter_string_input(method, options = {}) ⇒ Object (protected)



54
55
56
57
58
59
60
# File 'lib/active_admin/view_helpers/filter_form_helper.rb', line 54

def filter_string_input(method, options = {})
  field_name = "#{method}_contains"

  [ label(field_name, I18n.t('active_admin.search_field', :field => options[:label])),
    text_field(field_name)
  ].join("\n").html_safe
end

#find_collection_for_column(method, options = {}) ⇒ Object (protected)

Override the standard finder to accept a proc



134
135
136
137
138
139
140
141
# File 'lib/active_admin/view_helpers/filter_form_helper.rb', line 134

def find_collection_for_column(method, options = {})
  options = options.dup
  case options[:collection]
  when Proc
    options[:collection] = options[:collection].call
  end
  super(method, options)
end

#numeric_filters_for_method(method, filters) ⇒ Object (protected)



92
93
94
# File 'lib/active_admin/view_helpers/filter_form_helper.rb', line 92

def numeric_filters_for_method(method, filters)
  filters.collect{|scope| [scope[0], [method,scope[1]].join("_") ] }
end

#reflection_for(method) ⇒ Object (protected)

Returns the association reflection for the method if it exists



181
182
183
# File 'lib/active_admin/view_helpers/filter_form_helper.rb', line 181

def reflection_for(method)
  @object.base.reflect_on_association(method) if @object.base.respond_to?(:reflect_on_association)
end