繁体   English   中英

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

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

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

ActiveAdmin.register Promo do

  filter :name
  filter :address
  filter :city
  filter :state
  filter :zip

end

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

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

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

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

Active Admin 使用meta_search gem 作为其过滤器。 ORed 条件语法允许在一个查询中组合多个字段,例如

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

在 Active Admin DSL 中,这转化为

ActiveAdmin.register Promo do

  filter :name_or_address, :as => :string

end

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

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

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

然后在 users.rb 中,我可以使用我的范围作为自定义过滤器:

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

在较新版本的活动管理员中进行此类过滤的另一种方法:

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

然后在模型文件中添加以下 2 个函数

您的过滤逻辑:

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

为 Ransack 注册新过滤器

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

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

config.clear_sidebar_sections!

sidebar :filters do
  render partial: 'search'
end

然后使用构建器ActiveAdmin::FormBuilder_search部分中ActiveAdmin::FormBuilder就像它所做的那样:

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

2018 年回答。ActiveAdmin 使用 Ransack。

在模型本身上,您需要添加 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 

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

我有属于User模型的模型WithdrawalRequest

要通过用户的电子邮件过滤取款请求,需要写:

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

这对我有用:

在我的模型中

  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

在我的管理文件中

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