简体   繁体   English

如何向 Active Admin 添加自定义过滤器?

[英]How to add custom filter to Active Admin?

Active Admin allows me to define filters that are displayed on the index page like so: Active Admin 允许我定义显示在索引页面上的过滤器,如下所示:

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

Active admin uses metasearch.主动管理员使用元搜索。 For example you can do this:例如,您可以这样做:

filter :"subscription_billing_plan_name" , :as => :select, :collection => BillingPlan.all.map(&:name)

Active Admin uses the meta_search gem for its filters. Active Admin 使用meta_search gem 作为其过滤器。 ORed conditions syntax allows to combine several fields in one query, for example ORed 条件语法允许在一个查询中组合多个字段,例如

Promo.metasearch(:name_or_address_contains => 'brooklyn')

In Active Admin DSL this translates to在 Active Admin DSL 中,这转化为

ActiveAdmin.register Promo do

  filter :name_or_address, :as => :string

end

To use a custom filter, you can create a scope function and add it as search_methods in the model.要使用自定义过滤器,您可以创建一个范围函数并将其作为 search_methods 添加到模型中。

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:然后在 users.rb 中,我可以使用我的范围作为自定义过滤器:

filter :role, label: "Roles", as: :select, collection: %w[ student teacher parent ]

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然后在模型文件中添加以下 2 个函数

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为 Ransack 注册新过滤器

def self.ransackable_scopes(_auth_object = nil)
  %i(my_custom_filter_eq)
end

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:然后使用构建器ActiveAdmin::FormBuilder_search部分中ActiveAdmin::FormBuilder就像它所做的那样:

https://github.com/gregbell/active_admin/blob/master/lib/active_admin/filters/forms.rb 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 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...这将能够通过as: :custom_string调用,但我不喜欢这个想法,因为你很快就会发现,你需要创建 custom_select 等等......

Answering in 2018. ActiveAdmin uses Ransack. 2018 年回答。ActiveAdmin 使用 Ransack。

On model itself you need to add Ransack formatter:在模型本身上,您需要添加 Ransack 格式化程序:

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:在 ActiveAdmin 文件中,您需要指定规则:

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. 

I have model WithdrawalRequest which belongs to User model.我有属于User模型的模型WithdrawalRequest

For filtering withdrawal requests by user's email need write:要通过用户的电子邮件过滤取款请求,需要写:

filter :user_id, :as => :select, :collection => User.all.map {|user| [user.email, user.id]}

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM