Skip to content

Commit d9ba5cd

Browse files
committed
Fix typos and make examples more explicit
1 parent 5e72ec4 commit d9ba5cd

File tree

1 file changed

+77
-62
lines changed

1 file changed

+77
-62
lines changed

docs/howto/upgrade_from_0_8_to_0_10.md

Lines changed: 77 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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 situation that may cause issues,
8+
developers who have done this already. It may not cover all edge cases and situations that may cause issues,
99
so please proceed with a certain level of caution.
1010

1111
## Overview
@@ -15,30 +15,46 @@ the migration successfully. The method has been tested specifically for migratin
1515
to `0.10.2`.
1616

1717
The high level approach is to upgrade to `0.10` and change all serializers to use
18-
a backwards-compatible `VersionEightSerializer`or `VersionEightCollectionSerializer`
19-
and a `VersionEightAdapter`. After a few more manual changes, you should have the same
18+
a backwards-compatible `ActiveModel::V08::Serializer`or `ActiveModel::V08::CollectionSerializer`
19+
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-
- Ex. `render json: CustomerSerializer.new(customer)`
26+
27+
```ruby
28+
render json: CustomerSerializer.new(customer) # rendered in 0.8, errors in 0.10
29+
```
30+
2731
- Passing a nil resource to serializer now fails
28-
- Ex. `CustomerSerializer.new(nil)`
29-
- Attribute methods are no longer accessible from other serializer methods
30-
- Ex.
31-
```ruby
32-
class MySerializer
33-
attributes :foo, :bar
34-
35-
def foo
36-
bar + 1
37-
end
38-
end
39-
```
32+
33+
```ruby
34+
CustomerSerializer.new(nil) # returned nil in 0.8, throws error in 0.10
35+
```
36+
37+
- Attribute methods are no longer defined on the serializer, and must be explicitly
38+
accessed through `object`
39+
40+
```ruby
41+
class MySerializer
42+
attributes :foo, :bar
43+
44+
def foo
45+
bar + 1 # bar does not work, needs to be object.bar in 0.10
46+
end
47+
end
48+
```
49+
4050
- `root` option to collection serializer behaves differently
41-
- Ex. `ActiveModel::ArraySerializer.new(resources, root: "resources")`
51+
52+
```ruby
53+
# in 0.8
54+
ActiveModel::ArraySerializer.new(resources, root: "resources")
55+
# resulted in { "resources": <serialized_resources> }, does not work in 0.10
56+
```
57+
4258
- No default serializer when serializer doesn't exist
4359
- `@options` changed to `instance_options`
4460

@@ -47,41 +63,42 @@ existing serializers to the `0.10` versions as needed.
4763
### 1. Upgrade the `active_model_serializer` gem in you `Gemfile`
4864
Change to `gem 'active_model_serializers', '~> 0.10'` and run `bundle install`
4965

50-
### 2. Add `VersionEightSerializer`
66+
### 2. Add `ActiveModel::V08::Serializer`
5167

