Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the way polymorphic fields display links #1737

Merged
merged 1 commit into from
Aug 13, 2020
Merged

Change the way polymorphic fields display links #1737

merged 1 commit into from
Aug 13, 2020

Conversation

rwehresmann
Copy link
Contributor

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 of owner, and the owner can be a Shop and User model. We have the ShopsController and UsersController, but not an OwnersController. 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.

There is a problem in the way we're calling `valid_action?` in the polymorphic field.

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 of `owner`, and the owner can be a Shop and User model. We have the `ShopsController` and `UsersController`, but not an `OwnersController`. 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.
Copy link
Collaborator

@pablobm pablobm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This checks out for me. Thank you 👍

@pablobm pablobm merged commit 86cfaa8 into thoughtbot:master Aug 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants