Created
December 8, 2012 15:45
-
-
Save dmitry/4240801 to your computer and use it in GitHub Desktop.
Custom filters/search in sidebar for active_admin using a partial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
= form_for @search, url: collection_path, | |
as: :q, | |
builder: ActiveAdmin::Filters::CustomFormBuilder, | |
html: {method: :get, class: :filter_form} do |f| | |
= f.input :code_contains, as: :string | |
= f.input :code_contains, as: :filter_string | |
= f.form_buffers.last | |
= f.buttons do | |
= f.submit I18n.t('active_admin.filter') | |
= link_to(I18n.t('active_admin.clear_filters'), "#", :class => "clear_filters_btn") | |
= hidden_field_tags_for(params, :except => [:q, :page]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/active_admin/filters/custom_form_builder.rb | |
module ActiveAdmin | |
module Filters | |
class CustomFormBuilder < ::ActiveAdmin::Filters::FormBuilder | |
def input(method, options = {}) | |
options = options.dup # Allow options to be shared without being tainted by Formtastic | |
options[:as] ||= default_input_type(method, options) | |
klass = input_class(options[:as]) | |
obj = klass.new(self, template, @object, @object_name, method, options) | |
class << obj | |
include ActiveAdmin::Inputs::FilterBase | |
end | |
obj.to_html | |
end | |
def custom_input_class_name(as) | |
"#{as.to_s.camelize}Input" | |
end | |
def active_admin_input_class_name(as) | |
"ActiveAdmin::Inputs::#{custom_input_class_name(as)}" | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.clear_sidebar_sections! | |
sidebar :filters do | |
render partial: 'search' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@jsyoun0823 nice that you find it helpful!