Commit 9952073
committed
Ensure inheritance hooks run
I was seeing transient failures where adapters may not be registered.
e.g. https://travis-ci.org/rails-api/active_model_serializers/builds/77735382
Since we're using the Adapter, JsonApi, and Json classes
as namespaces, some of the conventions we use for modules don't apply.
Basically, we don't want to define the class anywhere besides itself.
Otherwise, the inherited hooks may not run, and some adapters may not
be registered.
For example:
If we have a class Api `class Api; end`
And Api is also used as a namespace for `Api::Product`
And the classes are defined in different files.
In one file:
```ruby
class Api
autoload :Product
def self.inherited(subclass)
puts
p [:inherited, subclass.name]
puts
end
end
```
And in another:
```ruby
class Api
class Product < Api
def sell_sell_sell!
# TODO: sell
end
end
end
```
If we load the Api class file first, the inherited hook will be defined on the class
so that when we load the Api::Product class, we'll see the output:
```plain
[ :inherited, Api::Product]
```
However, if we load the Api::Product class first, since it defines the `Api` class
and then inherited from it, the Api file was never loaded, the hook never defined,
and thus never run.
By defining the class as `class Api::Product < Api` We ensure the the Api class
MUST be defined, and thus, the hook will be defined and run and so sunshine and unicorns.
Appendix:
The below would work, but triggers a circular reference warning.
It's also not recommended to mix require with autoload.
```ruby
require 'api'
class Api
class Product < Api
def sell_sell_sell!
# TODO: sell
end
end
end
```
This failure scenario was introduced by removing the circular reference warnings in
rails-api#1067
Style note:
To make diffs on the adapters smalleer and easier to read, I've maintained the same
identention that was in the original file. I've decided to prefer ease of reading
the diff over style, esp. since we may later return to the preferred class declaration style.1 parent 2ba54fc commit 9952073
File tree
14 files changed
+65
-85
lines changed- lib/active_model
- serializer
- adapter
- json_api
- json
- test
- action_controller
14 files changed
+65
-85
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
11 | 47 | | |
12 | 48 | | |
13 | 49 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
79 | | - | |
| 79 | + | |
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | 8 | | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
| 1 | + | |
5 | 2 | | |
6 | 3 | | |
7 | 4 | | |
| |||
13 | 10 | | |
14 | 11 | | |
15 | 12 | | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | 13 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
| 1 | + | |
5 | 2 | | |
6 | 3 | | |
7 | 4 | | |
| |||
75 | 72 | | |
76 | 73 | | |
77 | 74 | | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | 75 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
2 | 4 | | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | 5 | | |
8 | 6 | | |
9 | 7 | | |
| |||
44 | 42 | | |
45 | 43 | | |
46 | 44 | | |
47 | | - | |
| 45 | + | |
48 | 46 | | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | 47 | | |
Lines changed: 1 addition & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
| 1 | + | |
7 | 2 | | |
8 | 3 | | |
9 | 4 | | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | 5 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
3 | 5 | | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | 6 | | |
9 | 7 | | |
10 | 8 | | |
| |||
39 | 37 | | |
40 | 38 | | |
41 | 39 | | |
42 | | - | |
| 40 | + | |
43 | 41 | | |
44 | 42 | | |
45 | | - | |
46 | | - | |
47 | 43 | | |
48 | 44 | | |
49 | 45 | | |
| |||
169 | 165 | | |
170 | 166 | | |
171 | 167 | | |
172 | | - | |
173 | | - | |
| 168 | + | |
| 169 | + | |
174 | 170 | | |
175 | 171 | | |
176 | 172 | | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
181 | 177 | | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | 178 | | |
Lines changed: 1 addition & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
| 1 | + | |
7 | 2 | | |
8 | 3 | | |
9 | 4 | | |
| |||
15 | 10 | | |
16 | 11 | | |
17 | 12 | | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | 13 | | |
Lines changed: 1 addition & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
| 1 | + | |
6 | 2 | | |
7 | 3 | | |
8 | 4 | | |
| |||
51 | 47 | | |
52 | 48 | | |
53 | 49 | | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | 50 | | |
0 commit comments