Skip to content

Conversation

@javierjulio
Copy link
Member

@javierjulio javierjulio commented Jun 26, 2020

The display_name helper only uses the display name fallback if no matching method is defined on a model from the configured display_name_methods list. The output of the display name fallback is the model human name and the primary key. So with a given User model and a numeric primary key, you would get User #1 as an example output.

If the model implements a method from that display_name_methods list but it returns a blank value, this has a side effect where an association link is generated without text so it's not seen or clickable. I think it would help to use our display name fallback in this specific case since it is a good default, rather than be all or nothing. The display_name_methods list is a global configuration that cannot be modified per resource.

When working on this change, the test case I modified demonstrated the exact behavior I ran into. The anchor (link) HTML is generated but without a value so a user wouldn't see anything in the browser. While the test shows this is expected behavior, I think this would be an improvement for this specific scenario.

This change would not have any effect on cases where there is a custom display name because as long as it returns a present value, it will always be used. This is a small improvement to rely on a good default, our display name fallback, when there is no present value.

Let me know your thoughts and any suggestions for changes here.

@javierjulio javierjulio force-pushed the fix-display-name-with-fallback branch from e124f2c to 7b8f345 Compare June 26, 2020 18:49
@deivid-rodriguez
Copy link
Member

As discussed privately, my concern is that this introduces the inconsistency of potentially showing the value of different model attributes in the same column. So, in case it is intended that the value of the chosen display attribute is empty, then this would be a strict improvement. However, in the case where this is an unintended data problem, the current behaviour would make it easier to identify and fix, whereas the new behaviour would make a model whose name is User #1 indistinguishable from a model with empty name.

So, although I don't currently have a great idea, I'd like us to at least apply some visual hint in the case where this "extra fallback" has been applied. I'll try to think about something more concrete.

@javierjulio
Copy link
Member Author

I think a visual hint is a great idea. Some other suggestions could be to also log a warning and/or use a configuration to opt-in to this change.

@sunny
Copy link
Contributor

sunny commented Jul 12, 2020

Perhaps (User #1)?

@deivid-rodriguez
Copy link
Member

I don't think there's a "correct" solution involving changing only the wording of the name, because users should be free to name models whatever they like without causing conflicts with this, including the (User #1) name.

I mean, we could probably use some virtually impossible name like THIS MODEL HAS EMPTY NAME SO WE'RE SHOWING THIS DUMMY TEXT SO THAT YOU CAN STILL CLICK THIS LINK AND NAVIGATE TO THE MODEL PAGE. But a visual hint to explain this feels better, maybe a clickable "info" icon or something.

@sunny
Copy link
Contributor

sunny commented Jul 14, 2020

A quick fix could also be to replace blank values by a non-breaking space. This would, at least, make the link appear and be clickable. This would help show visually that the association is present but the name is empty.

E.g.:

image

This would not be ideal since the link is very small, though.

@deivid-rodriguez
Copy link
Member

Well, that's definitely better than what we have now, and it doesn't bring any ambiguity. Thoughts @javierjulio?

@javierjulio
Copy link
Member Author

Thanks for the suggestion! Hmm I don't think so because by implementing that then we are actually giving a present value of   vs just an empty space as now. I don't think others would even find the link with such a small, invisible click area. If we do that, I'd prefer to put effort towards hopefully a suitable fix.

Just to reiterate, there are two key items here. One is that the display name fallback is a good default overall but is not used in all cases. Second is that by just adding a column on a model, that does not require a value but just so happens to be in the config.display_name_methods list, causes this blank display name issue, even if the new field is not anyway related for the admin. I'm torn really because the fallback works great so to me it makes sense to utilize that somehow since in our admin usage, we are used to the default display output.

@javierjulio javierjulio force-pushed the fix-display-name-with-fallback branch from 7b8f345 to efffbc0 Compare April 22, 2021 14:04
@deivid-rodriguez
Copy link
Member

@javierjulio Sorry for dropping the ball on this, I'll have another look as soon as possible.

@javierjulio
Copy link
Member Author

@deivid-rodriguez haha I was just updating to keep it current with CI/lint changes since then, so no worries, thanks!

@javierjulio javierjulio force-pushed the fix-display-name-with-fallback branch 2 times, most recently from 55d04e7 to 214cbc3 Compare May 12, 2021 15:08
@javierjulio javierjulio force-pushed the fix-display-name-with-fallback branch from 214cbc3 to e3956c0 Compare February 4, 2022 14:39
@javierjulio javierjulio force-pushed the fix-display-name-with-fallback branch from e3956c0 to efcd5e7 Compare July 10, 2023 16:20
@javierjulio javierjulio changed the title Use the display name fallback if the display name method returns a blank value Use display name fallback if blank display name result Jul 10, 2023
@javierjulio javierjulio removed the request for review from deivid-rodriguez July 10, 2023 19:07
@javierjulio javierjulio force-pushed the fix-display-name-with-fallback branch from efcd5e7 to 6a2d841 Compare July 12, 2023 00:08
@codecov
Copy link

codecov bot commented Jul 12, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (6333cfc) 99.12% compared to head (aa5ed1d) 99.12%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #6342   +/-   ##
=======================================
  Coverage   99.12%   99.12%           
=======================================
  Files         196      196           
  Lines        4939     4940    +1     
=======================================
+ Hits         4896     4897    +1     
  Misses         43       43           
Impacted Files Coverage Δ
lib/active_admin/view_helpers/display_helper.rb 96.77% <100.00%> (+0.22%) ⬆️
lib/active_admin/views/pages/show.rb 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

This addresses an issue where if a model has a method that is one in the list of configured display_name_methods but it returns a blank value, then the display name will always come back blank. The display name fallback was only used if a model did not have one of the configured display names but it would be helpful to always rely on the display name fallback even if there is no return value otherwise this will cause side effects like this test case in its original form shown, the link had no value and so it wouldn't even appear to be clickable in a browser.
This addresses an issue where if a model has a method that is one in the list of configured display_name_methods but it returns a blank value, then the display name will always come back blank. The display name fallback was only used if a model did not have one of the configured display names but it would be helpful to always rely on the display name fallback even if there is no return value otherwise this will cause side effects like this test case in its original form shown, the link had no value and so it wouldn't even appear to be clickable in a browser.
@javierjulio javierjulio force-pushed the fix-display-name-with-fallback branch from dc8dbb8 to aa5ed1d Compare July 13, 2023 00:13
@javierjulio javierjulio merged commit 88699eb into activeadmin:master Jul 13, 2023
@javierjulio javierjulio deleted the fix-display-name-with-fallback branch July 13, 2023 00:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants