How to add custom filter to Active Admin?

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

    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...

    0 讨论(0)
  • 2020-12-25 11:30

    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 ]
    
    0 讨论(0)
提交回复
热议问题