How to add custom filter to Active Admin?

2020-02-21 02:15发布

问题:

Active Admin allows me to define filters that are displayed on the index page like so:

ActiveAdmin.register Promo do

  filter :name
  filter :address
  filter :city
  filter :state
  filter :zip

end

I would like to combine all the fields above into one, so that I can search for Promos that contain the search string in name or full address. My model already has a named scope that I can use:

class Promo < ActiveRecord::Base
  scope :by_name_or_full_address, lambda { |q| where('name LIKE :q OR address LIKE :q OR city LIKE :q OR state LIKE :q OR zip LIKE :q', :q => "%#{q}%") }
end

回答1:

Active admin uses metasearch. For example you can do this:

filter :"subscription_billing_plan_name" , :as => :select, :collection => BillingPlan.all.map(&:name)


回答2:

Active Admin uses the meta_search gem for its filters. ORed conditions syntax allows to combine several fields in one query, for example

Promo.metasearch(:name_or_address_contains => 'brooklyn')

In Active Admin DSL this translates to

ActiveAdmin.register Promo do

  filter :name_or_address, :as => :string

end


回答3:

To use a custom filter, you can create a scope function and add it as search_methods in the model.

For example, on my User model:

search_methods :role_eq
scope :role_eq, -> (role) { where("? LIKE ANY(roles)", role) }

Then in users.rb, I can use my scope as a custom filter:

filter :role, label: "Roles", as: :select, collection: %w[ student teacher parent ]


回答4:

I found better way of doing that. You just need to add:

config.clear_sidebar_sections!

sidebar :filters do
  render partial: 'search'
end

And then make form inside the _search partial with the builder ActiveAdmin::FormBuilder as it did in:

https://github.com/gregbell/active_admin/blob/master/lib/active_admin/filters/forms.rb

For more information how to do it, look to this gist:

https://gist.github.com/4240801

Another idea is to create class:

module ActiveAdmin
  module Inputs
    class FilterCustomStringInput < FilterStringInput
      def input_name
        "#{super}"
      end
    end
  end
end

that will be able to invoke by as: :custom_string, but I don't like that idea, because you can find soon, that you will need to create custom_select and so on...



回答5:

Answering in 2018. ActiveAdmin uses Ransack.

On model itself you need to add Ransack formatter:

ransacker :my_custom_filter, formatter: -> (category_id) {
    ids = MyModel.where(category_id: category_id).pluck(:id) # return only id-s of returned items.
    ids.present? ? ids : nil # return ids OR nil!
} do |parent| # not sure why this is needed .. but it is :)
    parent.table[:id]
end 

In ActiveAdmin file you need to specify the rule:

filter :my_custom_filter_in, as: :select, collection: -> { Category.all } # sometimes my_custom_filter_eq - depending on what you want .. Specify different "as" when you need it. 


回答6:

I have model WithdrawalRequest which belongs to User model.

For filtering withdrawal requests by user's email need write:

filter :user_id, :as => :select, :collection => User.all.map {|user| [user.email, user.id]}


回答7:

This worked for me:

In my model

  scope :active, -> { where(inactive_at: nil) }
  scope :inactive, -> { where.not(inactive_at: nil) }

  ...

  ransacker :listing_status, formatter: proc{ |status|
    ids = status == 'Active' ? active.ids : inactive.ids
    ids = ids.present? ? ids : nil
  }, splat_params: true do |parent|
    parent.table[:id]
  end

In my admin file

filter :listing_status_in, as: :select, collection: %w(Active Inactive), label: 'Listing Status'