ruby-on-railsrubyruby-on-rails-3activeadmin

How to add custom filter to Active Admin?


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

Solution

  • 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