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
from

Conversation

Projects
None yet
4 participants
@vasilakisfil
Contributor

vasilakisfil commented Sep 3, 2016

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

This comment has been minimized.

Show comment
Hide comment
@mention-bot

mention-bot Sep 3, 2016

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

mention-bot commented Sep 3, 2016

@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.

This comment has been minimized.

@bf4

bf4 Sep 5, 2016

Member

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

bf4 Sep 5, 2016

Member

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.

```
If your object is just a hash you can do the following trick:

This comment has been minimized.

@bf4

bf4 Sep 5, 2016

Member

I would like to strongly recommend not trying to serialize primitives like Hashes. AMS is intended to models. There's a lot of parts of the AMS contract that would be weird to have on a hash. https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializer/lint.rb

@bf4

bf4 Sep 5, 2016

Member

I would like to strongly recommend not trying to serialize primitives like Hashes. AMS is intended to models. There's a lot of parts of the AMS contract that would be weird to have on a hash. https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializer/lint.rb

This comment has been minimized.

@vasilakisfil

vasilakisfil Sep 5, 2016

Contributor

What do you mean by saying "models" ?

@vasilakisfil

vasilakisfil Sep 5, 2016

Contributor

What do you mean by saying "models" ?

This comment has been minimized.

@vasilakisfil

vasilakisfil Sep 5, 2016

Contributor

ping @bf4

@vasilakisfil

This comment has been minimized.

@bf4

bf4 Sep 5, 2016

Member

@vasilakisfil by model I mean a class that represents some domain concern. A Hash, being a primitive, doesn't do that. I think there's is a layer of AMS should be doing hash transformations, but that's not it's primary input, and that layer isn't yet exposed.

The imaginary pipeline is something like:

Given a model
Define attributes and associations to serializer (via ActiveModel::Serializer)
Serialize model to 'serializable_hash' per serializer and fields/include options (coordinated by adapter)

# This is where a hash would begin its journey, if this interface were exposed

Given a serializable hash
Transform in adapter

and then we would have a caching layer available in the pipeline

@bf4

bf4 Sep 5, 2016

Member

@vasilakisfil by model I mean a class that represents some domain concern. A Hash, being a primitive, doesn't do that. I think there's is a layer of AMS should be doing hash transformations, but that's not it's primary input, and that layer isn't yet exposed.

The imaginary pipeline is something like:

Given a model
Define attributes and associations to serializer (via ActiveModel::Serializer)
Serialize model to 'serializable_hash' per serializer and fields/include options (coordinated by adapter)

# This is where a hash would begin its journey, if this interface were exposed

Given a serializable hash
Transform in adapter

and then we would have a caching layer available in the pipeline

@bf4

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, :[])

This comment has been minimized.

@bf4

bf4 Jan 10, 2017

Member

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 likeuser_attributes = user.attributes; user_attributes.slice(:id, :name), wouldn't it be nice if we could start fromuser_attributesas{ id: 1, name: 'Mortimer' }` and have the serializer treat that hash as user_attributes?

@bf4

bf4 Jan 10, 2017

Member

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 likeuser_attributes = user.attributes; user_attributes.slice(:id, :name), wouldn't it be nice if we could start fromuser_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