Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vasilakisfil
Copy link
Contributor

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.

@mention-bot
Copy link

@vasilakisfil, thanks for your PR! By analyzing the annotation information on this pull request, we identified @DrSayre to be a potential reviewer

@@ -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.
Copy link
Member

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.

Copy link
Member

@bf4 bf4 left a 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, :[])
Copy link
Member

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants