Ruby-on-rails 如何向 Active Admin 添加自定义过滤器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7983654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:16:43  来源:igfitidea点击:

How to add custom filter to Active Admin?

ruby-on-railsrubyruby-on-rails-3activeadmin

提问by dkobozev

Active Admin allows me to define filtersthat 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

采纳答案by dkobozev

Active Admin uses the meta_searchgem for its filters. ORed conditions syntax allows to combine several fields in one query, for example

Active Admin 使用meta_searchgem 作为其过滤器。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

回答by Ivan

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

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

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

回答by user3353768

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 ]

回答by Dmitry Polushkin

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 _searchpartial with the builder ActiveAdmin::FormBuilderas it did in:

然后_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...

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

回答by knagode

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. 

回答by artamonovdev

I have model WithdrawalRequestwhich belongs to Usermodel.

我有属于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]}

回答by Arman Petrosyan

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

回答by theabhisheksoni

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'

filter :listing_status_in, as: :select, collection: %w(Active Inactive), label: 'Listing Status'