active_model_serializers icon indicating copy to clipboard operation
active_model_serializers copied to clipboard

Singularize and capitalize polymorphic types

Open dpdawson opened this issue 8 years ago • 9 comments

Singularizing polymorphic association types allows you to use plural or singular type values in your requests as allowed by JSON API.

Because ActiveRecord calls #constantize on the polymorphic association type when you use the hash to create a new model, you must capitalize the type to avoid a NameError.

dpdawson avatar Mar 24 '16 04:03 dpdawson

@dpdawson Awesome! Would you mind adding a changelog entry at the top of the fixes section? Also, a test of an ActiveRecord object using this result would be fantastic, if you don't mind the extra effort.

bf4 avatar Mar 27 '16 16:03 bf4

As I looked into it more, I realized the error only gets thrown when you validate the relationship is present. The fix doesn't cause any of the other tests to fail, so I assume it's ok.

I added a test, but I'm sure it's not the best implementation because I don't use Minitest and the organization of the tests is a bit difficult to discern at first glance.

dpdawson avatar Mar 28 '16 06:03 dpdawson

With a rebase and a brief look at the Transformer with @remear this should be ready to mege

bf4 avatar Apr 01 '16 08:04 bf4

I'd be happy to take a look if pointed in the right direction.

dpdawson avatar Apr 05 '16 15:04 dpdawson

@dpdawson Could you rebase this off master and take a look at adjusting KeyTransform to suit the needs of singularize.capitalize.

remear avatar Apr 05 '16 16:04 remear

Any updates on this? Or has the problem been solved in a different way? I'm currently running into this issue.

bartiaco avatar Feb 14 '17 20:02 bartiaco

One note: I don't think the current implementation will work for multi-word classes, given that the standard wordbreak in jsonapi is a '-'

"blog-posts".singularize.capitalize => "Blog-post"

bartiaco avatar Feb 14 '17 20:02 bartiaco

I get this situation here, by now, I solve with hardcoded fix on controller. I made a SO because I had not seen this. Please consider merge it.

brunowego avatar May 01 '17 04:05 brunowego

@brunowego @bartiaco I'm reading the comments above and looks like the PR didn't get finished by anyone. Unless @dpdawson wants to pick it up again, it'll need to be reworked against master, and also related #1928 cc @NullVoxPopuli

bf4 avatar May 01 '17 05:05 bf4