Skip to content

Commit 26bcdbe

Browse files
committed
Clean up docs
1 parent a0dd5e5 commit 26bcdbe

File tree

5 files changed

+40
-34
lines changed

5 files changed

+40
-34
lines changed

docs/howto/add_root_key.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[Back to Guides](../README.md)
2+
13
# How to add root key
24

35
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:

docs/howto/grape_integration.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
The AMS grape formatter relies on the existence of `env['grape.request']` which is implemeted by `Grape::Middleware::Globals`. You can meet his dependency by calling it before mounting the endpoints.
1+
[Back to Guides](../README.md)
2+
3+
The ActiveModelSerializers grape formatter relies on the existence of `env['grape.request']` which is implemeted by `Grape::Middleware::Globals`. You can meet his dependency by calling it before mounting the endpoints.
24

35
In the simpliest way:
46

@@ -15,10 +17,10 @@ or more like what is shown in current Grape tutorials:
1517
module MyApi
1618
class ApiBase < Grape::API
1719
use Grape::Middleware::Globals
18-
20+
1921
require 'grape/active_model_serializers'
2022
include Grape::ActiveModelSerializers
21-
23+
2224
mount MyApi::V1::ApiBase
2325
end
2426
end
@@ -30,10 +32,10 @@ You could meet this dependency with your own middleware. The invocation might lo
3032
module MyApi
3133
class ApiBase < Grape::API
3234
use My::Middleware::Thingamabob
33-
35+
3436
require 'grape/active_model_serializers'
3537
include Grape::ActiveModelSerializers
36-
38+
3739
mount MyApi::V1::ApiBase
3840
end
3941
end

docs/howto/passing_arbitrary_options.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ For example, we could pass in a field, such as `user_id` into our serializer.
1111
```ruby
1212
# posts_controller.rb
1313
class PostsController < ApplicationController
14-
def dashboard
14+
def dashboard
1515
render json: @post, user_id: 12
1616
end
1717
end
@@ -20,7 +20,7 @@ end
2020
class PostSerializer < ActiveModel::Serializer
2121
attributes :id, :title, :body
2222

23-
def comments_by_me
23+
def comments_by_me
2424
Comments.where(user_id: instance_options[:user_id], post_id: object.id)
2525
end
2626
end

docs/howto/test.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[Back to Guides](../README.md)
2+
13
# How to test
24

35
## Controller Serializer Usage

docs/howto/upgrade_from_0_8_to_0_10.md

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,56 @@
55
## Disclaimer
66
### Proceed at your own risk
77
This document attempts to outline steps to upgrade your app based on the collective experience of
8-
developers who have done this already. It may not cover all edge cases and situations that may cause issues,
9-
so please proceed with a certain level of caution.
8+
developers who have done this already. It may not cover all edge cases and situations that may cause issues,
9+
so please proceed with a certain level of caution.
1010

1111
## Overview
1212
This document outlines the steps needed to migrate from `0.8` to `0.10`. The method described
1313
below has been created via the collective knowledge of contributions of those who have done
1414
the migration successfully. The method has been tested specifically for migrating from `0.8.3`
1515
to `0.10.2`.
1616

17-
The high level approach is to upgrade to `0.10` and change all serializers to use
18-
a backwards-compatible `ActiveModel::V08::Serializer`or `ActiveModel::V08::CollectionSerializer`
17+
The high level approach is to upgrade to `0.10` and change all serializers to use
18+
a backwards-compatible `ActiveModel::V08::Serializer`or `ActiveModel::V08::CollectionSerializer`
1919
and a `ActiveModelSerializers::Adapter::V08Adapter`. After a few more manual changes, you should have the same
2020
functionality as you had with `AMS 0.8`. Then, you can continue to develop in your app by creating
2121
new serializers that don't use these backwards compatible versions and slowly migrate
2222
existing serializers to the `0.10` versions as needed.
2323

2424
### `0.10` breaking changes
2525
- Passing a serializer to `render json:` is no longer supported
26-
26+
2727
```ruby
2828
render json: CustomerSerializer.new(customer) # rendered in 0.8, errors in 0.10
2929
```
30-
30+
3131
- Passing a nil resource to serializer now fails
32-
32+
3333
```ruby
3434
CustomerSerializer.new(nil) # returned nil in 0.8, throws error in 0.10
3535
```
36-
36+
3737
- Attribute methods are no longer defined on the serializer, and must be explicitly
3838
accessed through `object`
39-
39+
4040
```ruby
4141
class MySerializer
4242
attributes :foo, :bar
43-
43+
4444
def foo
4545
bar + 1 # bar does not work, needs to be object.bar in 0.10
4646
end
4747
end
4848
```
49-
49+
5050
- `root` option to collection serializer behaves differently
51-
51+
5252
```ruby
5353
# in 0.8
5454
ActiveModel::ArraySerializer.new(resources, root: "resources")
5555
# resulted in { "resources": <serialized_resources> }, does not work in 0.10
5656
```
57-
57+
5858
- No default serializer when serializer doesn't exist
5959
- `@options` changed to `instance_options`
6060

@@ -70,16 +70,16 @@ module ActiveModel
7070
module V08
7171
class Serializer < ActiveModel::Serializer
7272
include Rails.application.routes.url_helpers
73-
73+
7474
# AMS 0.8 would delegate method calls from within the serializer to the
7575
# object.
7676
def method_missing(*args)
7777
method = args.first
7878
read_attribute_for_serialization(method)
7979
end
80-
80+
8181
alias_method :options, :instance_options
82-
82+
8383
# Since attributes could be read from the `object` via `method_missing`,
8484
# the `try` method did not behave as before. This patches `try` with the
8585
# original implementation plus the addition of
@@ -90,7 +90,7 @@ module ActiveModel
9090
try!(*a, &b)
9191
end
9292
end
93-
93+
9494
# AMS 0.8 would return nil if the serializer was initialized with a nil
9595
# resource.
9696
def serializable_hash(adapter_options = nil,
@@ -104,7 +104,7 @@ module ActiveModel
104104
end
105105

106106
```
107-
Add this class to your app however you see fit. This is the class that your existing serializers
107+
Add this class to your app however you see fit. This is the class that your existing serializers
108108
that inherit from `ActiveMode::Serializer` should inherit from.
109109

