@codevise/activeadmin-searchable_select

中文文档
1.9.0发布于 2026/01/02MITCommonJS近 30 天下载 1万

Use searchable selects based on Select2 in Active Admin forms and filters.

原始使用文档
文档目录7 节
  1. Installation
  2. Usage
  3. Making Select Boxes Searchable
  4. Fetching Options via Ajax
  5. Passing options to Select2
  6. Development
  7. Acknowledgements

ActiveAdmin Searchable Select

Gem Version NPM Version npm Build Status

Searchable select boxes (via Select2) for ActiveAdmin forms and filters. Extends the ActiveAdmin resource DSL to allow defining JSON endpoints to fetch options from asynchronously.

Installation

Add activeadmin-searchable_select to your Gemfile:

   gem 'activeadmin-searchable_select'
Using assets via Sprockets

Import stylesheets and require javascripts:

// active_admin.css.scss
@import "active_admin/searchable_select";
// active_admin.js
//= require active_admin/searchable_select
Using assets via Webpacker (or any other assets bundler) as a NPM module (Yarn package)

Execute:

$ npm i @codevise/activeadmin-searchable_select

Or

$ yarn add @codevise/activeadmin-searchable_select

Or add manually to package.json:

"dependencies": {
  "@codevise/activeadmin-searchable_select": "1.6.0"
}

and execute:

$ yarn

Add the following line into app/javascript/active_admin.js:

import '@codevise/activeadmin-searchable_select';

Add the following line into app/javascript/stylesheets/active_admin.scss:

@import '@codevise/activeadmin-searchable_select';

Usage

Making Select Boxes Searchable

To add search functionality to a select box, use the :searchable_select input type:

   ActiveAdmin.register Product do
     form do |f|
       f.input(:category, as: :searchable_select)
     end
   end

This also works for filters:

   ActiveAdmin.register Product do
     filter(:category, as: :searchable_select)
   end

By default, you can only select one at a time for a filter. You can specify a multi-select with:

   ActiveAdmin.register Product do
     filter(:category, as: :searchable_select, multiple: true)
   end

Fetching Options via Ajax

For large collections, rendering the whole set of options can be to expensive. Use the ajax option to fetch a set of matching options once the user begins to type:


   ActiveAdmin.register Product do
     filter(:category,
            as: :searchable_select,
            ajax: true)
   end

If the input attribute corresponds to an ActiveAdmin resource, it is expected to provide the JSON endpoint that provides the options. Use the searchable_select_options method to define the required collection action:

   ActiveAdmin.register Category do
     searchable_select_options(scope: Category.all,
                               text_attribute: :name)
   end

By default, scope needs to be a Ransack enabled ActiveRecord collection proxy determining which options are available. The attribute given by text_attribute will be used to get a display name for each record. Via Ransack, it is also used to filter by search term. Limiting result set size is handled automatically.

You can customize the display text:

   ActiveAdmin.register Category do
     searchable_select_options(scope: Category.all,
                               text_attribute: :name,
                               display_text: ->(record) { "Category: #{record.name}" } )
   end

Note that text_attribute is still required to perform filtering via Ransack. You can pass the filter option, to specify your own filtering strategy:

   ActiveAdmin.register Category do
     searchable_select_options(scope: Category.all,
                               text_attribute: :name,
                               filter: lambda |term, scope|
                                 scope.ransack(name_cont_all: term.split(' ')).result
                               end)
   end

scope can also be a lambda which is evaluated in the context of the collection action defined by the helper:

   ActiveAdmin.register Category do
     searchable_select_options(scope: -> { Category.allowed_for(current_user) },
                               text_attribute: :name)
   end

If the input attribute is set on the form's object, ajax based searchable selects will automatically render a single option to ensure the selected item is displayed correctly even before options have been loaded asynchronously.

Specifying the Options Endpoint Resource

If the resource that provides the options endpoint cannot be guessed based on the input attribute name, you can pass an object with a resource key as ajax option:

   ActiveAdmin.register Product do
     form do |f|
       f.input(:additional_category,
               as: :searchable_select,
               ajax: { resource: Category })
     end
   end

Multiple Options Endpoints per Resource

A single ActiveAdmin resource can define multiple options endpoints:

   ActiveAdmin.register Category do
     searchable_select_options(name: :favorites,
                               scope: Category.favorites,
                               text_attribute: :name)

     searchable_select_options(name: :recent,
                               scope: Category.recent,
                               text_attribute: :name)
   end

To specify which collection to use, pass an object with a collection_name key as ajax option:

   ActiveAdmin.register Product do
     form do |f|
        f.input(:category,
                as: :searchable_select,
                ajax: { collection_name: :favorites })
     end
   end

Querying Multiple Attributes

ActiveAdmin Searchable Select querying is performed by Ransack. As such, you can build your query in a way that it can query multiple attributes at once.

   ActiveAdmin.register User do
     searchable_select_options(scope: User.all,
                               text_attribute: :username,
                               filter: lambda do |term, scope|
                                 scope.ransack(email_or_username_cont: term).result
                               end)
   end

