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

发布于 2024-12-13 08:01:05 字数 554 浏览 2 评论 0原文

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

Active Admin allows me to define filters that are displayed on the index page like so:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

深者入戏 2024-12-20 08:01:05

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

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

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

filter :"subscription_billing_plan_name" , :as => :select, :collection => BillingPlan.all.map(&:name)
一杆小烟枪 2024-12-20 08:01:05

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

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

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

In Active Admin DSL this translates to

ActiveAdmin.register Promo do

  filter :name_or_address, :as => :string

end
何以心动 2024-12-20 08:01:05

在较新版本的 ActiveAdmin 中执行此类过滤的另一种方法:

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

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

您的过滤逻辑:

def self.my_custom_filter_eq(value)
  # or probably a more complex query that uses the value inputted by the admin user
  where(column_1: value)
end

为 Ransack 注册新过滤器:

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

Another way of doing such filtering in newer version of ActiveAdmin:

# 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

Your filtering logic:

def self.my_custom_filter_eq(value)
  # or probably a more complex query that uses the value inputted by the admin user
  where(column_1: value)
end

Registering new filter for Ransack :

def self.ransackable_scopes(_auth_object = nil)
  %i(my_custom_filter_eq)
end
渡你暖光 2024-12-20 08:01:05

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

例如,在我的 User 模型上:

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 ]

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 ]
世界和平 2024-12-20 08:01:05

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. 

Answering in 2018. ActiveAdmin uses Ransack.

On model itself you need to add Ransack formatter:

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:

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. 
自在安然 2024-12-20 08:01:05

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

config.clear_sidebar_sections!

sidebar :filters do
  render partial: 'search'
end

然后使用构建器 ActiveAdmin::FormBuilder_search 部分中制作表单,就像它在:

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

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

遮了一弯 2024-12-20 08:01:05

我有属于 User 模型的模型 WithdrawalRequest

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

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

I have model WithdrawalRequest which belongs to User model.

For filtering withdrawal requests by user's email need write:

filter :user_id, :as => :select, :collection => User.all.map {|user| [user.email, user.id]}
2024-12-20 08:01:05

这对我有用:

在我的模型中

  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'

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'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文