Adding virtual attributes for a model in Administrate dashboard

43 Views Asked by At

In Administrate dashboard, I'd like to access model instance to create custom attributes with data that belongs to a different model, e.g. @instance.related_model.attribute.

ATTRIBUTE_TYPES = {
  custom_attribute: Field::SomeFieldType, @instance.related_model.attribute
}

The Administrate documentation doesn't mention anything like that. It seems that requested_resource might be accessed from the controller actions, but not the dashboard. Creating a custom field for such a basic use case seems overkill because it's not a custom field. Any help would be much appreciated.

1

There are 1 best solutions below

0
inmydelorean On BEST ANSWER

If you need a virtual attribute on your dashboard, you only need a model method. That's all, no custom fields and views, and no instances in the dashboard. So, in your model:

def custom_attribute
  # logic
end

In your dashboards/model_name_dashboard.rb:

ATTRIBUTE_TYPES = {
  custom_attribute: Field::SomeFieldType
}

Replace SomeFieldType depending on the data type you're working with, could be Boolean, Select, String, etc.