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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,16 @@ | |
|
||
# How to serialize a Plain-Old Ruby Object (PORO) | ||
|
||
When you are first getting started with ActiveModelSerializers, it may seem only `ActiveRecord::Base` objects can be serializable, but pretty much any object can be serializable with ActiveModelSerializers. Here is an example of a PORO that is serializable: | ||
### Using Duck Typing | ||
When you are first getting started with ActiveModelSerializers, it may seem only `ActiveRecord::Base` objects can be serializable, but pretty much any object can be serializable with ActiveModelSerializers. | ||
Here is an example of a PORO that is serializable just by implementing the needed methods: | ||
|
||
```ruby | ||
# my_model.rb | ||
class MyModel | ||
alias :read_attribute_for_serialization :send | ||
attr_accessor :id, :name, :level | ||
|
||
def initialize(attributes) | ||
@id = attributes[:id] | ||
@name = attributes[:name] | ||
|
@@ -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. | ||
The above code now becomes: | ||
```ruby | ||
# my_model.rb | ||
class MyModel < ActiveModelSerializers::Model | ||
attr_accessor :id, :name, :level | ||
end | ||
``` | ||
|
||
The default serializer would be `MyModelSerializer`. | ||
The default serializer would be `MyModelSerializer`. | ||
|
||
### Serializing an instance | ||
Sometimes you don't want to create yet another class just to serialize an object. | ||
Well, athough it is recommended to have defined classes for what you serialize, | ||
this is possible as well: | ||
|
||
Given the following serializer: | ||
|
||
```ruby | ||
class Api::V1::UserSerializer | ||
type :user #this is necessery, otherwise AMS will show your instance class name | ||
|
||
attributes :id, :name | ||
``` | ||
|
||
```ruby | ||
class Api::V1::UsersController | ||
def show | ||
#this is just an example | ||
an_instance = OpenStruct.new(id: 1, name: 'Just an example') | ||
|
||
an_instance.singleton_class.send(:alias_method, :read_attribute_for_serialization, :send) | ||
|
||
render jsonapi: an_instance, serializer: Api::V1::UserSerializer | ||
end | ||
end | ||
``` | ||
|
||
If your object is just a hash you can do the following trick: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean by saying "models" ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ping @bf4 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @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:
and then we would have a caching layer available in the pipeline |
||
```ruby | ||
class Api::V1::UsersController | ||
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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think maybe what we want here is something like: Given a |
||
|
||
render jsonapi: an_instance, serializer: Api::V1::UserSerializer, | ||
end | ||
end | ||
``` | ||
Note however that if your hash keys are strings then you will need to convert them | ||
to symbols first (or just wrap it in `HashWithIndifferentAccess`). |
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.