active_model_serializers 0.10.0.rc4 → 0.10.0.rc5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/ISSUE_TEMPLATE.md +29 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +19 -1
- data/.rubocop_todo.yml +30 -103
- data/.simplecov +0 -1
- data/.travis.yml +20 -8
- data/CHANGELOG.md +89 -5
- data/CONTRIBUTING.md +54 -179
- data/Gemfile +7 -2
- data/{LICENSE.txt → MIT-LICENSE} +0 -0
- data/README.md +27 -5
- data/Rakefile +44 -16
- data/active_model_serializers.gemspec +9 -1
- data/appveyor.yml +1 -0
- data/bin/bench +171 -0
- data/bin/bench_regression +316 -0
- data/bin/serve_benchmark +39 -0
- data/docs/ARCHITECTURE.md +13 -7
- data/docs/README.md +5 -1
- data/docs/STYLE.md +58 -0
- data/docs/general/adapters.md +99 -16
- data/docs/general/configuration_options.md +87 -14
- data/docs/general/deserialization.md +100 -0
- data/docs/general/getting_started.md +35 -0
- data/docs/general/instrumentation.md +1 -1
- data/docs/general/key_transforms.md +40 -0
- data/docs/general/rendering.md +115 -13
- data/docs/general/serializers.md +138 -6
- data/docs/howto/add_pagination_links.md +36 -18
- data/docs/howto/outside_controller_use.md +4 -4
- data/docs/howto/passing_arbitrary_options.md +27 -0
- data/docs/jsonapi/errors.md +56 -0
- data/docs/jsonapi/schema.md +29 -18
- data/docs/rfcs/0000-namespace.md +106 -0
- data/docs/rfcs/template.md +15 -0
- data/lib/action_controller/serialization.rb +10 -19
- data/lib/active_model/serializable_resource.rb +4 -65
- data/lib/active_model/serializer.rb +73 -18
- data/lib/active_model/serializer/adapter.rb +15 -82
- data/lib/active_model/serializer/adapter/attributes.rb +5 -56
- data/lib/active_model/serializer/adapter/base.rb +5 -47
- data/lib/active_model/serializer/adapter/json.rb +6 -12
- data/lib/active_model/serializer/adapter/json_api.rb +5 -213
- data/lib/active_model/serializer/adapter/null.rb +7 -3
- data/lib/active_model/serializer/array_serializer.rb +3 -3
- data/lib/active_model/serializer/association.rb +4 -5
- data/lib/active_model/serializer/attributes.rb +1 -1
- data/lib/active_model/serializer/caching.rb +56 -5
- data/lib/active_model/serializer/collection_serializer.rb +30 -13
- data/lib/active_model/serializer/configuration.rb +7 -0
- data/lib/active_model/serializer/error_serializer.rb +10 -0
- data/lib/active_model/serializer/errors_serializer.rb +27 -0
- data/lib/active_model/serializer/links.rb +4 -2
- data/lib/active_model/serializer/lint.rb +14 -0
- data/lib/active_model/serializer/meta.rb +29 -0
- data/lib/active_model/serializer/null.rb +17 -0
- data/lib/active_model/serializer/reflection.rb +57 -1
- data/lib/active_model/serializer/type.rb +1 -1
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +17 -0
- data/lib/active_model_serializers/adapter.rb +92 -0
- data/lib/active_model_serializers/adapter/attributes.rb +94 -0
- data/lib/active_model_serializers/adapter/base.rb +90 -0
- data/lib/active_model_serializers/adapter/json.rb +11 -0
- data/lib/active_model_serializers/adapter/json_api.rb +513 -0
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +213 -0
- data/lib/active_model_serializers/adapter/json_api/error.rb +96 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +49 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +83 -0
- data/lib/active_model_serializers/adapter/json_api/meta.rb +37 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +57 -0
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +52 -0
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +37 -0
- data/lib/active_model_serializers/adapter/null.rb +10 -0
- data/lib/active_model_serializers/cached_serializer.rb +87 -0
- data/lib/active_model_serializers/callbacks.rb +1 -1
- data/lib/active_model_serializers/deprecate.rb +55 -0
- data/lib/active_model_serializers/deserialization.rb +2 -2
- data/lib/active_model_serializers/fragment_cache.rb +118 -0
- data/lib/active_model_serializers/json_pointer.rb +14 -0
- data/lib/active_model_serializers/key_transform.rb +70 -0
- data/lib/active_model_serializers/logging.rb +4 -1
- data/lib/active_model_serializers/model.rb +11 -1
- data/lib/active_model_serializers/railtie.rb +9 -1
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +64 -0
- data/lib/active_model_serializers/serializable_resource.rb +81 -0
- data/lib/active_model_serializers/serialization_context.rb +24 -2
- data/lib/active_model_serializers/test/schema.rb +2 -2
- data/lib/grape/formatters/active_model_serializers.rb +1 -1
- data/test/action_controller/adapter_selector_test.rb +1 -1
- data/test/action_controller/json_api/deserialization_test.rb +56 -3
- data/test/action_controller/json_api/errors_test.rb +41 -0
- data/test/action_controller/json_api/linked_test.rb +10 -9
- data/test/action_controller/json_api/pagination_test.rb +2 -2
- data/test/action_controller/json_api/transform_test.rb +180 -0
- data/test/action_controller/serialization_scope_name_test.rb +201 -35
- data/test/action_controller/serialization_test.rb +39 -7
- data/test/active_model_serializers/adapter_for_test.rb +208 -0
- data/test/active_model_serializers/cached_serializer_test.rb +80 -0
- data/test/active_model_serializers/fragment_cache_test.rb +34 -0
- data/test/active_model_serializers/json_pointer_test.rb +20 -0
- data/test/active_model_serializers/key_transform_test.rb +263 -0
- data/test/active_model_serializers/logging_test.rb +8 -8
- data/test/active_model_serializers/railtie_test_isolated.rb +6 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +58 -0
- data/test/adapter/deprecation_test.rb +100 -0
- data/test/adapter/json/belongs_to_test.rb +32 -34
- data/test/adapter/json/collection_test.rb +73 -75
- data/test/adapter/json/has_many_test.rb +36 -38
- data/test/adapter/json/transform_test.rb +93 -0
- data/test/adapter/json_api/belongs_to_test.rb +127 -129
- data/test/adapter/json_api/collection_test.rb +80 -82
- data/test/adapter/json_api/errors_test.rb +78 -0
- data/test/adapter/json_api/fields_test.rb +68 -70
- data/test/adapter/json_api/has_many_embed_ids_test.rb +32 -34
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +75 -77
- data/test/adapter/json_api/has_many_test.rb +121 -123
- data/test/adapter/json_api/has_one_test.rb +59 -61
- data/test/adapter/json_api/json_api_test.rb +28 -30
- data/test/adapter/json_api/linked_test.rb +319 -321
- data/test/adapter/json_api/links_test.rb +75 -50
- data/test/adapter/json_api/pagination_links_test.rb +115 -82
- data/test/adapter/json_api/parse_test.rb +114 -116
- data/test/adapter/json_api/relationship_test.rb +161 -0
- data/test/adapter/json_api/relationships_test.rb +199 -0
- data/test/adapter/json_api/resource_identifier_test.rb +85 -0
- data/test/adapter/json_api/resource_meta_test.rb +100 -0
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +61 -63
- data/test/adapter/json_api/transform_test.rb +500 -0
- data/test/adapter/json_api/type_test.rb +61 -0
- data/test/adapter/json_test.rb +35 -37
- data/test/adapter/null_test.rb +13 -15
- data/test/adapter/polymorphic_test.rb +72 -0
- data/test/adapter_test.rb +27 -29
- data/test/array_serializer_test.rb +7 -8
- data/test/benchmark/app.rb +65 -0
- data/test/benchmark/benchmarking_support.rb +67 -0
- data/test/benchmark/bm_caching.rb +117 -0
- data/test/benchmark/bm_transform.rb +34 -0
- data/test/benchmark/config.ru +3 -0
- data/test/benchmark/controllers.rb +77 -0
- data/test/benchmark/fixtures.rb +167 -0
- data/test/cache_test.rb +388 -0
- data/test/collection_serializer_test.rb +10 -0
- data/test/fixtures/active_record.rb +12 -0
- data/test/fixtures/poro.rb +28 -3
- data/test/grape_test.rb +5 -5
- data/test/lint_test.rb +9 -0
- data/test/serializable_resource_test.rb +59 -3
- data/test/serializers/associations_test.rb +8 -8
- data/test/serializers/attribute_test.rb +7 -7
- data/test/serializers/caching_configuration_test_isolated.rb +170 -0
- data/test/serializers/meta_test.rb +74 -6
- data/test/serializers/read_attribute_for_serialization_test.rb +79 -0
- data/test/serializers/serialization_test.rb +55 -0
- data/test/support/isolated_unit.rb +3 -0
- data/test/support/rails5_shims.rb +26 -8
- data/test/support/rails_app.rb +38 -18
- data/test/support/serialization_testing.rb +5 -5
- data/test/test_helper.rb +6 -10
- metadata +132 -37
- data/docs/DESIGN.textile +7 -1
- data/lib/active_model/serializer/adapter/cached_serializer.rb +0 -45
- data/lib/active_model/serializer/adapter/fragment_cache.rb +0 -111
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +0 -13
- data/lib/active_model/serializer/adapter/json_api/deserialization.rb +0 -207
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +0 -21
- data/lib/active_model/serializer/adapter/json_api/link.rb +0 -44
- data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +0 -58
- data/test/active_model_serializers/serialization_context_test.rb +0 -18
- data/test/adapter/fragment_cache_test.rb +0 -38
- data/test/adapter/json_api/resource_type_config_test.rb +0 -71
- data/test/serializers/adapter_for_test.rb +0 -166
- data/test/serializers/cache_test.rb +0 -209
- data/test/support/simplecov.rb +0 -6
- data/test/support/stream_capture.rb +0 -50
- data/test/support/test_case.rb +0 -19
@@ -1,45 +1,43 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
class
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
end
|
3
|
+
module ActiveModelSerializers
|
4
|
+
module Adapter
|
5
|
+
class Json
|
6
|
+
class HasManyTestTest < ActiveSupport::TestCase
|
7
|
+
def setup
|
8
|
+
ActionController::Base.cache_store.clear
|
9
|
+
@author = Author.new(id: 1, name: 'Steve K.')
|
10
|
+
@post = Post.new(id: 42, title: 'New Post', body: 'Body')
|
11
|
+
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
12
|
+
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
|
13
|
+
@post.comments = [@first_comment, @second_comment]
|
14
|
+
@post.author = @author
|
15
|
+
@first_comment.post = @post
|
16
|
+
@second_comment.post = @post
|
17
|
+
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
18
|
+
@post.blog = @blog
|
19
|
+
@tag = Tag.new(id: 1, name: '#hash_tag')
|
20
|
+
@post.tags = [@tag]
|
21
|
+
end
|
23
22
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
23
|
+
def test_has_many
|
24
|
+
serializer = PostSerializer.new(@post)
|
25
|
+
adapter = ActiveModelSerializers::Adapter::Json.new(serializer)
|
26
|
+
assert_equal([
|
27
|
+
{ id: 1, body: 'ZOMG A COMMENT' },
|
28
|
+
{ id: 2, body: 'ZOMG ANOTHER COMMENT' }
|
29
|
+
], adapter.serializable_hash[:post][:comments])
|
30
|
+
end
|
32
31
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
end
|
32
|
+
def test_has_many_with_no_serializer
|
33
|
+
serializer = PostWithTagsSerializer.new(@post)
|
34
|
+
adapter = ActiveModelSerializers::Adapter::Json.new(serializer)
|
35
|
+
assert_equal({
|
36
|
+
id: 42,
|
37
|
+
tags: [
|
38
|
+
{ 'id' => 1, 'name' => '#hash_tag' }
|
39
|
+
]
|
40
|
+
}.to_json, adapter.serializable_hash[:post].to_json)
|
43
41
|
end
|
44
42
|
end
|
45
43
|
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModelSerializers
|
4
|
+
module Adapter
|
5
|
+
class Json
|
6
|
+
class KeyCaseTest < ActiveSupport::TestCase
|
7
|
+
def mock_request(key_transform = nil)
|
8
|
+
context = Minitest::Mock.new
|
9
|
+
context.expect(:request_url, URI)
|
10
|
+
context.expect(:query_parameters, {})
|
11
|
+
@options = {}
|
12
|
+
@options[:key_transform] = key_transform if key_transform
|
13
|
+
@options[:serialization_context] = context
|
14
|
+
end
|
15
|
+
|
16
|
+
Post = Class.new(::Model)
|
17
|
+
class PostSerializer < ActiveModel::Serializer
|
18
|
+
attributes :id, :title, :body, :publish_at
|
19
|
+
end
|
20
|
+
|
21
|
+
def setup
|
22
|
+
ActionController::Base.cache_store.clear
|
23
|
+
@blog = Blog.new(id: 1, name: 'My Blog!!', special_attribute: 'neat')
|
24
|
+
serializer = CustomBlogSerializer.new(@blog)
|
25
|
+
@adapter = ActiveModelSerializers::Adapter::Json.new(serializer)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_transform_default
|
29
|
+
mock_request
|
30
|
+
assert_equal({
|
31
|
+
blog: { id: 1, special_attribute: 'neat', articles: nil }
|
32
|
+
}, @adapter.serializable_hash(@options))
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_transform_global_config
|
36
|
+
mock_request
|
37
|
+
result = with_config(key_transform: :camel_lower) do
|
38
|
+
@adapter.serializable_hash(@options)
|
39
|
+
end
|
40
|
+
assert_equal({
|
41
|
+
blog: { id: 1, specialAttribute: 'neat', articles: nil }
|
42
|
+
}, result)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_transform_serialization_ctx_overrides_global_config
|
46
|
+
mock_request(:camel)
|
47
|
+
result = with_config(key_transform: :camel_lower) do
|
48
|
+
@adapter.serializable_hash(@options)
|
49
|
+
end
|
50
|
+
assert_equal({
|
51
|
+
Blog: { Id: 1, SpecialAttribute: 'neat', Articles: nil }
|
52
|
+
}, result)
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_transform_undefined
|
56
|
+
mock_request(:blam)
|
57
|
+
result = nil
|
58
|
+
assert_raises NoMethodError do
|
59
|
+
result = @adapter.serializable_hash(@options)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_transform_dash
|
64
|
+
mock_request(:dash)
|
65
|
+
assert_equal({
|
66
|
+
blog: { id: 1, :"special-attribute" => 'neat', articles: nil }
|
67
|
+
}, @adapter.serializable_hash(@options))
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_transform_unaltered
|
71
|
+
mock_request(:unaltered)
|
72
|
+
assert_equal({
|
73
|
+
blog: { id: 1, special_attribute: 'neat', articles: nil }
|
74
|
+
}, @adapter.serializable_hash(@options))
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_transform_camel
|
78
|
+
mock_request(:camel)
|
79
|
+
assert_equal({
|
80
|
+
Blog: { Id: 1, SpecialAttribute: 'neat', Articles: nil }
|
81
|
+
}, @adapter.serializable_hash(@options))
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_transform_camel_lower
|
85
|
+
mock_request(:camel_lower)
|
86
|
+
assert_equal({
|
87
|
+
blog: { id: 1, specialAttribute: 'neat', articles: nil }
|
88
|
+
}, @adapter.serializable_hash(@options))
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -1,155 +1,153 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
class
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
@author.posts = []
|
3
|
+
module ActiveModelSerializers
|
4
|
+
module Adapter
|
5
|
+
class JsonApi
|
6
|
+
class BelongsToTest < ActiveSupport::TestCase
|
7
|
+
def setup
|
8
|
+
@author = Author.new(id: 1, name: 'Steve K.')
|
9
|
+
@author.bio = nil
|
10
|
+
@author.roles = []
|
11
|
+
@blog = Blog.new(id: 23, name: 'AMS Blog')
|
12
|
+
@post = Post.new(id: 42, title: 'New Post', body: 'Body')
|
13
|
+
@anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
|
14
|
+
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
15
|
+
@post.comments = [@comment]
|
16
|
+
@post.blog = @blog
|
17
|
+
@anonymous_post.comments = []
|
18
|
+
@anonymous_post.blog = nil
|
19
|
+
@comment.post = @post
|
20
|
+
@comment.author = nil
|
21
|
+
@post.author = @author
|
22
|
+
@anonymous_post.author = nil
|
23
|
+
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
24
|
+
@blog.writer = @author
|
25
|
+
@blog.articles = [@post, @anonymous_post]
|
26
|
+
@author.posts = []
|
28
27
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
28
|
+
@serializer = CommentSerializer.new(@comment)
|
29
|
+
@adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer)
|
30
|
+
ActionController::Base.cache_store.clear
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_includes_post_id
|
34
|
+
expected = { data: { type: 'posts', id: '42' } }
|
35
|
+
|
36
|
+
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:post])
|
37
|
+
end
|
33
38
|
|
34
|
-
|
35
|
-
|
39
|
+
def test_includes_linked_post
|
40
|
+
@adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer, include: [:post])
|
41
|
+
expected = [{
|
42
|
+
id: '42',
|
43
|
+
type: 'posts',
|
44
|
+
attributes: {
|
45
|
+
title: 'New Post',
|
46
|
+
body: 'Body'
|
47
|
+
},
|
48
|
+
relationships: {
|
49
|
+
comments: { data: [{ type: 'comments', id: '1' }] },
|
50
|
+
blog: { data: { type: 'blogs', id: '999' } },
|
51
|
+
author: { data: { type: 'authors', id: '1' } }
|
52
|
+
}
|
53
|
+
}]
|
54
|
+
assert_equal expected, @adapter.serializable_hash[:included]
|
55
|
+
end
|
36
56
|
|
37
|
-
|
38
|
-
|
57
|
+
def test_limiting_linked_post_fields
|
58
|
+
@adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer, include: [:post], fields: { post: [:title, :comments, :blog, :author] })
|
59
|
+
expected = [{
|
60
|
+
id: '42',
|
61
|
+
type: 'posts',
|
62
|
+
attributes: {
|
63
|
+
title: 'New Post'
|
64
|
+
},
|
65
|
+
relationships: {
|
66
|
+
comments: { data: [{ type: 'comments', id: '1' }] },
|
67
|
+
blog: { data: { type: 'blogs', id: '999' } },
|
68
|
+
author: { data: { type: 'authors', id: '1' } }
|
69
|
+
}
|
70
|
+
}]
|
71
|
+
assert_equal expected, @adapter.serializable_hash[:included]
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_include_nil_author
|
75
|
+
serializer = PostSerializer.new(@anonymous_post)
|
76
|
+
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
|
77
|
+
|
78
|
+
assert_equal({ comments: { data: [] }, blog: { data: { type: 'blogs', id: '999' } }, author: { data: nil } }, adapter.serializable_hash[:data][:relationships])
|
79
|
+
end
|
39
80
|
|
40
|
-
|
41
|
-
|
42
|
-
|
81
|
+
def test_include_type_for_association_when_different_than_name
|
82
|
+
serializer = BlogSerializer.new(@blog)
|
83
|
+
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
|
84
|
+
relationships = adapter.serializable_hash[:data][:relationships]
|
85
|
+
expected = {
|
86
|
+
writer: {
|
87
|
+
data: {
|
88
|
+
type: 'authors',
|
89
|
+
id: '1'
|
90
|
+
}
|
91
|
+
},
|
92
|
+
articles: {
|
93
|
+
data: [
|
94
|
+
{
|
95
|
+
type: 'posts',
|
96
|
+
id: '42'
|
97
|
+
},
|
98
|
+
{
|
99
|
+
type: 'posts',
|
100
|
+
id: '43'
|
101
|
+
}
|
102
|
+
]
|
103
|
+
}
|
104
|
+
}
|
105
|
+
assert_equal expected, relationships
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_include_linked_resources_with_type_name
|
109
|
+
serializer = BlogSerializer.new(@blog)
|
110
|
+
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, include: [:writer, :articles])
|
111
|
+
linked = adapter.serializable_hash[:included]
|
112
|
+
expected = [
|
113
|
+
{
|
114
|
+
id: '1',
|
115
|
+
type: 'authors',
|
116
|
+
attributes: {
|
117
|
+
name: 'Steve K.'
|
118
|
+
},
|
119
|
+
relationships: {
|
120
|
+
posts: { data: [] },
|
121
|
+
roles: { data: [] },
|
122
|
+
bio: { data: nil }
|
123
|
+
}
|
124
|
+
}, {
|
43
125
|
id: '42',
|
44
126
|
type: 'posts',
|
45
127
|
attributes: {
|
46
128
|
title: 'New Post',
|
47
|
-
body: 'Body'
|
129
|
+
body: 'Body'
|
48
130
|
},
|
49
131
|
relationships: {
|
50
132
|
comments: { data: [{ type: 'comments', id: '1' }] },
|
51
133
|
blog: { data: { type: 'blogs', id: '999' } },
|
52
134
|
author: { data: { type: 'authors', id: '1' } }
|
53
135
|
}
|
54
|
-
}
|
55
|
-
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_limiting_linked_post_fields
|
59
|
-
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: [:post], fields: { post: [:title, :comments, :blog, :author] })
|
60
|
-
expected = [{
|
61
|
-
id: '42',
|
136
|
+
}, {
|
137
|
+
id: '43',
|
62
138
|
type: 'posts',
|
63
139
|
attributes: {
|
64
|
-
title: '
|
140
|
+
title: 'Hello!!',
|
141
|
+
body: 'Hello, world!!'
|
65
142
|
},
|
66
143
|
relationships: {
|
67
|
-
comments: { data: [
|
144
|
+
comments: { data: [] },
|
68
145
|
blog: { data: { type: 'blogs', id: '999' } },
|
69
|
-
author: { data:
|
70
|
-
}
|
71
|
-
}]
|
72
|
-
assert_equal expected, @adapter.serializable_hash[:included]
|
73
|
-
end
|
74
|
-
|
75
|
-
def test_include_nil_author
|
76
|
-
serializer = PostSerializer.new(@anonymous_post)
|
77
|
-
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
78
|
-
|
79
|
-
assert_equal({ comments: { data: [] }, blog: { data: { type: 'blogs', id: '999' } }, author: { data: nil } }, adapter.serializable_hash[:data][:relationships])
|
80
|
-
end
|
81
|
-
|
82
|
-
def test_include_type_for_association_when_different_than_name
|
83
|
-
serializer = BlogSerializer.new(@blog)
|
84
|
-
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
85
|
-
relationships = adapter.serializable_hash[:data][:relationships]
|
86
|
-
expected = {
|
87
|
-
writer: {
|
88
|
-
data: {
|
89
|
-
type: 'authors',
|
90
|
-
id: '1'
|
91
|
-
}
|
92
|
-
},
|
93
|
-
articles: {
|
94
|
-
data: [
|
95
|
-
{
|
96
|
-
type: 'posts',
|
97
|
-
id: '42'
|
98
|
-
},
|
99
|
-
{
|
100
|
-
type: 'posts',
|
101
|
-
id: '43'
|
102
|
-
}
|
103
|
-
]
|
146
|
+
author: { data: nil }
|
104
147
|
}
|
105
148
|
}
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
def test_include_linked_resources_with_type_name
|
110
|
-
serializer = BlogSerializer.new(@blog)
|
111
|
-
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer, include: [:writer, :articles])
|
112
|
-
linked = adapter.serializable_hash[:included]
|
113
|
-
expected = [
|
114
|
-
{
|
115
|
-
id: '1',
|
116
|
-
type: 'authors',
|
117
|
-
attributes: {
|
118
|
-
name: 'Steve K.'
|
119
|
-
},
|
120
|
-
relationships: {
|
121
|
-
posts: { data: [] },
|
122
|
-
roles: { data: [] },
|
123
|
-
bio: { data: nil }
|
124
|
-
}
|
125
|
-
}, {
|
126
|
-
id: '42',
|
127
|
-
type: 'posts',
|
128
|
-
attributes: {
|
129
|
-
title: 'New Post',
|
130
|
-
body: 'Body'
|
131
|
-
},
|
132
|
-
relationships: {
|
133
|
-
comments: { data: [{ type: 'comments', id: '1' }] },
|
134
|
-
blog: { data: { type: 'blogs', id: '999' } },
|
135
|
-
author: { data: { type: 'authors', id: '1' } }
|
136
|
-
}
|
137
|
-
}, {
|
138
|
-
id: '43',
|
139
|
-
type: 'posts',
|
140
|
-
attributes: {
|
141
|
-
title: 'Hello!!',
|
142
|
-
body: 'Hello, world!!'
|
143
|
-
},
|
144
|
-
relationships: {
|
145
|
-
comments: { data: [] },
|
146
|
-
blog: { data: { type: 'blogs', id: '999' } },
|
147
|
-
author: { data: nil }
|
148
|
-
}
|
149
|
-
}
|
150
|
-
]
|
151
|
-
assert_equal expected, linked
|
152
|
-
end
|
149
|
+
]
|
150
|
+
assert_equal expected, linked
|
153
151
|
end
|
154
152
|
end
|
155
153
|
end
|