|
| 1 | +module ActiveAdmin |
| 2 | + module Filters |
| 3 | + |
| 4 | + # This form builder defines methods to build filter forms such |
| 5 | + # as the one found in the sidebar of the index page of a standard resource. |
| 6 | + class FormBuilder < ::ActiveAdmin::FormBuilder |
| 7 | + |
| 8 | + def filter(method, options = {}) |
| 9 | + return "" if method.nil? || method == "" |
| 10 | + options[:as] ||= default_input_type(method) |
| 11 | + return "" unless options[:as] |
| 12 | + content = input(method, options) |
| 13 | + form_buffers.last << content.html_safe if content |
| 14 | + end |
| 15 | + |
| 16 | + protected |
| 17 | + |
| 18 | + # Returns the default filter type for a given attribute |
| 19 | + def default_input_type(method, options = {}) |
| 20 | + if column = column_for(method) |
| 21 | + case column.type |
| 22 | + when :date, :datetime |
| 23 | + return :date_range |
| 24 | + when :string, :text |
| 25 | + return :string |
| 26 | + when :integer |
| 27 | + return :select if reflection_for(method.to_s.gsub('_id','').to_sym) |
| 28 | + return :numeric |
| 29 | + when :float, :decimal |
| 30 | + return :numeric |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + if reflection = reflection_for(method) |
| 35 | + return :select if reflection.macro == :belongs_to && !reflection.options[:polymorphic] |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + def custom_input_class_name(as) |
| 40 | + "ActiveAdmin::Inputs::Filter#{as.to_s.camelize}Input" |
| 41 | + end |
| 42 | + |
| 43 | + # Returns the column for an attribute on the object being searched |
| 44 | + # if it exists. Otherwise returns nil |
| 45 | + def column_for(method) |
| 46 | + @object.base.columns_hash[method.to_s] if @object.base.respond_to?(:columns_hash) |
| 47 | + end |
| 48 | + |
| 49 | + # Returns the association reflection for the method if it exists |
| 50 | + def reflection_for(method) |
| 51 | + @object.base.reflect_on_association(method) if @object.base.respond_to?(:reflect_on_association) |
| 52 | + end |
| 53 | + |
| 54 | + end |
| 55 | + |
| 56 | + |
| 57 | + # This module is included into the view |
| 58 | + module ViewHelper |
| 59 | + |
| 60 | + # Helper method to render a filter form |
| 61 | + def active_admin_filters_form_for(search, filters, options = {}) |
| 62 | + options[:builder] ||= ActiveAdmin::Filters::FormBuilder |
| 63 | + options[:url] ||= collection_path |
| 64 | + options[:html] ||= {} |
| 65 | + options[:html][:method] = :get |
| 66 | + options[:html][:class] ||= "filter_form" |
| 67 | + options[:as] = :q |
| 68 | + clear_link = link_to(I18n.t('active_admin.clear_filters'), "#", :class => "clear_filters_btn") |
| 69 | + form_for search, options do |f| |
| 70 | + filters.each do |filter_options| |
| 71 | + filter_options = filter_options.dup |
| 72 | + attribute = filter_options.delete(:attribute) |
| 73 | + f.filter attribute, filter_options |
| 74 | + end |
| 75 | + |
| 76 | + buttons = content_tag :div, :class => "buttons" do |
| 77 | + f.submit(I18n.t('active_admin.filter')) + |
| 78 | + clear_link + |
| 79 | + hidden_field_tag("order", params[:order]) + |
| 80 | + hidden_field_tag("scope", params[:scope]) |
| 81 | + end |
| 82 | + |
| 83 | + f.form_buffers.last + buttons |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + end |
| 88 | + |
| 89 | + end |
| 90 | +end |
0 commit comments