Skip to content

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

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?

- `#read_attribute_for_serialization(name)`: gets the attribute value for serialization

ActiveRecord::Base objects include ActiveModel::Serialization

The additionally must implement:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They?

- `#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
Copy link
Member Author

Choose a reason for hiding this comment

The 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:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with the snippet below sounds better IMO, or with the following snippet


```ruby
serializer_class = ActiveModel::Serializer.serializer_for(obj)
serializer = serializer_class.new(obj)
adapter = ActiveModel::Serializer.adapter.new(serializer)
adapter.serializable_hash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is outdated.

Copy link
Member Author

Choose a reason for hiding this comment

The 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).
Expand Down
2 changes: 1 addition & 1 deletion test/serializers/cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def test_fragment_fetch_with_virtual_associations
assert_equal({place: 'Nowhere'}, ActionController::Base.cache_store.fetch(@location.cache_key))
end

def test_uses_file_digest_in_cahe_key
def test_uses_file_digest_in_cache_key
blog = render_object_with_cache(@blog)
assert_equal(@blog_serializer.attributes, ActionController::Base.cache_store.fetch(@blog.cache_key_with_digest))
end
Expand Down