Open ammancilla opened 1 year ago
That's a valid point. I had a look and my current impression is that we may want to change the belongs_to
templates to render the value even if it's not present. Currently this is the code for the show
template:
Perhaps it should change to the following:
<% if field.data && accessible_action?(field.data, :show) %>
<%= link_to(
field.display_associated_resource,
[namespace, field.data],
) %>
<% else %>
<%= field.display_associated_resource %>
<% end %>
The problem though is that this would introduce an incompatibility with existing code. Any display_resource
that is not looking out for nils could raise an exception.
I wonder if there's a way to handle this in a way that offers a good transition path. Or perhaps we should do it differently, in one of the ways that you propose.
Hi @pablobm,
A combination of:
fallback_display_name
(or similar) for Field::BelongsTo
.display_associated_resource
in Field::BelongsTo
to use the fallback.sounds like a good way to tackle the described use case. Only changing the views not only introduces the incompatibility that you mentioned in your comment
The problem though is that this would introduce an incompatibility with existing code. Any display_resource that is not looking out for nils could raise an exception.
but also has the (minor) secondary effect of having to care for handling nils also for the optional belongs to associations for which the current behavior is totally fine.
In general, passing the fallback as an option is not only compatible with existing but it is also more flexible and requires less changes around dashboards.
Let me know what you think.
Disclaimer: the option described above is the one I ended up following for now.
Do you want to prepare a PR to prototype your proposal? No need for specs or docs just yet. Only to see what it would look like and discuss.
What would you like to be able to do? Can you provide some examples?
Recently, I ran into the following use case:
For example, in the
index
page of aBook
that optionally belongs to anAuthor
, displayAnonymous
if there isn't an associated author.Is there already a way to achieve this with
Field::BelongsTo
without having to generate and modify the default views of the field/resource?How could we go about implementing that?
If possible, having an option (
fallback_display_name
or similar) that can be passed toField::BelongsTo
to be displayed in the views when the association is not present.Having a custom field
OptionalBelongsTo < Field::BelongsTo
that supports the mentioned behavior.