Change the way polymorphic fields display links #1737
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In one of my recent projects using administrate I made a change in the
_show
of the polymorphic field. It attended my needs, and I think it's the way to go.Contextualizing: The
valid_action?
implementation receives as second argument the resource class name (constantize), and so will look for the correspondent controller and check if the action exists. In the polymorphic field, we're using this to display the attribute as a link or just plain text.The problem: In the
_show
partial we check for the field attribute, that will return just the attribute name, and polymorphic fields constantly have attribute names that don't correspond whit any controller name. Here is an example: I can have an Address model that have a polymorphic association that receives the name ofowner
, and the owner can be a Shop and User model. We have theShopsController
andUsersController
, but not anOwnersController
. As result, we'll be showing the owner as plain text only.The solution: Just inform the field data class instead of the attribute name. Doing so, using the example described above, we'll be checking for the ShopsController and UsersController instead of the OwnersController, properly showing the owner as a link.