Skip to content

Commit 1ea5608

Browse files
committed
updating tests to match new adapters structure
1 parent 2e46507 commit 1ea5608

17 files changed

+49
-43
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/adapter.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ def serializable_hash(options = {})
2222

2323
def as_json(options = {})
2424
hash = serializable_hash(options)
25-
include_meta(hash)
25+
include_meta(hash) unless self.class == FlattenJson
26+
hash
2627
end
2728

2829
def self.create(resource, options = {})

lib/active_model/serializer/array_serializer.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ class ArraySerializer
77
attr_reader :meta, :meta_key
88

99
def initialize(objects, options = {})
10-
options.merge!(root: nil)
11-
1210
@objects = objects.map do |object|
1311
serializer_class = options.fetch(
1412
:serializer,
@@ -21,7 +19,7 @@ def initialize(objects, options = {})
2119
end
2220

2321
def json_key
24-
@objects.first.json_key.pluralize if @objects.first
22+
@objects.first.json_key if @objects.first
2523
end
2624

2725
def root=(root)

lib/active_model/serializer/fieldset.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def fields
1212
end
1313

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

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: 16 additions & 9 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
@@ -219,6 +225,7 @@ def test_render_using_custom_root_in_adapter_with_a_default
219225

220226
def test_render_array_using_custom_root_and_meta
221227
get :render_array_using_custom_root_and_meta
228+
222229
assert_equal 'application/json', @response.content_type
223230

224231
expected = { custom_root: [

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_blog:[{
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 = { post: [{
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

0 commit comments

Comments
 (0)