38

Active Adminを使用すると、次のようにインデックスページに表示されるフィルターを定義できます。

ActiveAdmin.register Promo do

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

end

上記のすべてのフィールドを1つにまとめて、名前または完全な住所に検索文字列を含むプロモーションを検索できるようにします。私のモデルには、使用できる名前付きスコープがすでにあります。

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_searchgemを使用します。ORed条件構文を使用すると、たとえば、1つのクエリで複数のフィールドを組み合わせることができます。

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

それはによって呼び出すことが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 に答える