How to Add Custom Filter to Active Admin

How to add custom filter to Active Admin?

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

ActiveAdmin custom filter from model attributes on the fly

I'd be inclined to take the messy business of generating the query and delegate it to the model, using its own scope/class method. Then you just need to inform MetaSearch/Ransack (depending on your ActiveAdmin version) that it can search that scope, and you can add it as a filter.

For bonus points, you could drop the search method into a concern that you can include into any model.

app/admin/notifications.rb

filter :containing_text, as: :string, label: 'Text Search:'

app/models/notification.rb

# for MetaSearch
# search_methods :containing_text

# for Ransack
def self.ransackable_scopes(_opts)
[:containing_text]
end

# this could be dropped into a concern as-is
def self.containing_text(query)
# select text-type columns
cols = columns.select { |c| [:string, :text].include?(c.type) }

# generate query fragment
fragment = cols.map { |c| "#{ table_name }.#{ c.name } LIKE ?" }
.join(' OR ')

# execute sanitized query
where(fragment, *Array.new(cols.size, "%#{ query }%"))
end

### EDIT by OP ###

I had never used concerns before so eventually worked out how to get it working:

1) Add the concern path to your application.rb

config/application.rb

class Application < Rails::Application
config.autoload_paths += %W(#{config.root}/app/models/concerns)
end

2) Add the include to the Searchable concern and method call into the notifcation model

app/models/notification.rb

include Searchable
search_methods :containing_text

3) Created the concern:

/app/models/concerns/searchable.rb

module Searchable
extend ActiveSupport::Concern
module ClassMethods
def self.containing_text(query)
# select text-type columns (string and text)
cols = columns.select { |c| [:string, :text].include?(c.type) }
# generate query fragment
fragment = cols.map { |c| "#{ table_name }.#{ c.name } LIKE ?" }

.join(' OR ')
# execute sanitized query
where(fragment, *Array.new(cols.size, "%#{ query }%"))
end
end
end

That then seemed to work. I probably should rename the searchable into something better but it works.

add a custom Filter Active Admin Rails

OK so here's what I finally did; It solves the problem perfectly.

I added has_one :restaurant, through: :meal in model/order.rb and added the filter simply this way: filter :restaurant.

This alowed me to add the restaurant filter.

To get the restaurant column, here's the code:

in admin/order.rb

index do
column("Order", :sortable => :id) {|order| link_to "##{order.id} ", admin_order_path(order) }
column("pick up time", :pick_up_time)
column("status", :status)
column("User", :user_id ) {|order| link_to "#{order.user.first_name} #{order.user.last_name}", admin_user_path(order.user) }
column("Meal", :meal)
column("Restaurant", :restaurant) // This line adds the restaurant column
column("Created at", :created_at)
column("Updated at", :updated_at)
end

Custom filter to active admin to extract multiple rows on Index page

Well the ransack code was fine, I just needed to change the filter on the index page , any of these would work

  filter :custome_search, label: 'Custom Data Search:', filters: ['in']

or

  filter :custome_search_in, label: 'Custom Data Search:', as: :string


Related Topics



Leave a reply



Submit