简体   繁体   中英

ActiveAdmin - how can I strip the filter / search terms

In ActiveAdmin, how can I strip the search term? (remove whitespace around " steve@example.com ")

I am trying to apply this to an email field.

ActiveAdmin.register User do
  ...
  filter :email, # <- I want to strip! this search term
  filter :name
  filter :address_phone
  ...

Thanks

You can create your own ransacker in this simple case:

in the User ActiveRecord model:

ransacker :stripped_email, formatter: proc { |v| v.strip } do |parent|
  parent.table[:email]
end

in your User ActiveAdmin resource:

filter :stripped_email_cont

or filter :stripped_email_eq

https://github.com/activerecord-hackery/ransack/wiki/Basic-Searching#eq-equals

你可以试试这个

  filter :email, :as => :check_boxes, :collection => proc { User.all.collect(&:email).map(&:strip) }

Monkey patch will do it: something like this will work for "cont" queries

  module ActiveAdmin
    module ResourceController::DataAccess
      def apply_filtering(chain)
        filter_params = params[:q] || {}
        filter_params.update(filter_params) do |key, value|
          key.include?("cont") ? value.strip : value
        end

        @search = chain.ransack(filter_params || {})
        @search.result
      end
    end
  end

With rails 5.2 and active_admin 2.2.0

File to config/initializers/active_filter.rb

module ActiveAdmin
  module ResourceController::DataAccess
    def apply_filtering(chain)
      filter_params = params[:q] || {}

      filter_params.each do |key, value|
        filter_params[key] = value.strip if value.class == String
      end

      @search = chain.ransack(filter_params || {})
      @search.result
    end
  end
end

Restart you server...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM