Render custom partials for a Field without overriding the default ones
What would you like to be able to do? Can you provide some examples?
As a developer I can render custom partials for a
Field
without overriding its default ones
For example, I have a dashboard with three Field::HasMany
attributes. For two of them I want to render the default partials but for the third one I want to render custom show
and index
pages.
How could we go about implementing that?
Introducing a new option (partials
or so) to allow developers to provide path(s) to custom(s) partials(s) to be used when rendering the field in the context of the dashboard. If no custom partial is given for a page then the default one is used.
i.e
- Dashboard
# app/dashboards/book_dashboard.rb
ATTRIBUTE_TYPES = {
pages: Field::HasMany,
authors: Field::HasMany.with_options(partials: { index: '/path/to/custom/partial'}),
publishers: Field::HasMany.with_options(partials: { show: '/path/to/custom/partial'})
}
- Aministrate Base field
# administrate/lib/administrate/field/base.rb
def to_partial_path
options.dig(:partials, page) || "/fields/#{self.class.field_type}/#{page}"
end
Can you think of other approaches to the problem?
Edit: https://github.com/thoughtbot/administrate/issues/2290#issuecomment-1327363377
At the moment I can't think of another approach to be taken from within Administrate
but I have been doing the following to cover the described use case:
Create a custom field type (without any custom behavior), that inherit from the respective administrate field type. Use the custom field in the dashboard where you want to have custom partials rendered.
For example:
def HasManyExtendedView < Field::HasMany; end
def HasManySimpleView < Field::HashMany; end
This approach doesn't scale and results in having a lot of unnecessary files/code (a bunch of custom fields without custom behaviour)