mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Merge branch 'master' into domitian-move-namespace-of-adapter-to-active-model-serializers
Conflicts: CHANGELOG.md lib/active_model/serializer/adapter/attributes.rb lib/active_model/serializer/adapter/cached_serializer.rb lib/active_model/serializer/adapter/fragment_cache.rb lib/active_model/serializer/adapter/json_api.rb lib/active_model/serializer/adapter/json_api/link.rb test/adapter/fragment_cache_test.rb test/adapter/json_api/links_test.rb test/adapter/json_api/resource_type_config_test.rb
This commit is contained in:
@@ -56,7 +56,7 @@ High-level overview:
|
||||
- `:each_serializer` specifies the serializer for each resource in the collection.
|
||||
- For a single resource, the `:serializer` option is the resource serializer.
|
||||
- Options are partitioned in serializer options and adapter options. Keys for adapter options are specified by
|
||||
[`ADAPTER_OPTIONS`](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializable_resource.rb#L4).
|
||||
[`ADAPTER_OPTION_KEYS`](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializable_resource.rb#L4).
|
||||
The remaining options are serializer options.
|
||||
|
||||
Details:
|
||||
@@ -64,7 +64,7 @@ Details:
|
||||
1. **ActionController::Serialization**
|
||||
1. `serializable_resource = ActiveModel::SerializableResource.new(resource, options)`
|
||||
1. `options` are partitioned into `adapter_opts` and everything else (`serializer_opts`).
|
||||
The adapter options keys for the are defined by `ADAPTER_OPTIONS`.
|
||||
The `adapter_opts` keys are defined in `ActiveModel::SerializableResource::ADAPTER_OPTION_KEYS`.
|
||||
1. **ActiveModel::SerializableResource**
|
||||
1. `if serializable_resource.serializer?` (there is a serializer for the resource, and an adapter is used.)
|
||||
- Where `serializer?` is `use_adapter? && !!(serializer)`
|
||||
|
||||
@@ -103,7 +103,46 @@ PR please :)
|
||||
|
||||
#### links
|
||||
|
||||
PR please :)
|
||||
##### How to add top-level links
|
||||
|
||||
JsonApi supports a [links object](http://jsonapi.org/format/#document-links) to be specified at top-level, that you can specify in the `render`:
|
||||
|
||||
```ruby
|
||||
links_object = {
|
||||
href: "http://example.com/api/posts",
|
||||
meta: {
|
||||
count: 10
|
||||
}
|
||||
}
|
||||
render json: @posts, links: links_object
|
||||
```
|
||||
|
||||
That's the result:
|
||||
|
||||
```json
|
||||
{
|
||||
"data": [
|
||||
{
|
||||
"type": "posts",
|
||||
"id": "1",
|
||||
"attributes": {
|
||||
"title": "JSON API is awesome!",
|
||||
"body": "You should be using JSON API",
|
||||
"created": "2015-05-22T14:56:29.000Z",
|
||||
"updated": "2015-05-22T14:56:28.000Z"
|
||||
}
|
||||
}
|
||||
],
|
||||
"links": {
|
||||
"href": "http://example.com/api/posts",
|
||||
"meta": {
|
||||
"count": 10
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
This feature is specific to JsonApi, so you have to use the use the [JsonApi Adapter](adapters.md#jsonapi)
|
||||
|
||||
### serializer_opts
|
||||
|
||||
|
||||
@@ -107,12 +107,30 @@ end
|
||||
|
||||
#### ::type
|
||||
|
||||
e.g.
|
||||
The `::type` method defines the JSONAPI [type](http://jsonapi.org/format/#document-resource-object-identification) that will be rendered for this serializer.
|
||||
It either takes a `String` or `Symbol` as parameter.
|
||||
|
||||
Note: This method is useful only when using the `:json_api` adapter.
|
||||
|
||||
Examples:
|
||||
```ruby
|
||||
class UserProfileSerializer < ActiveModel::Serializer
|
||||
type 'profile'
|
||||
end
|
||||
class AuthorProfileSerializer < ActiveModel::Serializer
|
||||
type :profile
|
||||
end
|
||||
```
|
||||
|
||||
With the `:json_api` adapter, the previous serializers would be rendered as:
|
||||
|
||||
``` json
|
||||
{
|
||||
"data": {
|
||||
"id": "1",
|
||||
"type": "profile"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### ::link
|
||||
|
||||
@@ -74,32 +74,30 @@ ActiveModelSerializers pagination relies on a paginated collection with the meth
|
||||
|
||||
If you are using `JSON` adapter, pagination links will not be included automatically, but it is possible to do so using `meta` key.
|
||||
|
||||
In your action specify a custom serializer.
|
||||
```ruby
|
||||
render json: @posts, serializer: PaginatedSerializer, each_serializer: PostPreviewSerializer
|
||||
```
|
||||
Add this method to your base API controller.
|
||||
|
||||
And then, you could do something like the following class.
|
||||
```ruby
|
||||
class PaginatedSerializer < ActiveModel::Serializer::CollectionSerializer
|
||||
def initialize(object, options={})
|
||||
meta_key = options[:meta_key] || :meta
|
||||
options[meta_key] ||= {}
|
||||
options[meta_key] = {
|
||||
current_page: object.current_page,
|
||||
next_page: object.next_page,
|
||||
prev_page: object.prev_page,
|
||||
total_pages: object.total_pages,
|
||||
total_count: object.total_count
|
||||
}
|
||||
super(object, options)
|
||||
end
|
||||
def pagination_dict(object)
|
||||
{
|
||||
current_page: object.current_page,
|
||||
next_page: object.next_page,
|
||||
prev_page: object.prev_page,
|
||||
total_pages: object.total_pages,
|
||||
total_count: object.total_count
|
||||
}
|
||||
end
|
||||
```
|
||||
|
||||
Then, use it on your render method.
|
||||
|
||||
```ruby
|
||||
render json: posts, meta: pagination_dict(posts)
|
||||
```
|
||||
|
||||
ex.
|
||||
```json
|
||||
{
|
||||
"articles": [
|
||||
"posts": [
|
||||
{
|
||||
"id": 2,
|
||||
"title": "JSON API paints my bikeshed!",
|
||||
|
||||
Reference in New Issue
Block a user