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

Field::Polymorphic accepts a call-able for the classes option #2391

Merged
merged 1 commit into from
Jun 9, 2023

Conversation

brent-yearone
Copy link
Contributor

This commit allows a field definition like this:

ATTRIBUTE_TYPES = {
  listable: Field::Polymorphic.with_options(classes: -> { Listable.listable_classes })
}

In my app I use a pattern where I have polymorphic associations that use a module to come into being. The module defines the associations and any special behaviors, and the model on the other side of the polymorphic relationship just includes the module to get the association and all the behaviors. At that time, the module registers the calling class. In the example above, the module is Listable; any model that includes Listable then has_many :lists, as: :listable.

It's a great pattern. The trouble is, the administrate dashboards get parsed before my models, so without the call-able classes option, Listable.listable_classes ends up empty all the time. By making it callable, it's evaluated at runtime, after the models are parsed, so we get the correct class list. Yay for dynamic discovery!

This commit allows a field definition like this:

ATTRIBUTE_TYPES = {
  listable: Field::Polymorphic.with_options(classes: -> { Listable.listable_classes })
}
@pablobm
Copy link
Collaborator

pablobm commented Jun 9, 2023

Sounds good to me 👍 Thank you for the PR.

@pablobm pablobm merged commit 44d4387 into thoughtbot:main Jun 9, 2023
@brent-yearone
Copy link
Contributor Author

You're most welcome! Thank you for all the work you do to maintain this most excellent library 😀

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