josevalim/active_model_serializers — pull request #23 by Adman65 (2011-12-20 21:13:23+00:00)
Title: Use the action_model_serializer method to determine the object for serializing associated objects.

I started out this this problem: My application has a Message superclass, then Im, SMS, and Email subclasses. They all serialize differently, but are just given as `messages`. You cannot do this with pervious structure because the Serializer class is determined at load time, not serialization time.

@josevalim and I talked about how to solve the problem. The code defines a `active_model_serializer` method on all things that use `ActiveModel::Serializers`. Instead of determining the serializer classes based off the `:name` or `:key`, we'd use the object being serialized to determine how to serialize itself. This is essentially just delegating to `#active_model_serializer`. This is a much better structure because the association doesn't need to know anything about how to serialization itself or worry about constantizing things.

This also creates possible solutions to some open issues. Now a serializer instance is created for each object allowing you to pass some options (eventually) and handle more edge cases.

This change also changes the behavior of the library. Previously the library would implicitly detect nested serializers. (See failing tests). Now if you want to use your own serializer instead of the objects, you can pass `:serializer`.

All in all, I think this is a positive change for the library because.

1. Supports polymorphic arrays (Rails STI for the use case)
2. Makes the library more flexible because it does not have to determine constants at Load time.
3. Allows serialization options to be passed to objects at runtime.