How to Customize the Administrate Dashboard Search Engine
In this guide, we are going to refine the search functionality in the Administrate dashboard. Though search already exists, it isn't doing much, so we need to change that.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this guide, we are going to refine the search functionality in the Administrate dashboard. Though search already exists, it isn't doing much, so we need to change that.

Let's go to user_dashboard.rb, and make the email field searchable by updating the ATTRIBUTE_TYPES.

# app/dashboards/user_dashboard.rb

class UserDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    posts: Field::HasMany.with_options(searchable: false),
    id: Field::Number.with_options(searchable: false),
    email: Field::String.with_options(searchable: true),
    password: Field::String.with_options(searchable: false),
    sign_in_count: Field::Number.with_options(searchable: false),
    current_sign_in_at: Field::DateTime.with_options(searchable: false),
    last_sign_in_at: Field::DateTime.with_options(searchable: false),
    current_sign_in_ip: Field::String.with_options(searchable: false),
    last_sign_in_ip: Field::String.with_options(searchable: false),
    first_name: Field::String.with_options(searchable: false),
    last_name: Field::String.with_options(searchable: false),
    created_at: Field::DateTime.with_options(searchable: false),
    updated_at: Field::DateTime.with_options(searchable: false),
    type: Field::String.with_options(searchable: false),
  }.freeze

Now, let's see what happens in the browser.

large

With these changes in place the search engine is now functioning properly.

Maybe, we should not have the ID attribute visible at all on the show page, so let's remove it from SHOW_PAGE_ATTRIBUTES.

# app/dashboards/user_dashboard.rb

  SHOW_PAGE_ATTRIBUTES = [
    :posts,
    :email,
    :sign_in_count,
    :current_sign_in_at,
    :last_sign_in_at,
    :current_sign_in_ip,
    :last_sign_in_ip,
    :first_name,
    :last_name,
    :created_at,
    :updated_at,
    :type,
  ].freeze

large

Now, let's do the same with admin_user_dashboard.rb too. We'll set the search value to true only for the email attribute, and not for other attributes.

# app/dashboards/admin_user_dashboard.rb

class AdminUserDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    posts: Field::HasMany.with_options(searchable: false),
    id: Field::Number.with_options(searchable: false),
    email: Field::String.with_options(searchable: true),
    password: Field::String.with_options(searchable: false),
    sign_in_count: Field::Number.with_options(searchable: false),
    current_sign_in_at: Field::DateTime.with_options(searchable: false),
    last_sign_in_at: Field::DateTime.with_options(searchable: false),
    current_sign_in_ip: Field::String.with_options(searchable: false),
    last_sign_in_ip: Field::String.with_options(searchable: false),
    first_name: Field::String.with_options(searchable: false),
    last_name: Field::String.with_options(searchable: false),
    created_at: Field::DateTime.with_options(searchable: false),
    updated_at: Field::DateTime.with_options(searchable: false),
    type: Field::String.with_options(searchable: false),
  }.freeze

If we test it in the browser, it works properly.

Resources