Skip to content

Commit bc8fd0a

Browse files
committed
Merge pull request #958 from joaomdmoura/spliting-json-adapter
Splitting json adapter into two
2 parents 7fa123b + e321cb3 commit bc8fd0a

23 files changed

+81
-109
lines changed

lib/action_controller/serialization.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Serialization
66

77
include ActionController::Renderers
88

9-
ADAPTER_OPTION_KEYS = [:include, :fields, :root, :adapter]
9+
ADAPTER_OPTION_KEYS = [:include, :fields, :adapter]
1010

1111
included do
1212
class_attribute :_serialization_scope

lib/active_model/serializer.rb

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,6 @@ def self.adapter
145145
adapter_class
146146
end
147147

148-
def self._root
149-
@@root ||= false
150-
end
151-
152-
def self._root=(root)
153-
@@root = root
154-
end
155-
156148
def self.root_name
157149
name.demodulize.underscore.sub(/_serializer$/, '') if name
158150
end
@@ -162,7 +154,7 @@ def self.root_name
162154
def initialize(object, options = {})
163155
@object = object
164156
@options = options
165-
@root = options[:root] || (self.class._root ? self.class.root_name : false)
157+
@root = options[:root]
166158
@meta = options[:meta]
167159
@meta_key = options[:meta_key]
168160
@scope = options[:scope]
@@ -176,11 +168,7 @@ def initialize(object, options = {})
176168
end
177169

178170
def json_key
179-
if root == true || root.nil?
180-
self.class.root_name
181-
else
182-
root
183-
end
171+
self.class.root_name
184172
end
185173

186174
def id

lib/active_model/serializer/adapter.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class Serializer
55
class Adapter
66
extend ActiveSupport::Autoload
77
autoload :Json
8+
autoload :FlattenJson
89
autoload :Null
910
autoload :JsonApi
1011

@@ -21,7 +22,8 @@ def serializable_hash(options = {})
2122

2223
def as_json(options = {})
2324
hash = serializable_hash(options)
24-
include_meta(hash)
25+
include_meta(hash) unless self.class == FlattenJson
26+
hash
2527
end
2628

2729
def self.create(resource, options = {})
@@ -48,7 +50,7 @@ def cache_check(serializer)
4850
yield
4951
end
5052
elsif is_fragment_cached?
51-
FragmentCache.new(self, @cached_serializer, @options, @root).fetch
53+
FragmentCache.new(self, @cached_serializer, @options).fetch
5254
else
5355
yield
5456
end
@@ -82,11 +84,11 @@ def meta_key
8284
end
8385

8486
def root
85-
@options.fetch(:root) { serializer.json_key }
87+
serializer.json_key.to_sym if serializer.json_key
8688
end
8789

8890
def include_meta(json)
89-
json[meta_key] = meta if meta && root
91+
json[meta_key] = meta if meta
9092
json
9193
end
9294
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module ActiveModel
2+
class Serializer
3+
class Adapter
4+
class FlattenJson < Json
5+
def serializable_hash(options = {})
6+
super
7+
@result
8+
end
9+
end
10+
end
11+
end
12+
end

lib/active_model/serializer/adapter/fragment_cache.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ class FragmentCache
55

66
attr_reader :serializer
77

8-
def initialize(adapter, serializer, options, root)
9-
@root = root
8+
def initialize(adapter, serializer, options)
109
@options = options
1110
@adapter = adapter
1211
@serializer = serializer

lib/active_model/serializer/adapter/json.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Adapter
66
class Json < Adapter
77
def serializable_hash(options = {})
88
if serializer.respond_to?(:each)
9-
@result = serializer.map{|s| self.class.new(s).serializable_hash }
9+
@result = serializer.map{|s| FlattenJson.new(s).serializable_hash }
1010
else
1111
@hash = {}
1212

@@ -37,16 +37,13 @@ def serializable_hash(options = {})
3737
@result = @core.merge @hash
3838
end
3939

40-
if root
41-
@result = { root => @result }
42-
else
43-
@result
44-
end
40+
{ root => @result }
41+
end
42+
43+
def fragment_cache(cached_hash, non_cached_hash)
44+
Json::FragmentCache.new().fragment_cache(cached_hash, non_cached_hash)
4545
end
46-
end
4746

48-
def fragment_cache(cached_hash, non_cached_hash)
49-
Json::FragmentCache.new().fragment_cache(cached_hash, non_cached_hash)
5047
end
5148
end
5249
end

