How to add custom filter to Active Admin?

后端 未结 8 2101
眼角桃花
眼角桃花 2020-12-25 11:04

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

ActiveAdmin.register Promo do

  filter :name
  filter :address
  filt         


        
8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-25 11:11

    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. 
    

提交回复
热议问题