-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhance PORO documentation #1910
base: master
Are you sure you want to change the base?
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
@@ -21,12 +24,56 @@ class MyModel | |||
end | |||
``` | |||
|
|||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question considering intent of using a UseSerializer with a hash
#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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.