Conversation
|
@vasilakisfil, thanks for your PR! By analyzing the annotation information on this pull request, we identified @DrSayre to be a potential reviewer |
120c380 to
fdd9970
Compare
|
|
||
| Fortunately, ActiveModelSerializers provides a [`ActiveModelSerializers::Model`](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model_serializers/model.rb) which you can use in production code that will make your PORO a lot cleaner. The above code now becomes: | ||
| ### Inheriting ActiveModelSerializers::Model | ||
| Fortunately, ActiveModelSerializers provides a [`ActiveModelSerializers::Model`](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model_serializers/model.rb) which you can use in production code that will make your PORO a lot cleaner. |
There was a problem hiding this comment.
What's missing from these docs is that AMS::Model also serves as executable documentation of the serializable interface, which can be copied or referenced without actually using the class.
bf4
left a comment
There was a problem hiding this comment.
Question considering intent of using a UseSerializer with a hash
| def show | ||
| #this is just an example | ||
| an_instance = {id: 1, name: 'Just an example'} | ||
| an_instance.singleton_class.send(:alias_method, :read_attribute_for_serialization, :[]) |
There was a problem hiding this comment.
I think maybe what we want here is something like:
Given a user instance, the serializer should act the user to get the defined attributes and associations. However, since the transformation for a user with id: 1 and name: 'Mortimer' could be compared to something like user_attributes = user.attributes; user_attributes.slice(:id, :name), wouldn't it be nice if we could start from user_attributesas{ id: 1, name: 'Mortimer' }` and have the serializer treat that hash as user_attributes?
Purpose
I found it a bit frustrating sometimes when I just wanted to serialize an object using an AMS serializer, but I first had to define a class for it and either inherit or duck type the needed AMS methods. I showcase how this can be done quickly and dirty but I explicitly say that it is not recommended.
Changes
Enhance PORO documentation
Caveats
Related GitHub issues
Additional helpful information
Sometimes I felt that POROs (its methods actually that have the same name with the serializer defined attributes) and Hashes (the values which belong to keys which have the same name with the serializer defined attributes) should be serialized automatically without doing anything when specifying a serializer.