Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
active_admin custom filter
# config/initializers/active_admin.rb
require 'active_admin_custom_filter'
ActiveAdmin.setup do |config|
# ...
end
# lib/active_admin_custom_filter.rb
module ActiveAdmin
class FilterFormBuilder
include ::ActionView::Helpers::OutputSafetyHelper
def filter_custom_input( method, options = {} )
field_name = method
safe_join(
[
label( field_name, I18n.t( 'active_admin.search_field', :field => options[:label] ) ),
text_field( field_name )
],
"\n"
)
end
end
end
# app/admin/resource.rb
ActiveAdmin.register Resource do
filter :first_name_or_last_name_contains, :as => :custom, :label => 'Name'
end
@panSarin

This comment has been minimized.

Copy link

@panSarin panSarin commented Jan 12, 2012

Very nice , simple and helpfull . Thank you ;]

@piotrmasior

This comment has been minimized.

Copy link

@piotrmasior piotrmasior commented May 15, 2012

damn, why it is not in wiki or README !

it is really important piece of code

@americodls

This comment has been minimized.

Copy link

@americodls americodls commented May 25, 2012

tks for sharing!

@kwojtaszek

This comment has been minimized.

Copy link

@kwojtaszek kwojtaszek commented Oct 16, 2012

lib/active_admin_custom_filter.rb

module ActiveAdmin

module Inputs
class FilterCustomInput < ::Formtastic::Inputs::StringInput
include FilterBase

  def to_html
    input_wrapping do
      label_html <<
      builder.text_field(input_name, input_html_options)
    end
  end

  def label_text
    I18n.t('active_admin.search_field', :field => super)
  end

  def input_name
    "#{super}"
  end
end

end

end

it works better with newest activeadmin

@xhero

This comment has been minimized.

Copy link

@xhero xhero commented Sep 14, 2016

The class seems to have changed in 1.0.0.pre. This works for me.

module ActiveAdmin
  module Inputs
    module Filters
      class CustomInput < ::Formtastic::Inputs::StringInput
        include Base
        include Base::SearchMethodSelect

        filter :contains, :equals, :starts_with, :ends_with

        def to_html
          input_wrapping do
            label_html << builder.text_field(input_name, input_html_options)
          end
        end

        def label_text
          I18n.t('active_admin.search_field', field: super)
        end

        def input_name
          "#{super}"
        end
      end
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment