graphiti icon indicating copy to clipboard operation
graphiti copied to clipboard

Polymorphic has_one and has_many doesn't respect custom polymorphic_name

Open jasonkarns opened this issue 2 years ago • 0 comments

Rails uses .polymorphic_name to derive the value for the {association}_type attribute value. It's fairly common, if not documented/supported, to override polymorphic_name for AR Models that need to store or support custom translations between the attribute value and ruby class names.

Unfortunately, Graphiti::Adapters::Persistence::Associations directly emits the ruby class name for the _type attribute without consulting polymorphic_name. https://github.com/graphiti-api/graphiti/blob/14e84ef7e10c347ec391b96948365b5195074632/lib/graphiti/adapters/persistence/associations.rb#L64

Luckily, this behavior lives on the adapter, so it can be overridden. However, I would like to push this into the AR adapter so that the rails adapter more closely follows Rails' own behavior.

I think my proposal would be for the Assocations 'base' adapter to have a polymorphic_name method which evaluates to class.name, thus allowing the AR adapter to override as class.polymorphic_name. Unless anyone sees any reason for this to be something that lives on the resource or resource proxy?

jasonkarns avatar Oct 05 '22 17:10 jasonkarns