-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Document serialization interface and what to test #967
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
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 |
---|---|---|
|
@@ -322,6 +322,48 @@ class PostSerializer < ActiveModel::Serializer | |
end | ||
``` | ||
|
||
## Serializable Resource Interface | ||
|
||
Serializable resources must include [`ActiveModel::Serialization`](https://github.com/rails/rails/blob/master/activemodel/lib/active_model/serialization.rb) | ||
or implement its interface: | ||
|
||
- `#serializable_hash(options)`: hash representation of a resources attributes | ||
- `#read_attribute_for_serialization(name)`: gets the attribute value for serialization | ||
|
||
ActiveRecord::Base objects include ActiveModel::Serialization | ||
|
||
The additionally must implement: | ||
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.
|
||
- `#as_json(options)`: Hash representation of a serialized resource, may delegate to #serializable_hash | ||
- `#to_json(options)`: string representation (JSON) to Hash from `as_json` | ||
- `#cache_key`: a (self-expiring) unique key for the instance, used by the adapter | ||
- `#id`: a unique identifier for the object | ||
- `::model_name`: an ActiveModel::Name instance, used by the serializer to identify the type | ||
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. Done in #996 |
||
|
||
ActiveRecord::Base objects implement these methods by default. | ||
|
||
## Manual Serialization | ||
|
||
For the simple case, you may serialize a object with the below snippet: | ||
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.
|
||
|
||
```ruby | ||
serializer_class = ActiveModel::Serializer.serializer_for(obj) | ||
serializer = serializer_class.new(obj) | ||
adapter = ActiveModel::Serializer.adapter.new(serializer) | ||
adapter.serializable_hash | ||
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. This is outdated. 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. Yeah, it's been needing an update for a few months.. I just haven't circled back to it #967 (comment) |
||
``` | ||
|
||
For more information, see [ActionController::Serialization](https://github.com/rails-api/active_model_serializers/blob/master/lib/action_controller/serialization.rb) | ||
|
||
## Testing Serializers | ||
|
||
Things a serializer test should cover: | ||
|
||
| What | How | ||
|----- | ---- | ||
| serialized attributes | e.g. `serializer._attributes` | ||
| serialized nested resources (has associations) | e.g. `serializer._associations` | ||
| serializing a single resource and collections | e.g. see manual serialization code above | ||
|
||
## Getting Help | ||
|
||
If you find a bug, please report an [Issue](https://github.com/rails-api/active_model_serializers/issues/new). | ||
|
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.
Could we detail the current/future options?