Customize Delegated Type's type column

Delegated Types leans heavily on Rails’ existing polymorphic associations. The primary association created by delegated_type is a polymorphic belongs_to association.

For apps that are using polymorphic associations but need to customize the column containing the type/class, foreign_type is available as an option. It would be helpful if the same customization were available for Delegated Types.

Since delegated_types already passes its options along to the underlying belongs_to association, this is a relatively straightforward change. Instead of only deriving the type column by using "#{role}_type", it can respect a foreign_type option. (Same option key as used by polymorphic associations.)

I have validated this customization works in an existing Rails app and have made the change in my fork of Rails. Happy to open as a PR (including documentation in via comments).

Example:

delegated_type :entryable, types: %w[ Message Comment ], foreign_type: :entry_class

Working branch: Comparing rails:main...jasonkarns:delegated-type-column · rails/rails · GitHub

PR open: Delegated Type supports customizeable foreign_type column by jasonkarns · Pull Request #45041 · rails/rails · GitHub