Field::Polymorphic accepts a call-able for the classes option #2391
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.
This commit allows a field definition like this:
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 includesListable
thenhas_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!