public
Last active

active_admin custom filter

  • Download Gist
active_admin.rb
Ruby
1 2 3 4 5 6 7
# config/initializers/active_admin.rb
 
require 'active_admin_custom_filter'
 
ActiveAdmin.setup do |config|
# ...
end
active_admin_custom_filter.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 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
resource.rb
Ruby
1 2 3 4 5
# app/admin/resource.rb
 
ActiveAdmin.register Resource do
filter :first_name_or_last_name_contains, :as => :custom, :label => 'Name'
end

Very nice , simple and helpfull . Thank you ;]

damn, why it is not in wiki or README !

it is really important piece of code

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

@kwojtaszek comment as valid code

# From https://gist.github.com/rdj/1057991
# From `filter :foo, as: :custom` for ActiveAdmin
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

FYI: I doesn't work at least on ActiveAdmin 1.0.0.pre

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.