Skip to content

Commit a309132

Browse files
committed
Merge pull request #1117 from bf4/remove_flatten_json_inheriting_json
FlattenJson adapter no longer inherits Json adapter, renamed to Attributes
2 parents 24a5f38 + c6f8d0f commit a309132

16 files changed

+89
-80
lines changed

.rubocop.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ AllCops:
1010

1111
Style/IndentationConsistency:
1212
Exclude:
13-
- lib/active_model/serializer/adapter/flatten_json.rb
13+
- lib/active_model/serializer/adapter/attributes.rb
1414
- lib/active_model/serializer/adapter/fragment_cache.rb
1515
- lib/active_model/serializer/adapter/json.rb
1616
- lib/active_model/serializer/adapter/json/fragment_cache.rb
@@ -21,7 +21,7 @@ Style/IndentationConsistency:
2121

2222
Style/IndentationWidth:
2323
Exclude:
24-
- lib/active_model/serializer/adapter/flatten_json.rb
24+
- lib/active_model/serializer/adapter/attributes.rb
2525
- lib/active_model/serializer/adapter/fragment_cache.rb
2626
- lib/active_model/serializer/adapter/json.rb
2727
- lib/active_model/serializer/adapter/json/fragment_cache.rb
@@ -32,7 +32,7 @@ Style/IndentationWidth:
3232

3333
Style/AccessModifierIndentation:
3434
Exclude:
35-
- lib/active_model/serializer/adapter/flatten_json.rb
35+
- lib/active_model/serializer/adapter/attributes.rb
3636
- lib/active_model/serializer/adapter/fragment_cache.rb
3737
- lib/active_model/serializer/adapter/json.rb
3838
- lib/active_model/serializer/adapter/json/fragment_cache.rb

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ AMS does this through two components: **serializers** and **adapters**.
1212
Serializers describe _which_ attributes and relationships should be serialized.
1313
Adapters describe _how_ attributes and relationships should be serialized.
1414