In this example, the all scope will query email OR username.

You can add the additional payload as dsl option:

   ActiveAdmin.register Category do
     searchable_select_options(scope: Category.all,
                               text_attribute: :name,
                               additional_payload: ->(record) { { foo: record.bar } } )
   end

response example which uses additional_payload:

{
  "results": [{ "id": "1", "text": "Bicycles", "foo": "Bar" }],
  "pagination": { "more": "false" }
}

Passing Parameters

You can pass additional parameters to the options endpoint:

   ActiveAdmin.register Product do
     form do |f|
        f.input(:category,
                as: :searchable_select,
                ajax: {
                  params: {
                    some: 'value'
                  }
                })
     end
   end

The lambda passed as scope can receive those parameters as first argument:

   ActiveAdmin.register Category do
     searchable_select_options(scope: lambda do |params|
                                 Category.find_all_by_some(params[:some])
                               end,
                               text_attribute: :name)
   end

Path options for nested resources

Example for the following setup:

# Models
class OptionType < ActiveRecord::Base; end

class OptionValue < ActiveRecord::Base
  belongs_to :option_type
end

class Product < ActiveRecord::Base
  belongs_to :option_type
  has_many :variants
end

class Variant < ActiveRecord::Base
  belongs_to :product
  belongs_to :option_value
end

# ActiveAdmin
ActiveAdmin.register(OptionType)

ActiveAdmin.register(Product)

ActiveAdmin.register(OptionValue) do
  belongs_to :option_type
  searchable_select_options(scope: lambda do |params|
                                     OptionValue.where(
                                       option_type_id: params[:option_type_id]
                                     )
                                   end,
                            text_attribute: :value)
end

It is possible to pass path parameters for correctly generating URLs for nested resources fetching via path_params

ActiveAdmin.register(Variant) do
  belongs_to :product

  form do |f|
    ...
    f.input(:option_value,
            as: :searchable_select,
            ajax: {
              resource: OptionValue,
              path_params: {
                option_type_id: f.object.product.option_type_id
              }
            })
    ...
  end
end

This will generate the path for fetching as all_options_admin_option_type_option_values(option_type_id: f.object.product.option_type_id) (e.g. /admin/option_types/2/option_values/all_options)

Inlining Ajax Options in Feature Tests

When writing UI driven feature specs (i.e. with Capybara), asynchronous loading of select options can increase test complexity. activeadmin-searchable_select provides an option to render all available options just like a normal select input while still exercsing the same code paths including scope and text_attribute handling.

For example with RSpec/Capybara, simply set inline_ajax_options true for feature specs:

  RSpec.configure do |config|
    config.before(:each) do |example|
      ActiveAdmin::SearchableSelect.inline_ajax_options = (example.metadata[:type] == :feature)
    end
  end

Passing options to Select2

It is possible to pass and define configuration options to Select2 via data-attributes using nested (subkey) options.

Attributes need to be added to the input_html option in the form input. For example you can tell Select2 how long to wait after a user has stopped typing before sending the request:

   ...
   f.input(:category,
           as: :searchable_select,
           ajax: true,
           input_html: {
             data: {
               'ajax--delay' => 500
             }
           })
   ...

Development

To run the tests install bundled gems and invoke RSpec:

$ bundle
$ bundle exec rspec

The test suite can be run against different versions of Rails and Active Admin (see Appraisals file):

$ appraisal install
$ appraisal rspec

Please make sure changes conform with the styleguide:

$ bundle exec rubocop

Acknowledgements

Based on mfairburn/activeadmin-select2.

版本与发布记录

@codevise/activeadmin-searchable_select 共发布 5 个版本,首次发布于 2022/06/07;过去 12 个月发布了 1 个版本。

版本发布日期距今
1.9.0latest2026/01/0210 个月前
1.8.02023/08/173 年前
1.7.02023/05/033 年前
1.6.12022/06/074 年前
1.6.02022/06/074 年前

关于 @codevise/activeadmin-searchable_select 的常见问题

@codevise/activeadmin-searchable_select 是什么?

Use searchable selects based on Select2 in Active Admin forms and filters.

如何安装 @codevise/activeadmin-searchable_select?

在项目目录运行 npm install @codevise/activeadmin-searchable_select,或使用 pnpm add / yarn add / bun add。

@codevise/activeadmin-searchable_select 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/codevise__activeadmin-searchable_select 是否存在。

@codevise/activeadmin-searchable_select 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@codevise/activeadmin-searchable_select 可以商用吗?

@codevise/activeadmin-searchable_select 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@codevise/activeadmin-searchable_select 的下载量有多少?

根据 npm 官方下载统计,@codevise/activeadmin-searchable_select 在 2026/10/10 – 2026/11/08 这 30 天内被下载 10,357 次,日均约 345 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@codevise/activeadmin-searchable_select 还在维护吗?

最新版本 1.9.0 发布于 2026/01/02(10 个月前);至今共发布 5 个版本,其中过去 12 个月发布了 1 个;首次发布于 2022/06/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。