如何向Active Admin添加自定义filter?

Active Admin允许我定义索引页面上显示的filter ,如下所示:

ActiveAdmin.register Promo do filter :name filter :address filter :city filter :state filter :zip end 

我想将上面的所有字段合并为一个,这样我就可以搜索包含名称或完整地址中的搜索字符串的Promos。 我的模型已经有一个我可以使用的命名范围:

 class Promo  "%#{q}%") } end 

活动管理员使用元搜索。 例如,你可以这样做:

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

Active Admin将meta_search gem用于其filter。 例如,ORed条件语法允许在一个查询中组合多个字段

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

在Active Admin DSL中,这转换为

 ActiveAdmin.register Promo do filter :name_or_address, :as => :string end 

要使用自定义filter,您可以创建范围函数并将其作为search_methods添加到模型中。

例如,在我的用户模型上:

 search_methods :role_eq scope :role_eq, -> (role) { where("? LIKE ANY(roles)", role) } 

然后在users.rb中,我可以将我的范围用作自定义filter:

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

我找到了更好的方法。 你只需要添加:

 config.clear_sidebar_sections! sidebar :filters do render partial: 'search' end 

然后使用构建器ActiveAdmin::FormBuilder_search partial中创建表单,如下所示:

https://github.com/gregbell/active_admin/blob/master/lib/active_admin/filters/forms.rb

有关如何操作的更多信息,请查看以下要点:

https://gist.github.com/4240801

另一个想法是创建类:

 module ActiveAdmin module Inputs class FilterCustomStringInput < FilterStringInput def input_name "#{super}" end end end end 

这将能够通过as: :custom_string调用,但我不喜欢这个想法,因为你很快就会发现,你需要创建custom_select等等......

我有模型WithdrawalRequest属于用户模型。

要按用户的电子邮件过滤提款请求,请写入:

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