Add this line to your application's Gemfile:
gem 'active_admin_search'
And then execute:
$ bundle install
ActiveAdmin.register Author do
active_admin_search! json_term_key: :custom_search_key
end
model definition
class Author
scope :custom_search_key, ->(term) { where('name = ?', term) }
end
example:
ActiveAdmin.register Author do
active_admin_search! term_key_rename: :custom_term, highlight: :custom_term
end
localhost:3000/authors/search?term=text
model definition
class Author
scope :custom_term, ->(term) { where('name = ?', term) }
end
using this setting, regardless of which variable is used in the ajax request, the custom_term method will be called anyway
active_admin_search! skip_pagination: true
If you want to define a specific limit you can use limit
key defined after skip_pagination
or define as in example below
active_admin_search! limit: 1000
By default, active_admin_search gem response your records with two key [{ "value":"1", "text":"Ukraine"" }]
value we get from record id, and text we get from special models method named: display_name,
but you can override these behaviors using key display_method
.
You can also override the value itself using the value_method
setting.
Example:
active_admin_search! display_method: :display_record
supported value: Hash, Array of Hashes, Lambda
active_admin_search! additional_payload: [:deleted_at, :other_method]
active_admin_search! additional_payload: :deleted_at
active_admin_search! additional_payload: ->(record) {{
deleted_date: record.deleted_at,
custom_field_name: record.display_name
}}
example:
active_admin_search! default_scope: :not_deleted
active_admin_search! default_scope: [:not_deleted, :tagged]
then you can override it if needed using url params: skip_default_scopes
example:
localhost:3000/posts/search?term=text&skip_default_scopes=true
more that you can define scope or list of scopes from url using scope variable example
localhost:3000/authors/search?term=string&scope=taged,not_deleted
You can perform a search record by id even if your scope is searching by a different field.
localhost:3000/authors/search?term=string
then
SELECT "authors".* FROM "authors" WHERE "authors"."name" LIKE '%string%'
localhost:3000/authors/search?term=id:443
then
SELECT "authors".* FROM "authors" WHERE "authors"."id" = 443
This works because we specify a prefix named id: at the beginning of the search term
You can highlight your response to easy perform search
example below to enable this feature
active_admin_search! highlight: :term
To solve N+1 problem you can use a special setting named includes example for Article:
active_admin_search! includes: [:category, :tag]
If you want override default order (id: :desc) you can use setting order_clause like examples below:
active_admin_search! order_clause: :deleted_at
then
SELECT "authors".* FROM "authors" ORDER BY "authors"."deleted_at" ASC
active_admin_search! order_clause: { deleted_at: :desc }
then
SELECT "authors".* FROM "authors" ORDER BY "authors"."deleted_at" DESC
active_admin_search!
localhost:3000/article/search?term=text&q[author_id_eq]=1
then
SELECT "articles".* FROM "articles" WHERE "articles"."author_id" = 1