110110
### 3. Add `ActiveModel::V08::CollectionSerializer`
@@ -145,7 +145,7 @@ module ActiveModel
145145
# the given resource. When not using an adapter, this is not true in
146146
# `0.10`
147147
def serializer_from_resource(resource, serializer_context_class, options)
148-
serializer_class =
148+
serializer_class =
149149
options.fetch(:serializer) { serializer_context_class.serializer_for(resource) }
150150

151151
if serializer_class.nil? # rubocop:disable Style/GuardClause
@@ -170,7 +170,7 @@ module ActiveModel
170170
end
171171
end
172172
```
173-
Add this class to your app however you see fit. This is the class that existing uses of
173+
Add this class to your app however you see fit. This is the class that existing uses of
174174
`ActiveModel::ArraySerializer` should be changed to use.
175175

176176
### 4. Add `ActiveModelSerializers::Adapter::V08Adapter`
@@ -231,20 +231,20 @@ Add
231231
ActiveModelSerializers.config.adapter =
232232
ActiveModelSerializers::Adapter::V08Adapter
233233
```
234-
to `config/active_model_serializer.rb` to configure AMS to use this
234+
to `config/active_model_serializer.rb` to configure AMS to use this
235235
class as the default adapter.
236-
236+
237237
### 5. Change inheritors of `ActiveModel::Serializer` to inherit from `ActiveModel::V08::Serializer`
238238
Simple find/replace
239-
239+
240240
### 6. Remove `private` keyword from serializers
241241
Simple find/replace. This is required to allow the `ActiveModel::V08::Serializer`
242-
to have proper access to the methods defined in the serializer.
242+
to have proper access to the methods defined in the serializer.
243243

244244
You may be able to change the `private` to `protected`, but this is hasn't been tested yet.
245-
245+
246246
### 7. Remove references to `ActiveRecord::Base#active_model_serializer`
247-
This method is no longer supported in `0.10`.
247+
This method is no longer supported in `0.10`.
248248

249249
`0.10` does a good job of discovering serializers for `ActiveRecord` objects.
250250

@@ -260,4 +260,4 @@ Simple find/replace
260260
After you've done the steps above, you should test your app to ensure that everything is still working properly.
261261

262262
If you run into issues, please contribute back to this document so others can benefit from your knowledge.
263-
263+

0 commit comments

Comments
 (0)