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,95 +1,93 @@
|
|
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
|
-
@author.posts = [@first_post, @second_post]
|
3
|
+
module ActiveModelSerializers
|
4
|
+
module Adapter
|
5
|
+
class JsonApi
|
6
|
+
class CollectionTest < ActiveSupport::TestCase
|
7
|
+
def setup
|
8
|
+
@author = Author.new(id: 1, name: 'Steve K.')
|
9
|
+
@author.bio = nil
|
10
|
+
@blog = Blog.new(id: 23, name: 'AMS Blog')
|
11
|
+
@first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
|
12
|
+
@second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
|
13
|
+
@first_post.comments = []
|
14
|
+
@second_post.comments = []
|
15
|
+
@first_post.blog = @blog
|
16
|
+
@second_post.blog = nil
|
17
|
+
@first_post.author = @author
|
18
|
+
@second_post.author = @author
|
19
|
+
@author.posts = [@first_post, @second_post]
|
21
20
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
21
|
+
@serializer = ActiveModel::Serializer::CollectionSerializer.new([@first_post, @second_post])
|
22
|
+
@adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer)
|
23
|
+
ActionController::Base.cache_store.clear
|
24
|
+
end
|
26
25
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
26
|
+
def test_include_multiple_posts
|
27
|
+
expected = [
|
28
|
+
{
|
29
|
+
id: '1',
|
30
|
+
type: 'posts',
|
31
|
+
attributes: {
|
32
|
+
title: 'Hello!!',
|
33
|
+
body: 'Hello, world!!'
|
34
|
+
},
|
35
|
+
relationships: {
|
36
|
+
comments: { data: [] },
|
37
|
+
blog: { data: { type: 'blogs', id: '999' } },
|
38
|
+
author: { data: { type: 'authors', id: '1' } }
|
39
|
+
}
|
40
|
+
},
|
41
|
+
{
|
42
|
+
id: '2',
|
43
|
+
type: 'posts',
|
44
|
+
attributes: {
|
45
|
+
title: 'New Post',
|
46
|
+
body: 'Body'
|
41
47
|
},
|
42
|
-
{
|
43
|
-
|
44
|
-
type: '
|
45
|
-
|
46
|
-
title: 'New Post',
|
47
|
-
body: 'Body'
|
48
|
-
},
|
49
|
-
relationships: {
|
50
|
-
comments: { data: [] },
|
51
|
-
blog: { data: { type: 'blogs', id: '999' } },
|
52
|
-
author: { data: { type: 'authors', id: '1' } }
|
53
|
-
}
|
48
|
+
relationships: {
|
49
|
+
comments: { data: [] },
|
50
|
+
blog: { data: { type: 'blogs', id: '999' } },
|
51
|
+
author: { data: { type: 'authors', id: '1' } }
|
54
52
|
}
|
55
|
-
|
53
|
+
}
|
54
|
+
]
|
56
55
|
|
57
|
-
|
58
|
-
|
56
|
+
assert_equal(expected, @adapter.serializable_hash[:data])
|
57
|
+
end
|
59
58
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
59
|
+
def test_limiting_fields
|
60
|
+
actual = ActiveModelSerializers::SerializableResource.new(
|
61
|
+
[@first_post, @second_post], adapter: :json_api,
|
62
|
+
fields: { posts: %w(title comments blog author) })
|
63
|
+
.serializable_hash
|
64
|
+
expected = [
|
65
|
+
{
|
66
|
+
id: '1',
|
67
|
+
type: 'posts',
|
68
|
+
attributes: {
|
69
|
+
title: 'Hello!!'
|
70
|
+
},
|
71
|
+
relationships: {
|
72
|
+
comments: { data: [] },
|
73
|
+
blog: { data: { type: 'blogs', id: '999' } },
|
74
|
+
author: { data: { type: 'authors', id: '1' } }
|
75
|
+
}
|
76
|
+
},
|
77
|
+
{
|
78
|
+
id: '2',
|
79
|
+
type: 'posts',
|
80
|
+
attributes: {
|
81
|
+
title: 'New Post'
|
77
82
|
},
|
78
|
-
{
|
79
|
-
|
80
|
-
type: '
|
81
|
-
|
82
|
-
title: 'New Post'
|
83
|
-
},
|
84
|
-
relationships: {
|
85
|
-
comments: { data: [] },
|
86
|
-
blog: { data: { type: 'blogs', id: '999' } },
|
87
|
-
author: { data: { type: 'authors', id: '1' } }
|
88
|
-
}
|
83
|
+
relationships: {
|
84
|
+
comments: { data: [] },
|
85
|
+
blog: { data: { type: 'blogs', id: '999' } },
|
86
|
+
author: { data: { type: 'authors', id: '1' } }
|
89
87
|
}
|
90
|
-
|
91
|
-
|
92
|
-
|
88
|
+
}
|
89
|
+
]
|
90
|
+
assert_equal(expected, actual[:data])
|
93
91
|
end
|
94
92
|
end
|
95
93
|
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModelSerializers
|
4
|
+
module Adapter
|
5
|
+
class JsonApi < Base
|
6
|
+
class ErrorsTest < Minitest::Test
|
7
|
+
include ActiveModel::Serializer::Lint::Tests
|
8
|
+
|
9
|
+
def setup
|
10
|
+
@resource = ModelWithErrors.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_active_model_with_error
|
14
|
+
options = {
|
15
|
+
serializer: ActiveModel::Serializer::ErrorSerializer,
|
16
|
+
adapter: :json_api
|
17
|
+
}
|
18
|
+
|
19
|
+
@resource.errors.add(:name, 'cannot be nil')
|
20
|
+
|
21
|
+
serializable_resource = ActiveModelSerializers::SerializableResource.new(@resource, options)
|
22
|
+
assert_equal serializable_resource.serializer_instance.attributes, {}
|
23
|
+
assert_equal serializable_resource.serializer_instance.object, @resource
|
24
|
+
|
25
|
+
expected_errors_object =
|
26
|
+
{ :errors =>
|
27
|
+
[
|
28
|
+
{
|
29
|
+
source: { pointer: '/data/attributes/name' },
|
30
|
+
detail: 'cannot be nil'
|
31
|
+
}
|
32
|
+
]
|
33
|
+
}
|
34
|
+
assert_equal serializable_resource.as_json, expected_errors_object
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_active_model_with_multiple_errors
|
38
|
+
options = {
|
39
|
+
serializer: ActiveModel::Serializer::ErrorSerializer,
|
40
|
+
adapter: :json_api
|
41
|
+
}
|
42
|
+
|
43
|
+
@resource.errors.add(:name, 'cannot be nil')
|
44
|
+
@resource.errors.add(:name, 'must be longer')
|
45
|
+
@resource.errors.add(:id, 'must be a uuid')
|
46
|
+
|
47
|
+
serializable_resource = ActiveModelSerializers::SerializableResource.new(@resource, options)
|
48
|
+
assert_equal serializable_resource.serializer_instance.attributes, {}
|
49
|
+
assert_equal serializable_resource.serializer_instance.object, @resource
|
50
|
+
|
51
|
+
expected_errors_object =
|
52
|
+
{ :errors =>
|
53
|
+
[
|
54
|
+
{ :source => { :pointer => '/data/attributes/name' }, :detail => 'cannot be nil' },
|
55
|
+
{ :source => { :pointer => '/data/attributes/name' }, :detail => 'must be longer' },
|
56
|
+
{ :source => { :pointer => '/data/attributes/id' }, :detail => 'must be a uuid' }
|
57
|
+
]
|
58
|
+
}
|
59
|
+
assert_equal serializable_resource.as_json, expected_errors_object
|
60
|
+
end
|
61
|
+
|
62
|
+
# see http://jsonapi.org/examples/
|
63
|
+
def test_parameter_source_type_error
|
64
|
+
parameter = 'auther'
|
65
|
+
error_source = ActiveModelSerializers::Adapter::JsonApi::Error.error_source(:parameter, parameter)
|
66
|
+
assert_equal({ parameter: parameter }, error_source)
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_unknown_source_type_error
|
70
|
+
value = 'auther'
|
71
|
+
assert_raises(ActiveModelSerializers::Adapter::JsonApi::Error::UnknownSourceTypeError) do
|
72
|
+
ActiveModelSerializers::Adapter::JsonApi::Error.error_source(:hyper, value)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -1,87 +1,85 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
class
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
end
|
3
|
+
module ActiveModelSerializers
|
4
|
+
module Adapter
|
5
|
+
class JsonApi
|
6
|
+
class FieldsTest < ActiveSupport::TestCase
|
7
|
+
Post = Class.new(::Model)
|
8
|
+
class PostSerializer < ActiveModel::Serializer
|
9
|
+
type 'posts'
|
10
|
+
attributes :title, :body
|
11
|
+
belongs_to :author
|
12
|
+
has_many :comments
|
13
|
+
end
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
15
|
+
Author = Class.new(::Model)
|
16
|
+
class AuthorSerializer < ActiveModel::Serializer
|
17
|
+
type 'authors'
|
18
|
+
attributes :name, :birthday
|
19
|
+
end
|
21
20
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
21
|
+
Comment = Class.new(::Model)
|
22
|
+
class CommentSerializer < ActiveModel::Serializer
|
23
|
+
type 'comments'
|
24
|
+
attributes :body
|
25
|
+
belongs_to :author
|
26
|
+
end
|
28
27
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
28
|
+
def setup
|
29
|
+
@author = Author.new(id: 1, name: 'Lucas', birthday: '10.01.1990')
|
30
|
+
@comment1 = Comment.new(id: 7, body: 'cool', author: @author)
|
31
|
+
@comment2 = Comment.new(id: 12, body: 'awesome', author: @author)
|
32
|
+
@post = Post.new(id: 1337, title: 'Title 1', body: 'Body 1',
|
33
|
+
author: @author, comments: [@comment1, @comment2])
|
34
|
+
@comment1.post = @post
|
35
|
+
@comment2.post = @post
|
36
|
+
end
|
38
37
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
38
|
+
def test_fields_attributes
|
39
|
+
fields = { posts: [:title] }
|
40
|
+
hash = serializable(@post, adapter: :json_api, fields: fields).serializable_hash
|
41
|
+
expected = {
|
42
|
+
title: 'Title 1'
|
43
|
+
}
|
45
44
|
|
46
|
-
|
47
|
-
|
45
|
+
assert_equal(expected, hash[:data][:attributes])
|
46
|
+
end
|
48
47
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
}
|
48
|
+
def test_fields_relationships
|
49
|
+
fields = { posts: [:author] }
|
50
|
+
hash = serializable(@post, adapter: :json_api, fields: fields).serializable_hash
|
51
|
+
expected = {
|
52
|
+
author: {
|
53
|
+
data: {
|
54
|
+
type: 'authors',
|
55
|
+
id: '1'
|
58
56
|
}
|
59
57
|
}
|
58
|
+
}
|
60
59
|
|
61
|
-
|
62
|
-
|
60
|
+
assert_equal(expected, hash[:data][:relationships])
|
61
|
+
end
|
63
62
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
}
|
74
|
-
}, {
|
75
|
-
type: 'comments',
|
76
|
-
id: '12',
|
77
|
-
attributes: {
|
78
|
-
body: 'awesome'
|
79
|
-
}
|
63
|
+
def test_fields_included
|
64
|
+
fields = { posts: [:author], comments: [:body] }
|
65
|
+
hash = serializable(@post, adapter: :json_api, fields: fields, include: 'comments').serializable_hash
|
66
|
+
expected = [
|
67
|
+
{
|
68
|
+
type: 'comments',
|
69
|
+
id: '7',
|
70
|
+
attributes: {
|
71
|
+
body: 'cool'
|
80
72
|
}
|
81
|
-
|
73
|
+
}, {
|
74
|
+
type: 'comments',
|
75
|
+
id: '12',
|
76
|
+
attributes: {
|
77
|
+
body: 'awesome'
|
78
|
+
}
|
79
|
+
}
|
80
|
+
]
|
82
81
|
|
83
|
-
|
84
|
-
end
|
82
|
+
assert_equal(expected, hash[:included])
|
85
83
|
end
|
86
84
|
end
|
87
85
|
end
|
@@ -1,43 +1,41 @@
|
|
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
|
-
@second_post.blog = nil
|
3
|
+
module ActiveModelSerializers
|
4
|
+
module Adapter
|
5
|
+
class JsonApi
|
6
|
+
class HasManyEmbedIdsTest < ActiveSupport::TestCase
|
7
|
+
def setup
|
8
|
+
@author = Author.new(name: 'Steve K.')
|
9
|
+
@author.bio = nil
|
10
|
+
@author.roles = nil
|
11
|
+
@first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
|
12
|
+
@second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
|
13
|
+
@author.posts = [@first_post, @second_post]
|
14
|
+
@first_post.author = @author
|
15
|
+
@second_post.author = @author
|
16
|
+
@first_post.comments = []
|
17
|
+
@second_post.comments = []
|
18
|
+
@blog = Blog.new(id: 23, name: 'AMS Blog')
|
19
|
+
@first_post.blog = @blog
|
20
|
+
@second_post.blog = nil
|
22
21
|
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
@serializer = AuthorSerializer.new(@author)
|
23
|
+
@adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer)
|
24
|
+
end
|
26
25
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
26
|
+
def test_includes_comment_ids
|
27
|
+
expected = {
|
28
|
+
data: [
|
29
|
+
{ type: 'posts', id: '1' },
|
30
|
+
{ type: 'posts', id: '2' }
|
31
|
+
]
|
32
|
+
}
|
34
33
|
|
35
|
-
|
36
|
-
|
34
|
+
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:posts])
|
35
|
+
end
|
37
36
|
|
38
|
-
|
39
|
-
|
40
|
-
end
|
37
|
+
def test_no_includes_linked_comments
|
38
|
+
assert_nil @adapter.serializable_hash[:linked]
|
41
39
|
end
|
42
40
|
end
|
43
41
|
end
|