# Customizing Dashboards

In order to customize which attributes get displayed for each resource,
edit the dashboard file generated by the installation generator.

By default, the file will look something like this:

```ruby
require "administrate/dashboard/base"

class CustomerDashboard < Administrate::Dashboard::Base
  ATTRIBUTE_TYPES = {
    id: Field::Number,
    name: Field::String,
    email: Field::String,
    created_at: Field::DateTime,
    updated_at: Field::DateTime,
    orders: Field::HasMany,
  }

  COLLECTION_ATTRIBUTES = [
    :id,
    :name,
    :email,
    :created_at,
    :updated_at,
    :orders,
  ]

  SHOW_PAGE_ATTRIBUTES = [
    :id,
    :name,
    :email,
    :created_at,
    :updated_at,
    :orders,
  ]

  FORM_ATTRIBUTES = [
    :name,
    :email,
    :orders,
  ]
end
```

To change which attributes appear on each of the `index`, `show`, and `edit`
pages, add or remove attributes to each constant array.

Finally, the `ATTRIBUTE_TYPES` method defines how each attribute is displayed
throughout the dashboard. There are a number of `Field` classes that you can
specify, including:

- `Field::BelongsTo`
- `Field::Boolean`
- `Field::DateTime`
- `Field::Email`
- `Field::HasMany`
- `Field::HasOne`
- `Field::Number`
- `Field::Polymorphic`
- `Field::Select`
- `Field::String`
- `Field::Text`
- `Field::Password`

## Customizing Fields

### Setting Options

Each of the `Field` types take a different set of options,
which are specified through the `.with_options` class method:

**Field::BelongsTo**

`:order` - Specifies the order of the dropdown menu, can be ordered by more
than one column. e.g.: `"name, email DESC"`.

`:primary_key` - Specifies object's primary_key. Defaults to `:id`.

`:foreign_key` - Specifies the name of the foreign key directly.
Defaults to `:#{attribute}_id`.

`:scope` - Specifies a custom scope inside a callable. Useful for preloading.
Example: `.with_options(scope: -> { MyModel.includes(:rel).limit(5) })`

`:class_name` - Specifies the name of the associated class.
Defaults to `:#{attribute}.to_s.singularize.camelcase`.

`:searchable` - Specify if the attribute should be considered when searching.
Default is `false`.

`searchable_field` - Specify which column to use on the search, only applies
if `searchable` is `true`

For example:

```ruby
  country: Field::BelongsTo(
    searchable: true,
    seachable_field: 'name',
  )
```

with this, you will be able to search through the column `name` from the
association `belongs_to :country`, from your model.

**Field::HasMany**

`:limit` - Set the number of resources to display in the show view. Default is
`5`.

`:sort_by` - What to sort the association by in the show view.

`:direction` - What direction the sort should be in, `:asc` (default) or `:desc`.

`:primary_key` - Specifies object's primary_key. Defaults to `:id`.

`:foreign_key` - Specifies the name of the foreign key directly. Defaults to `:#{attribute}_id`

`:class_name` - Specifies the name of the associated class.
Defaults to `:#{attribute}.to_s.singularize.camelcase`.

**Field::HasOne**

`:class_name` - Specifies the name of the associated class.
Defaults to `:#{attribute}.to_s.singularize.camelcase`.

`:searchable` - Specify if the attribute should be considered when searching.
Default is `false`.

`searchable_field` - Specify which column to use on the search, only applies if
`searchable` is `true`

For example:

```ruby
  cities: Field::HasMany(
    searchable: true,
    seachable_field: 'name',
  )
```

with this, you will be able to search through the column `name` from the
association `has_many :cities`, from your model.

**Field::Number**

`:searchable` - Specify if the attribute should be considered when searching.
Note that currently number fields are searched like text, which may yield
more results than expected. Default is `false`.

`:decimals` - Set the number of decimals to display. Defaults to `0`.

`:prefix` - Prefixes the number with a string. Defaults to `""`.

`:suffix` - Suffixes the number with a string. Defaults to `""`.

For example, you might use the following to display U.S. currency:

```ruby
  unit_price: Field::Number.with_options(
    prefix: "$",
    decimals: 2,
  )
```

Or, to display a distance in kilometers:

```ruby
  unit_price: Field::Number.with_options(
    suffix: " km",
    decimals: 2,
  )
```

**Field::Polymorphic**

`:classes` - Specify a list of classes whose objects will be used to populate select boxes for editing this polymorphic field.
Default is `[]`.

`:order` - What to sort the association by in the form select.
Default is `nil`.

**Field::DateTime**

`:format` - Specify what format, using `strftime` you would like `DateTime`
objects to display as.

`:timezone` - Specify which timezone `Date` and `DateTime` objects are based
in.

**Field::Select**

`:collection` - Specify the array or range to select from.  Defaults to `[]`.

`:searchable` - Specify if the attribute should be considered when searching.
Default is `true`.

**Field::String**

`:searchable` - Specify if the attribute should be considered when searching.
Default is `true`.

`:truncate` - Set the number of characters to display in the index view.
Defaults to `50`.

**Field::Text**

`:searchable` - Specify if the attribute should be considered when searching.
Default is `false`.

`:truncate` - Set the number of characters to display in the index view.
Defaults to `50`.

**Field::Password**

`:searchable` - Specify if the attribute should be considered when searching.
Default is `false`.

`:truncate` - Set the number of characters to display in the views.
Defaults to `50`.

`:character` - Set the replace character.
Defaults to `•`.

### Defining Labels

To change the user-facing label for an attribute,
define a custom I18n translation:

```yaml
en:
  helpers:
    label:
      customer:
        name: Full Name
```


To change the labels used for resources in dashboard collections.
Assume you have a users dashboard and you want to change "User #1" to "Testy
McTesterson", the user's name.

Add this method to the dashboard for Users.
Use whatever attribute or method you like.
Example for *user*:

````ruby
def display_resource(user)
  user.name
end
````

[define your own]: /adding_custom_field_types

To change the dashboard name in sidebar menu, sub-header and search string use default ActiveRecord i18n translations for models:

```yaml
en:
  activerecord:
    models:
      customer:
        one: Happy Customer
        others: Happy Customers
```

## Customizing Actions

To enable or disable certain actions you could override `valid_action?` method in your dashboard controller like this:

```ruby
# disable 'edit' and 'destroy' links
def valid_action?(name, resource = resource_class)
  %w[edit destroy].exclude?(name.to_s) && super
end
```

Action is one of `new`, `edit`, `show`, `destroy`.