Module: ActiveAdmin::Filters::ViewHelper

Defined in:
lib/active_admin/filters/forms.rb

Overview

This module is included into the view

Instance Method Summary collapse

Instance Method Details

#active_admin_filters_form_for(search, filters, options = {}) ⇒ Object

Helper method to render a filter form



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/active_admin/filters/forms.rb', line 57

def active_admin_filters_form_for(search, filters, options = {})
  defaults = { :builder => ActiveAdmin::Filters::FormBuilder,
               :url     => collection_path,
               :html    => {:class  => 'filter_form'} }
  required = { :html    => {:method => :get},
               :as      => :q }
  options  = defaults.deep_merge(options).deep_merge(required)

  form_for search, options do |f|
    filters.group_by{ |o| o[:attribute] }.each do |attribute, array|
      opts     = array.last # grab last-defined `filter` call from DSL
      should   = opts.delete(:if)     || proc{ true }
      shouldnt = opts.delete(:unless) || proc{ false }

      if call_method_or_proc_on(self, should) && !call_method_or_proc_on(self, shouldnt)
        f.filter attribute, opts
      end
    end

    buttons =  :div, :class => "buttons" do
      f.submit(I18n.t('active_admin.filters.buttons.filter')) +
        link_to(I18n.t('active_admin.filters.buttons.clear'), '#', :class => 'clear_filters_btn') +
        hidden_field_tags_for(params, :except => [:q, :page])
    end

    f.form_buffers.last + buttons
  end
end