clio / polymorphic_integer_type

MIT License
37 stars 35 forks source link

Fix creating polymorphic associations #35

Closed Drew-Goddyn closed 3 years ago

Drew-Goddyn commented 3 years ago

We previously were only including the BelongsToPolymorphicAssociation module in specific activerecord versions. This module overides the replace_keys method to ensure we aren't sending the class's #polymorphic_name when creating associations. When this isn't included we send the polymorphic name which includes the class's namespace and can cause us to not find it in the mapping (assuming the mappings don't include namespaces).

By including it, we send the class object itself and let our fallback logic kick in to try several methods until we get a hit from the mapping.

It also cleans up a few other gotchas like creating a new ModuleGenerator. The generator defines an anonmyous module we can use for calling super since the previous implementation was all inline causing us to capture local variables in the blocks and behave oddly.