15-
By default AMS will use the **Flatten Json Adapter**. But we strongly advise you to use **JsonApi Adapter** that follows 1.0 of the format specified in [jsonapi.org/format](http://jsonapi.org/format).
15+
By default AMS will use the **Attributes Adapter**. But we strongly advise you to use **JsonApi Adapter** that follows 1.0 of the format specified in [jsonapi.org/format](http://jsonapi.org/format).
1616
Check how to change the adapter in the sections bellow.
1717

1818
# RELEASE CANDIDATE, PLEASE READ
@@ -66,7 +66,7 @@ ActiveModel::Serializer.config.adapter = :json_api
6666
You won't need to implement an adapter unless you wish to use a new format or
6767
media type with AMS.
6868

69-
If you want to have a root key on your responses you should use the Json adapter, instead of the default FlattenJson:
69+
If you want to have a root key on your responses you should use the Json adapter, instead of the default Attributes:
7070

7171
```ruby
7272
ActiveModel::Serializer.config.adapter = :json
@@ -137,7 +137,7 @@ render json: @post, meta: { total: 10 }, meta_key: "custom_meta"
137137
```
138138

139139
`meta` will only be included in your response if you are using an Adapter that supports `root`,
140-
as JsonAPI and Json adapters, the default adapter (FlattenJson) doesn't have `root`.
140+
as JsonAPI and Json adapters, the default adapter (Attributes) doesn't have `root`.
141141

142142
### Using a serializer without `render`
143143

@@ -190,7 +190,7 @@ end
190190

191191
### Built in Adapters
192192

193-
#### FlattenJSON
193+
#### Attributes
194194

195195
It's the default adapter, it generates a json response without a root key.
196196
Doesn't follow any specifc convention.

docs/general/adapters.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
AMS does this through two components: **serializers** and **adapters**.
44
Serializers describe _which_ attributes and relationships should be serialized.
55
Adapters describe _how_ attributes and relationships should be serialized.
6-
You can use one of the built-in adapters (```FlattenJSON``` is the default one) or create one by yourself, but you won't need to implement an adapter unless you wish to use a new format or media type with AMS.
6+
You can use one of the built-in adapters (```Attributes``` is the default one) or create one by yourself, but you won't need to implement an adapter unless you wish to use a new format or media type with AMS.
77

88
## Built in Adapters
99

10-
### FlattenJSON - Default
10+
### Attributes - Default
1111

1212
It's the default adapter, it generates a json response without a root key.
1313
Doesn't follow any specifc convention.
@@ -51,7 +51,7 @@ ActiveModel::Serializer.config.adapter = :json_api
5151
```
5252

5353
If you want to have a root key for each resource in your responses, you should use the Json or
54-
JsonApi adapters instead of the default FlattenJson:
54+
JsonApi adapters instead of the default Attributes:
5555

5656
```ruby
5757
ActiveModel::Serializer.config.adapter = :json

docs/general/configuration_options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ The following configuration options can be set on `ActiveModel::Serializer.confi
44

55
## General
66

7-
- `adapter`: The [adapter](adapters.md) to use. Possible values: `:flatten_json, :json, :json_api`. Default: `:flatten_json`.
7+
- `adapter`: The [adapter](adapters.md) to use. Possible values: `:attributes, :json, :json_api`. Default: `:attributes`.
88

99
## JSON API
1010

docs/howto/add_pagination_links.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,6 @@ ex.
110110
}
111111
```
112112

113-
### FlattenJSON adapter
113+
### Attributes adapter
114114

115115
This adapter does not allow us to use `meta` key, due to that it is not possible to add pagination links.

docs/howto/add_root_key.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# How to add root key
22

3-
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 ```FlattenJSON``` which doesn't have the root key, so your response is something similar to:
3+
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:
44

55
```json
66
{

lib/active_model/serializer/adapter.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ class Adapter
55
ADAPTER_MAP = {}
66
private_constant :ADAPTER_MAP if defined?(private_constant)
77
extend ActiveSupport::Autoload
8+
autoload :Attributes
9+
autoload :Null
810
autoload :FragmentCache
911
autoload :Json
1012
autoload :JsonApi
11-
autoload :Null
12-
autoload :FlattenJson
1313
autoload :CachedSerializer
1414

1515
def self.create(resource, options = {})
@@ -74,7 +74,8 @@ def lookup(adapter)
7474
# @api private
7575
def find_by_name(adapter_name)
7676
adapter_name = adapter_name.to_s.classify.tr('API', 'Api')
77-
ActiveModel::Serializer::Adapter.const_get(adapter_name.to_sym) or # rubocop:disable Style/AndOr
77+
"ActiveModel::Serializer::Adapter::#{adapter_name}".safe_constantize ||
78+
"ActiveModel::Serializer::Adapter::#{adapter_name.pluralize}".safe_constantize or # rubocop:disable Style/AndOr
7879
fail UnknownAdapterError
7980
end
8081
private :find_by_name
@@ -112,12 +113,14 @@ def cache_check(serializer)
112113
end
113114
end
114115

116+
private
117+
115118
def meta
116119
serializer.meta if serializer.respond_to?(:meta)
117120
end
118121

119122
def meta_key
120-
serializer.meta_key || 'meta'
123+
serializer.meta_key || 'meta'.freeze
121124
end
122125

123126
def root
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class ActiveModel::Serializer::Adapter::Attributes < ActiveModel::Serializer::Adapter
2+
def serializable_hash(options = nil)
3+
options ||= {}
4+
if serializer.respond_to?(:each)
5+
result = serializer.map { |s| Attributes.new(s).serializable_hash(options) }
6+
else
7+
hash = {}
8+
9+
core = cache_check(serializer) do
10+
serializer.attributes(options)
11+
end
12+
13+
serializer.associations.each do |association|
14+
serializer = association.serializer
15+
association_options = association.options
16+
17+
if serializer.respond_to?(:each)
18+
array_serializer = serializer
19+
hash[association.key] = array_serializer.map do |item|
20+
cache_check(item) do
21+
item.attributes(association_options)
22+
end
23+
end
24+
else
25+
hash[association.key] =
26+
if serializer && serializer.object
27+
cache_check(serializer) do
28+
serializer.attributes(options)
29+
end
30+
elsif association_options[:virtual_value]
31+
association_options[:virtual_value]
32+
end
33+
end
34+
end
35+
result = core.merge hash
36+
end
37+
result
38+
end
39+
40+
def fragment_cache(cached_hash, non_cached_hash)
41+
Json::FragmentCache.new.fragment_cache(cached_hash, non_cached_hash)
42+
end
43+
44+
private
45+
46+
# no-op: Attributes adapter does not include meta data, because it does not support root.
47+
def include_meta(json)
48+
json
49+
end
50+
end

lib/active_model/serializer/adapter/flatten_json.rb

Lines changed: 0 additions & 12 deletions
This file was deleted.

lib/active_model/serializer/adapter/json.rb

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,12 @@ class ActiveModel::Serializer::Adapter::Json < ActiveModel::Serializer::Adapter
44

55
def serializable_hash(options = nil)
66
options ||= {}
7-
if serializer.respond_to?(:each)
8-
result = serializer.map { |s| FlattenJson.new(s).serializable_hash(options) }
9-
else
10-
hash = {}
11-
12-
core = cache_check(serializer) do
13-
serializer.attributes(options)
14-
end
15-
16-
serializer.associations.each do |association|
17-
serializer = association.serializer
18-
association_options = association.options
19-
20-
if serializer.respond_to?(:each)
21-
array_serializer = serializer
22-
hash[association.key] = array_serializer.map do |item|
23-
cache_check(item) do
24-
item.attributes(association_options)
25-
end
26-
end
27-
else
28-
hash[association.key] =
29-
if serializer && serializer.object
30-
cache_check(serializer) do
31-
serializer.attributes(options)
32-
end
33-
elsif association_options[:virtual_value]
34-
association_options[:virtual_value]
35-
end
36-
end
37-
end
38-
result = core.merge hash
39-
end
40-
41-
{ root => result }
7+
{ root => Attributes.new(serializer).serializable_hash(options) }
428
end
439

10+
private
11+
4412
def fragment_cache(cached_hash, non_cached_hash)
45-
ActiveModel::Serializer::Adapter::Json::FragmentCache.new().fragment_cache(cached_hash, non_cached_hash)
13+
ActiveModel::Serializer::Adapter::Json::FragmentCache.new.fragment_cache(cached_hash, non_cached_hash)
4614
end
4715
end

0 commit comments

Comments
 (0)