lib/active_model/serializer/adapter/json_api.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ class Adapter
66
class JsonApi < Adapter
77
def initialize(serializer, options = {})
88
super
9-
serializer.root = true
109
@hash = { data: [] }
1110

1211
if fields = options.delete(:fields)

lib/active_model/serializer/array_serializer.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ class ArraySerializer
77
attr_reader :meta, :meta_key
88

99
def initialize(objects, options = {})
10-
options.merge!(root: nil)
11-
12-
@objects = objects.map do |object|
10+
@resource = objects
11+
@objects = objects.map do |object|
1312
serializer_class = options.fetch(
1413
:serializer,
1514
ActiveModel::Serializer.serializer_for(object)
@@ -21,7 +20,11 @@ def initialize(objects, options = {})
2120
end
2221

2322
def json_key
24-
@objects.first.json_key if @objects.first
23+
if @objects.first
24+
@objects.first.json_key.pluralize
25+
else
26+
@resource.name.downcase.pluralize if @resource.try(:name)
27+
end
2528
end
2629

2730
def root=(root)

lib/active_model/serializer/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Configuration
66

77
included do |base|
88
base.config.array_serializer = ActiveModel::Serializer::ArraySerializer
9-
base.config.adapter = :json
9+
base.config.adapter = :flatten_json
1010
end
1111
end
1212
end

lib/active_model/serializer/fieldset.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ def fields
1212
end
1313

1414
def fields_for(serializer)
15-
key = serializer.json_key || serializer.class.root_name
16-
fields[key.to_sym]
15+
key = serializer.json_key
16+
fields[key.to_sym] || fields[key.pluralize.to_sym]
1717
end
1818

1919
private

test/action_controller/explicit_serializer_test.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,10 @@ def test_render_array_using_explicit_serializer
7777
get :render_array_using_explicit_serializer
7878
assert_equal 'application/json', @response.content_type
7979

80-
expected = {
81-
'paginated' => [
82-
{ 'name' => 'Name 1' },
83-
{ 'name' => 'Name 2' }
84-
]
85-
}
80+
expected = [
81+
{ 'name' => 'Name 1' },
82+
{ 'name' => 'Name 2' }
83+
]
8684

8785
assert_equal expected.to_json, @response.body
8886
end

test/action_controller/serialization_test.rb

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ def render_using_implicit_serializer
1010
end
1111

1212
def render_using_custom_root
13-
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
14-
render json: @profile, root: "custom_root"
13+
with_adapter ActiveModel::Serializer::Adapter::Json do
14+
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
15+
render json: @profile, root: "custom_root"
16+
end
1517
end
1618

1719
def render_using_custom_root_and_meta
18-
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
19-
render json: @profile, root: "custom_root", meta: { total: 10 }
20+
with_adapter ActiveModel::Serializer::Adapter::Json do
21+
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
22+
render json: @profile, root: "custom_root", meta: { total: 10 }
23+
end
2024
end
2125

2226
def render_using_default_adapter_root
@@ -34,11 +38,13 @@ def render_using_custom_root_in_adapter_with_a_default
3438
end
3539

3640
def render_array_using_custom_root_and_meta
37-
array = [
38-
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
39-
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
40-
]
41-
render json: array, root: "custom_root", meta: { total: 10 }
41+
with_adapter ActiveModel::Serializer::Adapter::Json do
42+
array = [
43+
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
44+
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
45+
]
46+
render json: array, root: "custom_root", meta: { total: 10 }
47+
end
4248
end
4349

4450
def render_array_using_implicit_serializer
@@ -167,20 +173,6 @@ def test_render_using_implicit_serializer
167173
assert_equal expected.to_json, @response.body
168174
end
169175

170-
def test_render_using_custom_root
171-
get :render_using_custom_root
172-
173-
assert_equal 'application/json', @response.content_type
174-
assert_equal '{"custom_root":{"name":"Name 1","description":"Description 1"}}', @response.body
175-
end
176-
177-
def test_render_using_custom_root_and_meta
178-
get :render_using_custom_root_and_meta
179-
180-
assert_equal 'application/json', @response.content_type
181-
assert_equal '{"custom_root":{"name":"Name 1","description":"Description 1"},"meta":{"total":10}}', @response.body
182-
end
183-
184176
def test_render_using_default_root
185177
get :render_using_default_adapter_root
186178

@@ -217,25 +209,6 @@ def test_render_using_custom_root_in_adapter_with_a_default
217209
assert_equal expected.to_json, @response.body
218210
end
219211

220-
def test_render_array_using_custom_root_and_meta
221-
get :render_array_using_custom_root_and_meta
222-
assert_equal 'application/json', @response.content_type
223-
224-
expected = { custom_root: [
225-
{
226-
name: 'Name 1',
227-
description: 'Description 1',
228-
},
229-
{
230-
name: 'Name 2',
231-
description: 'Description 2',
232-
}],
233-
meta: { total: 10 }
234-
}
235-
236-
assert_equal expected.to_json, @response.body
237-
end
238-
239212
def test_render_array_using_implicit_serializer
240213
get :render_array_using_implicit_serializer
241214
assert_equal 'application/json', @response.content_type

test/adapter/fragment_cache_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def setup
88
@role = Role.new(name: 'Great Author', description:nil)
99
@role.author = [@author]
1010
@role_serializer = RoleSerializer.new(@role)
11-
@role_hash = FragmentCache.new(RoleSerializer.adapter.new(@role_serializer), @role_serializer, {}, nil)
11+
@role_hash = FragmentCache.new(RoleSerializer.adapter.new(@role_serializer), @role_serializer, {})
1212
end
1313

1414
def test_fragment_fetch_with_virtual_attributes

test/adapter/json/belongs_to_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,21 @@ def setup
2525
end
2626

2727
def test_includes_post
28-
assert_equal({id: 42, title: 'New Post', body: 'Body'}, @adapter.serializable_hash[:post])
28+
assert_equal({id: 42, title: 'New Post', body: 'Body'}, @adapter.serializable_hash[:comment][:post])
2929
end
3030

3131
def test_include_nil_author
3232
serializer = PostSerializer.new(@anonymous_post)
3333
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
3434

35-
assert_equal({title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], blog: {id: 999, name: "Custom blog"}, author: nil}, adapter.serializable_hash)
35+
assert_equal({post: {title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], blog: {id: 999, name: "Custom blog"}, author: nil}}, adapter.serializable_hash)
3636
end
3737

