38

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

ActiveAdmin.register Promo do

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

end

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

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
4

8 回答 8

31

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

filter :"subscription_billing_plan_name" , :as => :select, :collection => BillingPlan.all.map(&:name)
于 2011-11-24T09:41:54.833 回答
29

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
于 2011-11-28T18:57:26.777 回答
13

要使用自定义过滤器,您可以创建一个范围函数并将其添加为模型中的 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 ]
于 2014-07-29T22:55:12.617 回答
9

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

# 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
于 2020-03-08T21:47:11.473 回答
7

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. 
于 2018-11-16T15:49:42.017 回答
6

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

config.clear_sidebar_sections!

sidebar :filters do
  render partial: 'search'
end

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

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

于 2012-12-08T15:47:20.317 回答
2

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

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

filter :user_id, :as => :select, :collection => User.all.map {|user| [user.email, user.id]}
于 2017-09-16T08:32:42.707 回答
0

这对我有用:

在我的模型中

  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'

于 2019-04-24T15:17:01.797 回答