Class: ActiveAdmin::FilterFormBuilder

Inherits:
FormBuilder
  • Object
show all
Defined in:
lib/active_admin/filter_form_builder.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

#active_admin_input_class_name, #buttons, #cancel_link, #commit_button, #commit_button_with_cancel_link, #has_many, #initialize, #input, #input_class, #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



43
44
45
# File 'lib/active_admin/filter_form_builder.rb', line 43

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

#custom_input_class_name(as) ⇒ Object (protected)



37
38
39
# File 'lib/active_admin/filter_form_builder.rb', line 37

def custom_input_class_name(as)
  "ActiveAdmin::Inputs::Filter#{as.to_s.camelize}Input"
end

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

Returns the default filter type for a given attribute



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_admin/filter_form_builder.rb', line 17

def default_input_type(method, options = {})
  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

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



6
7
8
9
10
11
12
# File 'lib/active_admin/filter_form_builder.rb', line 6

def filter(method, options = {})
  return "" if method.nil? || method == ""
  options[:as] ||= default_input_type(method)
  return "" unless options[:as]
  content = input(method, options)
  form_buffers.last << content.html_safe if content
end

#reflection_for(method) ⇒ Object (protected)

Returns the association reflection for the method if it exists



48
49
50
# File 'lib/active_admin/filter_form_builder.rb', line 48

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