3838
def test_include_nil_author_with_specified_serializer
3939
serializer = PostPreviewSerializer.new(@anonymous_post)
4040
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
4141

42-
assert_equal({title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], author: nil}, adapter.serializable_hash)
42+
assert_equal({posts: {title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], author: nil}}, adapter.serializable_hash)
4343
end
4444
end
4545
end

test/adapter/json/collection_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ def test_with_serializer_option
2828
@serializer = ArraySerializer.new([@blog], serializer: CustomBlogSerializer)
2929
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
3030

31-
expected = [{
31+
expected = {custom_blogs:[{
3232
id: 1,
3333
special_attribute: "Special",
3434
articles: [{id: 1,title: "Hello!!", body: "Hello, world!!"}, {id: 2, title: "New Post", body: "Body"}]
35-
}]
35+
}]}
3636
assert_equal expected, @adapter.serializable_hash
3737
end
3838

3939
def test_include_multiple_posts
40-
expected = [{
40+
expected = { posts: [{
4141
title: "Hello!!",
4242
body: "Hello, world!!",
4343
id: 1,
@@ -63,7 +63,7 @@ def test_include_multiple_posts
6363
id: 999,
6464
name: "Custom blog"
6565
}
66-
}]
66+
}]}
6767
assert_equal expected, @adapter.serializable_hash
6868
end
6969
end

test/adapter/json/has_many_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_has_many
2626
assert_equal([
2727
{id: 1, body: 'ZOMG A COMMENT'},
2828
{id: 2, body: 'ZOMG ANOTHER COMMENT'}
29-
], @adapter.serializable_hash[:comments])
29+
], @adapter.serializable_hash[:post][:comments])
3030
end
3131
end
3232
end

test/adapter/json_api/collection_test.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ def test_limiting_fields
8686
}
8787
}
8888
]
89-
9089
assert_equal(expected, @adapter.serializable_hash[:data])
9190
end
9291

test/adapter/json_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_has_many
2525
assert_equal([
2626
{id: 1, body: 'ZOMG A COMMENT'},
2727
{id: 2, body: 'ZOMG ANOTHER COMMENT'}
28-
], @adapter.serializable_hash[:comments])
28+
], @adapter.serializable_hash[:post][:comments])
2929
end
3030
end
3131
end

test/adapter_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_adapter_class_for_unknown_adapter
3131

3232
def test_create_adapter
3333
adapter = ActiveModel::Serializer::Adapter.create(@serializer)
34-
assert_equal ActiveModel::Serializer::Adapter::Json, adapter.class
34+
assert_equal ActiveModel::Serializer::Adapter::FlattenJson, adapter.class
3535
end
3636

3737
def test_create_adapter_with_override

0 commit comments

Comments
 (0)