How to add custom filter to Active Admin?

后端 未结 8 2073
眼角桃花
眼角桃花 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:07

    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]}
    
    0 讨论(0)
  • 2020-12-25 11:07

    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'

    0 讨论(0)
  • 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. 
    
    0 讨论(0)
  • 2020-12-25 11:17

    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
    
    0 讨论(0)
  • 2020-12-25 11:18

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

    filter :"subscription_billing_plan_name" , :as => :select, :collection => BillingPlan.all.map(&:name)
    
    0 讨论(0)
  • 2020-12-25 11:25

    Another way of doing such filtering in newer version of active admin:

    # app/admin/my_model.rb
    filter :my_custom_filter,
    as: :numeric,
    label: 'Custom Filter',
    filters: [:eq]
    

    Then add following 2 functions in your model file

    Your filtering logic:

    def self.my_custom_filter_eq(value)
      where(column_1: value) # or probably a more complex query that uses the value inputted by the admin user
    end
    

    Registering new filter for Ransack

    def self.ransackable_scopes(_auth_object = nil)
      %i(my_custom_filter_eq)
    end
    
    0 讨论(0)
提交回复
热议问题