[DOCS] Refactor, update, create documentation [ci skip]

This commit is contained in:
Benjamin Fleischer
2015-12-10 15:12:06 -06:00
parent 1301b52696
commit ce17a1b305
26 changed files with 740 additions and 485 deletions

View File

@@ -1,3 +1,5 @@
[Back to Guides](../README.md)
# How to add pagination links
### JSON API adapter
@@ -8,6 +10,10 @@ the resource is paginated and if you are using the ```JsonApi``` adapter.
If you want pagination links in your response, use [Kaminari](https://github.com/amatsuda/kaminari)
or [WillPaginate](https://github.com/mislav/will_paginate).
Although the others adapters does not have this feature, it is possible to
implement pagination links to `JSON` adapter. For more information about it,
please see in our docs
###### Kaminari examples
```ruby
@@ -33,7 +39,7 @@ render json: @posts
```
```ruby
ActiveModel::Serializer.config.adapter = :json_api
ActiveModelSerializers.config.adapter = :json_api
```
ex:
@@ -61,7 +67,7 @@ ex:
}
```
AMS pagination relies on a paginated collection with the methods `current_page`, `total_pages`, and `size`, such as are supported by both [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate).
ActiveModelSerializers pagination relies on a paginated collection with the methods `current_page`, `total_pages`, and `size`, such as are supported by both [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate).
### JSON adapter

View File

@@ -1,6 +1,6 @@
# How to add root key
Add the root key to your API is quite simple with AMS. The **Adapter** is what determines the format of your JSON response. The default adapter is the ```Attributes``` which doesn't have the root key, so your response is something similar to:
Add the root key to your API is quite simple with ActiveModelSerializers. The **Adapter** is what determines the format of your JSON response. The default adapter is the ```Attributes``` which doesn't have the root key, so your response is something similar to:
```json
{
@@ -13,10 +13,10 @@ Add the root key to your API is quite simple with AMS. The **Adapter** is what d
In order to add the root key you need to use the ```JSON``` Adapter, you can change this in an initializer:
```ruby
ActiveModel::Serializer.config.adapter = :json
ActiveModelSerializers.config.adapter = :json
```
You can also specify a class as adapter, as long as it complies with the AMS adapters interface.
You can also specify a class as adapter, as long as it complies with the ActiveModelSerializers adapters interface.
It will add the root key to all your serialized endpoints.
ex:

View File

@@ -1,8 +1,10 @@
## Using AMS Outside Of A Controller
[Back to Guides](../README.md)
### Serializing a resource
## Using ActiveModelSerializers Outside Of A Controller
In AMS versions 0.10 or later, serializing resources outside of the controller context is fairly simple:
### Serializing a resource
In ActiveModelSerializers versions 0.10 or later, serializing resources outside of the controller context is fairly simple:
```ruby
# Create our resource
@@ -16,14 +18,14 @@ serializable_resource = ActiveModel::SerializableResource.new(post, options)
# Convert your resource into json
model_json = serializable_resource.as_json
```
```
### Retrieving a Resource's Active Model Serializer
### Looking up the Serializer for a Resource
If you want to retrieve a serializer for a specific resource, you can do the following:
```ruby
# Create our resource
# Create our resource
post = Post.create(title: "Another Example", body: "So much fun.")
# Optional options parameters
@@ -33,10 +35,24 @@ options = {}
serializer = ActiveModel::Serializer.serializer_for(post, options)
```
You could also retrieve the serializer via:
You could also retrieve the serializer via:
```ruby
ActiveModel::SerializableResource.new(post, options).serializer
ActiveModel::SerializableResource.new(post, options).serializer
```
Both approaches will return an instance, if any, of the resource's serializer.
Both approaches will return an instance, if any, of the resource's serializer.
## Serializing before controller render
At times, you might want to use a serializer without rendering it to the view. For those cases, you can create an instance of `ActiveModel::SerializableResource` with
the resource you want to be serialized and call `.as_json`.
```ruby
def create
message = current_user.messages.create!(message_params)
message_json = ActiveModel::SerializableResource.new(message).as_json
MessageCreationWorker.perform(message_json)
head 204
end
```