52-
#### Code
5368
```ruby
5469
module ActiveModel
55-
class VersionEightSerializer < Serializer
56-
include Rails.application.routes.url_helpers
57-
58-
# AMS 0.8 would delegate method calls from within the serializer to the
59-
# object.
60-
def method_missing(*args)
61-
method = args.first
62-
read_attribute_for_serialization(method)
63-
end
64-
65-
alias_method :options, :instance_options
66-
67-
# Since attributes could be read from the `object` via `method_missing`,
68-
# the `try` method did not behave as before. This patches `try` with the
69-
# original implementation plus the addition of
70-
# ` || object.respond_to?(a.first, true)` to check if the object responded to
71-
# the given method.
72-
def try(*a, &b)
73-
if a.empty? || respond_to?(a.first, true) || object.respond_to?(a.first, true)
74-
try!(*a, &b)
70+
module V08
71+
class Serializer < ActiveModel::Serializer
72+
include Rails.application.routes.url_helpers
73+
74+
# AMS 0.8 would delegate method calls from within the serializer to the
75+
# object.
76+
def method_missing(*args)
77+
method = args.first
78+
read_attribute_for_serialization(method)
79+
end
80+
81+
alias_method :options, :instance_options
82+
83+
# Since attributes could be read from the `object` via `method_missing`,
84+
# the `try` method did not behave as before. This patches `try` with the
85+
# original implementation plus the addition of
86+
# ` || object.respond_to?(a.first, true)` to check if the object responded to
87+
# the given method.
88+
def try(*a, &b)
89+
if a.empty? || respond_to?(a.first, true) || object.respond_to?(a.first, true)
90+
try!(*a, &b)
91+
end
92+
end
93+
94+
# AMS 0.8 would return nil if the serializer was initialized with a nil
95+
# resource.
96+
def serializable_hash(adapter_options = nil,
97+
options = {},
98+
adapter_instance =
99+
self.class.serialization_adapter_instance)
100+
object.nil? ? nil : super
75101
end
76-
end
77-
78-
# AMS 0.8 would return nil if the serializer was initialized with a nil
79-
# resource.
80-
def serializable_hash(adapter_options = nil,
81-
options = {},
82-
adapter_instance =
83-
self.class.serialization_adapter_instance)
84-
object.nil? ? nil : super
85102
end
86103
end
87104
end
@@ -90,12 +107,11 @@ end
90107
Add this class to your app however you see fit. This is the class that your existing serializers
91108
that inherit from `ActiveMode::Serializer` should inherit from.
92109

93-
### 3. Add `VersionEightCollectionSerializer`
94-
#### Code
110+
### 3. Add `ActiveModel::V08::CollectionSerializer`
95111
```ruby
96112
module ActiveModel
97-
class Serializer
98-
class VersionEightCollectionSerializer < CollectionSerializer
113+
module V08
114+
class CollectionSerializer < ActiveModel::Serializer::CollectionSerializer
99115
# In AMS 0.8, passing an ArraySerializer instance with a `root` option
100116
# properly nested the serialized resources within the given root.
101117
# Ex.
@@ -155,14 +171,13 @@ module ActiveModel
155171
end
156172
```
157173
Add this class to your app however you see fit. This is the class that existing uses of
158-
`ActiveMode::ArraySerializer` should be changed to use.
174+
`ActiveModel::ArraySerializer` should be changed to use.
159175

160-
### 4. Add `VersionEightAdapter`
161-
#### Code
176+
### 4. Add `ActiveModelSerializers::Adapter::V08Adapter`
162177
```ruby
163178
module ActiveModelSerializers
164179
module Adapter
165-
class VersionEightAdapter < Base
180+
class V08Adapter < ActiveModelSerializers::Adapter::Base
166181
def serializable_hash(options = nil)
167182
options ||= {}
168183

@@ -179,7 +194,7 @@ module ActiveModelSerializers
179194

180195
def serializable_hash_for_collection(options)
181196
serializer.map do |s|
182-
VersionEightAdapter.new(s, instance_options)
197+
V08Adapter.new(s, instance_options)
183198
.serializable_hash(options)
184199
end
185200
end
@@ -214,16 +229,16 @@ Add this class to your app however you see fit.
214229
Add
215230
```ruby
216231
ActiveModelSerializers.config.adapter =
217-
ActiveModelSerializers::Adapter::VersionEightAdapter
232+
ActiveModelSerializers::Adapter::V08Adapter
218233
```
219234
to `config/active_model_serializer.rb` to configure AMS to use this
220235
class as the default adapter.
221236

222-
### 5. Change inheritors of `ActiveModel::Serializer` to inherit from `ActiveModel::VersionEightSerializer`
237+
### 5. Change inheritors of `ActiveModel::Serializer` to inherit from `ActiveModel::V08::Serializer`
223238
Simple find/replace
224239

225240
### 6. Remove `private` keyword from serializers
226-
Simple find/replace. This is required to allow the `ActiveModel::VersionEightSerializer`
241+
Simple find/replace. This is required to allow the `ActiveModel::V08::Serializer`
227242
to have proper access to the methods defined in the serializer.
228243

229244
You may be able to change the `private` to `protected`, but this is hasn't been tested yet.
@@ -233,8 +248,8 @@ This method is no longer supported in `0.10`.
233248

234249
`0.10` does a good job of discovering serializers for `ActiveRecord` objects.
235250

236-
### 8. Rename `ActiveModel::ArraySerializer` to `ActiveModel::Serializer::Version8CollectionSerializer`
237-
Find/replace uses of `ActiveModel::ArraySerializer` with `ActiveModel::Serializer::Version8CollectionSerializer`.
251+
### 8. Rename `ActiveModel::ArraySerializer` to `ActiveModel::V08::CollectionSerializer`
252+
Find/replace uses of `ActiveModel::ArraySerializer` with `ActiveModel::V08::CollectionSerializer`.
238253

239254
Also, be sure to change the `each_serializer` keyword to `serializer` when calling making the replacement.
240255

0 commit comments

Comments
 (0)