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.
Files changed (179) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ISSUE_TEMPLATE.md +29 -0
  3. data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
  4. data/.gitignore +1 -0
  5. data/.rubocop.yml +19 -1
  6. data/.rubocop_todo.yml +30 -103
  7. data/.simplecov +0 -1
  8. data/.travis.yml +20 -8
  9. data/CHANGELOG.md +89 -5
  10. data/CONTRIBUTING.md +54 -179
  11. data/Gemfile +7 -2
  12. data/{LICENSE.txt → MIT-LICENSE} +0 -0
  13. data/README.md +27 -5
  14. data/Rakefile +44 -16
  15. data/active_model_serializers.gemspec +9 -1
  16. data/appveyor.yml +1 -0
  17. data/bin/bench +171 -0
  18. data/bin/bench_regression +316 -0
  19. data/bin/serve_benchmark +39 -0
  20. data/docs/ARCHITECTURE.md +13 -7
  21. data/docs/README.md +5 -1
  22. data/docs/STYLE.md +58 -0
  23. data/docs/general/adapters.md +99 -16
  24. data/docs/general/configuration_options.md +87 -14
  25. data/docs/general/deserialization.md +100 -0
  26. data/docs/general/getting_started.md +35 -0
  27. data/docs/general/instrumentation.md +1 -1
  28. data/docs/general/key_transforms.md +40 -0
  29. data/docs/general/rendering.md +115 -13
  30. data/docs/general/serializers.md +138 -6
  31. data/docs/howto/add_pagination_links.md +36 -18
  32. data/docs/howto/outside_controller_use.md +4 -4
  33. data/docs/howto/passing_arbitrary_options.md +27 -0
  34. data/docs/jsonapi/errors.md +56 -0
  35. data/docs/jsonapi/schema.md +29 -18
  36. data/docs/rfcs/0000-namespace.md +106 -0
  37. data/docs/rfcs/template.md +15 -0
  38. data/lib/action_controller/serialization.rb +10 -19
  39. data/lib/active_model/serializable_resource.rb +4 -65
  40. data/lib/active_model/serializer.rb +73 -18
  41. data/lib/active_model/serializer/adapter.rb +15 -82
  42. data/lib/active_model/serializer/adapter/attributes.rb +5 -56
  43. data/lib/active_model/serializer/adapter/base.rb +5 -47
  44. data/lib/active_model/serializer/adapter/json.rb +6 -12
  45. data/lib/active_model/serializer/adapter/json_api.rb +5 -213
  46. data/lib/active_model/serializer/adapter/null.rb +7 -3
  47. data/lib/active_model/serializer/array_serializer.rb +3 -3
  48. data/lib/active_model/serializer/association.rb +4 -5
  49. data/lib/active_model/serializer/attributes.rb +1 -1
  50. data/lib/active_model/serializer/caching.rb +56 -5
  51. data/lib/active_model/serializer/collection_serializer.rb +30 -13
  52. data/lib/active_model/serializer/configuration.rb +7 -0
  53. data/lib/active_model/serializer/error_serializer.rb +10 -0
  54. data/lib/active_model/serializer/errors_serializer.rb +27 -0
  55. data/lib/active_model/serializer/links.rb +4 -2
  56. data/lib/active_model/serializer/lint.rb +14 -0
  57. data/lib/active_model/serializer/meta.rb +29 -0
  58. data/lib/active_model/serializer/null.rb +17 -0
  59. data/lib/active_model/serializer/reflection.rb +57 -1
  60. data/lib/active_model/serializer/type.rb +1 -1
  61. data/lib/active_model/serializer/version.rb +1 -1
  62. data/lib/active_model_serializers.rb +17 -0
  63. data/lib/active_model_serializers/adapter.rb +92 -0
  64. data/lib/active_model_serializers/adapter/attributes.rb +94 -0
  65. data/lib/active_model_serializers/adapter/base.rb +90 -0
  66. data/lib/active_model_serializers/adapter/json.rb +11 -0
  67. data/lib/active_model_serializers/adapter/json_api.rb +513 -0
  68. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +213 -0
  69. data/lib/active_model_serializers/adapter/json_api/error.rb +96 -0
  70. data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +49 -0
  71. data/lib/active_model_serializers/adapter/json_api/link.rb +83 -0
  72. data/lib/active_model_serializers/adapter/json_api/meta.rb +37 -0
  73. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +57 -0
  74. data/lib/active_model_serializers/adapter/json_api/relationship.rb +52 -0
  75. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +37 -0
  76. data/lib/active_model_serializers/adapter/null.rb +10 -0
  77. data/lib/active_model_serializers/cached_serializer.rb +87 -0
  78. data/lib/active_model_serializers/callbacks.rb +1 -1
  79. data/lib/active_model_serializers/deprecate.rb +55 -0
  80. data/lib/active_model_serializers/deserialization.rb +2 -2
  81. data/lib/active_model_serializers/fragment_cache.rb +118 -0
  82. data/lib/active_model_serializers/json_pointer.rb +14 -0
  83. data/lib/active_model_serializers/key_transform.rb +70 -0
  84. data/lib/active_model_serializers/logging.rb +4 -1
  85. data/lib/active_model_serializers/model.rb +11 -1
  86. data/lib/active_model_serializers/railtie.rb +9 -1
  87. data/lib/active_model_serializers/register_jsonapi_renderer.rb +64 -0
  88. data/lib/active_model_serializers/serializable_resource.rb +81 -0
  89. data/lib/active_model_serializers/serialization_context.rb +24 -2
  90. data/lib/active_model_serializers/test/schema.rb +2 -2
  91. data/lib/grape/formatters/active_model_serializers.rb +1 -1
  92. data/test/action_controller/adapter_selector_test.rb +1 -1
  93. data/test/action_controller/json_api/deserialization_test.rb +56 -3
  94. data/test/action_controller/json_api/errors_test.rb +41 -0
  95. data/test/action_controller/json_api/linked_test.rb +10 -9
  96. data/test/action_controller/json_api/pagination_test.rb +2 -2
  97. data/test/action_controller/json_api/transform_test.rb +180 -0
  98. data/test/action_controller/serialization_scope_name_test.rb +201 -35
  99. data/test/action_controller/serialization_test.rb +39 -7
  100. data/test/active_model_serializers/adapter_for_test.rb +208 -0
  101. data/test/active_model_serializers/cached_serializer_test.rb +80 -0
  102. data/test/active_model_serializers/fragment_cache_test.rb +34 -0
  103. data/test/active_model_serializers/json_pointer_test.rb +20 -0
  104. data/test/active_model_serializers/key_transform_test.rb +263 -0
  105. data/test/active_model_serializers/logging_test.rb +8 -8
  106. data/test/active_model_serializers/railtie_test_isolated.rb +6 -0
  107. data/test/active_model_serializers/serialization_context_test_isolated.rb +58 -0
  108. data/test/adapter/deprecation_test.rb +100 -0
  109. data/test/adapter/json/belongs_to_test.rb +32 -34
  110. data/test/adapter/json/collection_test.rb +73 -75
  111. data/test/adapter/json/has_many_test.rb +36 -38
  112. data/test/adapter/json/transform_test.rb +93 -0
  113. data/test/adapter/json_api/belongs_to_test.rb +127 -129
  114. data/test/adapter/json_api/collection_test.rb +80 -82
  115. data/test/adapter/json_api/errors_test.rb +78 -0
  116. data/test/adapter/json_api/fields_test.rb +68 -70
  117. data/test/adapter/json_api/has_many_embed_ids_test.rb +32 -34
  118. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +75 -77
  119. data/test/adapter/json_api/has_many_test.rb +121 -123
  120. data/test/adapter/json_api/has_one_test.rb +59 -61
  121. data/test/adapter/json_api/json_api_test.rb +28 -30
  122. data/test/adapter/json_api/linked_test.rb +319 -321
  123. data/test/adapter/json_api/links_test.rb +75 -50
  124. data/test/adapter/json_api/pagination_links_test.rb +115 -82
  125. data/test/adapter/json_api/parse_test.rb +114 -116
  126. data/test/adapter/json_api/relationship_test.rb +161 -0
  127. data/test/adapter/json_api/relationships_test.rb +199 -0
  128. data/test/adapter/json_api/resource_identifier_test.rb +85 -0
  129. data/test/adapter/json_api/resource_meta_test.rb +100 -0
  130. data/test/adapter/json_api/toplevel_jsonapi_test.rb +61 -63
  131. data/test/adapter/json_api/transform_test.rb +500 -0
  132. data/test/adapter/json_api/type_test.rb +61 -0
  133. data/test/adapter/json_test.rb +35 -37
  134. data/test/adapter/null_test.rb +13 -15
  135. data/test/adapter/polymorphic_test.rb +72 -0
  136. data/test/adapter_test.rb +27 -29
  137. data/test/array_serializer_test.rb +7 -8
  138. data/test/benchmark/app.rb +65 -0
  139. data/test/benchmark/benchmarking_support.rb +67 -0
  140. data/test/benchmark/bm_caching.rb +117 -0
  141. data/test/benchmark/bm_transform.rb +34 -0
  142. data/test/benchmark/config.ru +3 -0
  143. data/test/benchmark/controllers.rb +77 -0
  144. data/test/benchmark/fixtures.rb +167 -0
  145. data/test/cache_test.rb +388 -0
  146. data/test/collection_serializer_test.rb +10 -0
  147. data/test/fixtures/active_record.rb +12 -0
  148. data/test/fixtures/poro.rb +28 -3
  149. data/test/grape_test.rb +5 -5
  150. data/test/lint_test.rb +9 -0
  151. data/test/serializable_resource_test.rb +59 -3
  152. data/test/serializers/associations_test.rb +8 -8
  153. data/test/serializers/attribute_test.rb +7 -7
  154. data/test/serializers/caching_configuration_test_isolated.rb +170 -0
  155. data/test/serializers/meta_test.rb +74 -6
  156. data/test/serializers/read_attribute_for_serialization_test.rb +79 -0
  157. data/test/serializers/serialization_test.rb +55 -0
  158. data/test/support/isolated_unit.rb +3 -0
  159. data/test/support/rails5_shims.rb +26 -8
  160. data/test/support/rails_app.rb +38 -18
  161. data/test/support/serialization_testing.rb +5 -5
  162. data/test/test_helper.rb +6 -10
  163. metadata +132 -37
  164. data/docs/DESIGN.textile +7 -1
  165. data/lib/active_model/serializer/adapter/cached_serializer.rb +0 -45
  166. data/lib/active_model/serializer/adapter/fragment_cache.rb +0 -111
  167. data/lib/active_model/serializer/adapter/json/fragment_cache.rb +0 -13
  168. data/lib/active_model/serializer/adapter/json_api/deserialization.rb +0 -207
  169. data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +0 -21
  170. data/lib/active_model/serializer/adapter/json_api/link.rb +0 -44
  171. data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +0 -58
  172. data/test/active_model_serializers/serialization_context_test.rb +0 -18
  173. data/test/adapter/fragment_cache_test.rb +0 -38
  174. data/test/adapter/json_api/resource_type_config_test.rb +0 -71
  175. data/test/serializers/adapter_for_test.rb +0 -166
  176. data/test/serializers/cache_test.rb +0 -209
  177. data/test/support/simplecov.rb +0 -6
  178. data/test/support/stream_capture.rb +0 -50
  179. data/test/support/test_case.rb +0 -19
@@ -1,96 +1,94 @@
1
1
  require 'test_helper'
2
2
 
3
- module ActiveModel
4
- class Serializer
5
- module Adapter
6
- class JsonApi
7
- # Test 'has_many :assocs, serializer: AssocXSerializer'
8
- class HasManyExplicitSerializerTest < ActiveSupport::TestCase
9
- def setup
10
- @post = Post.new(title: 'New Post', body: 'Body')
11
- @author = Author.new(name: 'Jane Blogger')
12
- @author.posts = [@post]
13
- @post.author = @author
14
- @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
15
- @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
16
- @post.comments = [@first_comment, @second_comment]
17
- @first_comment.post = @post
18
- @first_comment.author = nil
19
- @second_comment.post = @post
20
- @second_comment.author = nil
21
- @blog = Blog.new(id: 23, name: 'AMS Blog')
22
- @post.blog = @blog
3
+ module ActiveModelSerializers
4
+ module Adapter
5
+ class JsonApi
6
+ # Test 'has_many :assocs, serializer: AssocXSerializer'
7
+ class HasManyExplicitSerializerTest < ActiveSupport::TestCase
8
+ def setup
9
+ @post = Post.new(title: 'New Post', body: 'Body')
10
+ @author = Author.new(name: 'Jane Blogger')
11
+ @author.posts = [@post]
12
+ @post.author = @author
13
+ @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
14
+ @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
15
+ @post.comments = [@first_comment, @second_comment]
16
+ @first_comment.post = @post
17
+ @first_comment.author = nil
18
+ @second_comment.post = @post
19
+ @second_comment.author = nil
20
+ @blog = Blog.new(id: 23, name: 'AMS Blog')
21
+ @post.blog = @blog
23
22
 
24
- @serializer = PostPreviewSerializer.new(@post)
25
- @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
26
- @serializer,
27
- include: [:comments, :author]
28
- )
29
- end
23
+ @serializer = PostPreviewSerializer.new(@post)
24
+ @adapter = ActiveModelSerializers::Adapter::JsonApi.new(
25
+ @serializer,
26
+ include: [:comments, :author]
27
+ )
28
+ end
30
29
 
31
- def test_includes_comment_ids
32
- expected = {
33
- data: [
34
- { type: 'comments', id: '1' },
35
- { type: 'comments', id: '2' }
36
- ]
37
- }
30
+ def test_includes_comment_ids
31
+ expected = {
32
+ data: [
33
+ { type: 'comments', id: '1' },
34
+ { type: 'comments', id: '2' }
35
+ ]
36
+ }
38
37
 
39
- assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
40
- end
38
+ assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
39
+ end
41
40
 
42
- def test_includes_linked_data
43
- # If CommentPreviewSerializer is applied correctly the body text will not be present in the output
44
- expected = [
45
- {
46
- id: '1',
47
- type: 'comments',
48
- relationships: {
49
- post: { data: { type: 'posts', id: @post.id.to_s } }
50
- }
51
- },
52
- {
53
- id: '2',
54
- type: 'comments',
55
- relationships: {
56
- post: { data: { type: 'posts', id: @post.id.to_s } }
57
- }
58
- },
59
- {
60
- id: @author.id.to_s,
61
- type: 'authors',
62
- relationships: {
63
- posts: { data: [{ type: 'posts', id: @post.id.to_s }] }
64
- }
41
+ def test_includes_linked_data
42
+ # If CommentPreviewSerializer is applied correctly the body text will not be present in the output
43
+ expected = [
44
+ {
45
+ id: '1',
46
+ type: 'comments',
47
+ relationships: {
48
+ post: { data: { type: 'posts', id: @post.id.to_s } }
65
49
  }
66
- ]
50
+ },
51
+ {
52
+ id: '2',
53
+ type: 'comments',
54
+ relationships: {
55
+ post: { data: { type: 'posts', id: @post.id.to_s } }
56
+ }
57
+ },
58
+ {
59
+ id: @author.id.to_s,
60
+ type: 'authors',
61
+ relationships: {
62
+ posts: { data: [{ type: 'posts', id: @post.id.to_s }] }
63
+ }
64
+ }
65
+ ]
67
66
 
68
- assert_equal(expected, @adapter.serializable_hash[:included])
69
- end
67
+ assert_equal(expected, @adapter.serializable_hash[:included])
68
+ end
70
69
 
71
- def test_includes_author_id
72
- expected = {
73
- data: { type: 'authors', id: @author.id.to_s }
74
- }
70
+ def test_includes_author_id
71
+ expected = {
72
+ data: { type: 'authors', id: @author.id.to_s }
73
+ }
75
74
 
76
- assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:author])
77
- end
75
+ assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:author])
76
+ end
78
77
 
79
- def test_explicit_serializer_with_null_resource
80
- @post.author = nil
78
+ def test_explicit_serializer_with_null_resource
79
+ @post.author = nil
81
80
 
82
- expected = { data: nil }
81
+ expected = { data: nil }
83
82
 
84
- assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:author])
85
- end
83
+ assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:author])
84
+ end
86
85
 
87
- def test_explicit_serializer_with_null_collection
88
- @post.comments = []
86
+ def test_explicit_serializer_with_null_collection
87
+ @post.comments = []
89
88
 
90
- expected = { data: [] }
89
+ expected = { data: [] }
91
90
 
92
- assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
93
- end
91
+ assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
94
92
  end
95
93
  end
96
94
  end
@@ -1,143 +1,141 @@
1
1
  require 'test_helper'
2
2
 
3
- module ActiveModel
4
- class Serializer
5
- module Adapter
6
- class JsonApi
7
- class HasManyTest < ActiveSupport::TestCase
8
- def setup
9
- ActionController::Base.cache_store.clear
10
- @author = Author.new(id: 1, name: 'Steve K.')
11
- @author.posts = []
12
- @author.bio = nil
13
- @post = Post.new(id: 1, title: 'New Post', body: 'Body')
14
- @post_without_comments = Post.new(id: 2, title: 'Second Post', body: 'Second')
15
- @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
16
- @first_comment.author = nil
17
- @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
18
- @second_comment.author = nil
19
- @post.comments = [@first_comment, @second_comment]
20
- @post_without_comments.comments = []
21
- @first_comment.post = @post
22
- @second_comment.post = @post
23
- @post.author = @author
24
- @post_without_comments.author = nil
25
- @blog = Blog.new(id: 1, name: 'My Blog!!')
26
- @blog.writer = @author
27
- @blog.articles = [@post]
28
- @post.blog = @blog
29
- @post_without_comments.blog = nil
30
- @tag = Tag.new(id: 1, name: '#hash_tag')
31
- @post.tags = [@tag]
32
- @serializer = PostSerializer.new(@post)
33
- @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
3
+ module ActiveModelSerializers
4
+ module Adapter
5
+ class JsonApi
6
+ class HasManyTest < ActiveSupport::TestCase
7
+ def setup
8
+ ActionController::Base.cache_store.clear
9
+ @author = Author.new(id: 1, name: 'Steve K.')
10
+ @author.posts = []
11
+ @author.bio = nil
12
+ @post = Post.new(id: 1, title: 'New Post', body: 'Body')
13
+ @post_without_comments = Post.new(id: 2, title: 'Second Post', body: 'Second')
14
+ @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
15
+ @first_comment.author = nil
16
+ @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
17
+ @second_comment.author = nil
18
+ @post.comments = [@first_comment, @second_comment]
19
+ @post_without_comments.comments = []
20
+ @first_comment.post = @post
21
+ @second_comment.post = @post
22
+ @post.author = @author
23
+ @post_without_comments.author = nil
24
+ @blog = Blog.new(id: 1, name: 'My Blog!!')
25
+ @blog.writer = @author
26
+ @blog.articles = [@post]
27
+ @post.blog = @blog
28
+ @post_without_comments.blog = nil
29
+ @tag = Tag.new(id: 1, name: '#hash_tag')
30
+ @post.tags = [@tag]
31
+ @serializer = PostSerializer.new(@post)
32
+ @adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer)
34
33
 
35
- @virtual_value = VirtualValue.new(id: 1)
36
- end
34
+ @virtual_value = VirtualValue.new(id: 1)
35
+ end
37
36
 
38
- def test_includes_comment_ids
39
- expected = { data: [{ type: 'comments', id: '1' }, { type: 'comments', id: '2' }] }
37
+ def test_includes_comment_ids
38
+ expected = { data: [{ type: 'comments', id: '1' }, { type: 'comments', id: '2' }] }
40
39
 
41
- assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
42
- end
40
+ assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
41
+ end
43
42
 
44
- def test_includes_linked_comments
45
- @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: [:comments])
46
- expected = [{
47
- id: '1',
48
- type: 'comments',
49
- attributes: {
50
- body: 'ZOMG A COMMENT'
51
- },
52
- relationships: {
53
- post: { data: { type: 'posts', id: '1' } },
54
- author: { data: nil }
55
- }
56
- }, {
57
- id: '2',
58
- type: 'comments',
59
- attributes: {
60
- body: 'ZOMG ANOTHER COMMENT'
61
- },
62
- relationships: {
63
- post: { data: { type: 'posts', id: '1' } },
64
- author: { data: nil }
65
- }
66
- }]
67
- assert_equal expected, @adapter.serializable_hash[:included]
68
- end
43
+ def test_includes_linked_comments
44
+ @adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer, include: [:comments])
45
+ expected = [{
46
+ id: '1',
47
+ type: 'comments',
48
+ attributes: {
49
+ body: 'ZOMG A COMMENT'
50
+ },
51
+ relationships: {
52
+ post: { data: { type: 'posts', id: '1' } },
53
+ author: { data: nil }
54
+ }
55
+ }, {
56
+ id: '2',
57
+ type: 'comments',
58
+ attributes: {
59
+ body: 'ZOMG ANOTHER COMMENT'
60
+ },
61
+ relationships: {
62
+ post: { data: { type: 'posts', id: '1' } },
63
+ author: { data: nil }
64
+ }
65
+ }]
66
+ assert_equal expected, @adapter.serializable_hash[:included]
67
+ end
69
68
 
70
- def test_limit_fields_of_linked_comments
71
- @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: [:comments], fields: { comment: [:id, :post, :author] })
72
- expected = [{
73
- id: '1',
74
- type: 'comments',
75
- relationships: {
76
- post: { data: { type: 'posts', id: '1' } },
77
- author: { data: nil }
78
- }
79
- }, {
80
- id: '2',
81
- type: 'comments',
82
- relationships: {
83
- post: { data: { type: 'posts', id: '1' } },
84
- author: { data: nil }
85
- }
86
- }]
87
- assert_equal expected, @adapter.serializable_hash[:included]
88
- end
69
+ def test_limit_fields_of_linked_comments
70
+ @adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer, include: [:comments], fields: { comment: [:id, :post, :author] })
71
+ expected = [{
72
+ id: '1',
73
+ type: 'comments',
74
+ relationships: {
75
+ post: { data: { type: 'posts', id: '1' } },
76
+ author: { data: nil }
77
+ }
78
+ }, {
79
+ id: '2',
80
+ type: 'comments',
81
+ relationships: {
82
+ post: { data: { type: 'posts', id: '1' } },
83
+ author: { data: nil }
84
+ }
85
+ }]
86
+ assert_equal expected, @adapter.serializable_hash[:included]
87
+ end
89
88
 
90
- def test_no_include_linked_if_comments_is_empty
91
- serializer = PostSerializer.new(@post_without_comments)
92
- adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
89
+ def test_no_include_linked_if_comments_is_empty
90
+ serializer = PostSerializer.new(@post_without_comments)
91
+ adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
93
92
 
94
- assert_nil adapter.serializable_hash[:linked]
95
- end
93
+ assert_nil adapter.serializable_hash[:linked]
94
+ end
96
95
 
97
- def test_include_type_for_association_when_different_than_name
98
- serializer = BlogSerializer.new(@blog)
99
- adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
100
- actual = adapter.serializable_hash[:data][:relationships][:articles]
96
+ def test_include_type_for_association_when_different_than_name
97
+ serializer = BlogSerializer.new(@blog)
98
+ adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
99
+ actual = adapter.serializable_hash[:data][:relationships][:articles]
101
100
 
102
- expected = {
103
- data: [{
104
- type: 'posts',
105
- id: '1'
106
- }]
107
- }
108
- assert_equal expected, actual
109
- end
101
+ expected = {
102
+ data: [{
103
+ type: 'posts',
104
+ id: '1'
105
+ }]
106
+ }
107
+ assert_equal expected, actual
108
+ end
110
109
 
111
- def test_has_many_with_no_serializer
112
- serializer = PostWithTagsSerializer.new(@post)
113
- adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
110
+ def test_has_many_with_no_serializer
111
+ serializer = PostWithTagsSerializer.new(@post)
112
+ adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
114
113
 
115
- assert_equal({
116
- data: {
117
- id: '1',
118
- type: 'posts',
119
- relationships: {
120
- tags: { data: [@tag.as_json] }
121
- }
114
+ assert_equal({
115
+ data: {
116
+ id: '1',
117
+ type: 'posts',
118
+ relationships: {
119
+ tags: { data: [@tag.as_json] }
122
120
  }
123
- }, adapter.serializable_hash)
124
- end
121
+ }
122
+ }, adapter.serializable_hash)
123
+ end
125
124
 
126
- def test_has_many_with_virtual_value
127
- serializer = VirtualValueSerializer.new(@virtual_value)
128
- adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
125
+ def test_has_many_with_virtual_value
126
+ serializer = VirtualValueSerializer.new(@virtual_value)
127
+ adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
129
128
 
130
- assert_equal({
131
- data: {
132
- id: '1',
133
- type: 'virtual_values',
134
- relationships: {
135
- maker: { data: { id: 1 } },
136
- reviews: { data: [{ id: 1 }, { id: 2 }] }
137
- }
129
+ assert_equal({
130
+ data: {
131
+ id: '1',
132
+ type: 'virtual-values',
133
+ relationships: {
134
+ maker: { data: { id: 1 } },
135
+ reviews: { data: [{ id: 1 }, { id: 2 }] }
138
136
  }
139
- }, adapter.serializable_hash)
140
- end
137
+ }
138
+ }, adapter.serializable_hash)
141
139
  end
142
140
  end
143
141
  end
@@ -1,79 +1,77 @@
1
1
  require 'test_helper'
2
2
 
3
- module ActiveModel
4
- class Serializer
5
- module Adapter
6
- class JsonApi
7
- class HasOneTest < ActiveSupport::TestCase
8
- def setup
9
- @author = Author.new(id: 1, name: 'Steve K.')
10
- @bio = Bio.new(id: 43, content: 'AMS Contributor')
11
- @author.bio = @bio
12
- @bio.author = @author
13
- @post = Post.new(id: 42, title: 'New Post', body: 'Body')
14
- @anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
15
- @comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
16
- @post.comments = [@comment]
17
- @anonymous_post.comments = []
18
- @comment.post = @post
19
- @comment.author = nil
20
- @post.author = @author
21
- @anonymous_post.author = nil
22
- @blog = Blog.new(id: 1, name: 'My Blog!!')
23
- @blog.writer = @author
24
- @blog.articles = [@post, @anonymous_post]
25
- @author.posts = []
26
- @author.roles = []
3
+ module ActiveModelSerializers
4
+ module Adapter
5
+ class JsonApi
6
+ class HasOneTest < ActiveSupport::TestCase
7
+ def setup
8
+ @author = Author.new(id: 1, name: 'Steve K.')
9
+ @bio = Bio.new(id: 43, content: 'AMS Contributor')
10
+ @author.bio = @bio
11
+ @bio.author = @author
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
+ @anonymous_post.comments = []
17
+ @comment.post = @post
18
+ @comment.author = nil
19
+ @post.author = @author
20
+ @anonymous_post.author = nil
21
+ @blog = Blog.new(id: 1, name: 'My Blog!!')
22
+ @blog.writer = @author
23
+ @blog.articles = [@post, @anonymous_post]
24
+ @author.posts = []
25
+ @author.roles = []
27
26
 
28
- @virtual_value = VirtualValue.new(id: 1)
27
+ @virtual_value = VirtualValue.new(id: 1)
29
28
 
30
- @serializer = AuthorSerializer.new(@author)
31
- @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: [:bio, :posts])
32
- end
29
+ @serializer = AuthorSerializer.new(@author)
30
+ @adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer, include: [:bio, :posts])
31
+ end
33
32
 
34
- def test_includes_bio_id
35
- expected = { data: { type: 'bios', id: '43' } }
33
+ def test_includes_bio_id
34
+ expected = { data: { type: 'bios', id: '43' } }
36
35
 
37
- assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:bio])
38
- end
36
+ assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:bio])
37
+ end
39
38
 
40
- def test_includes_linked_bio
41
- @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: [:bio])
39
+ def test_includes_linked_bio
40
+ @adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer, include: [:bio])
42
41
 
43
- expected = [
44
- {
45
- id: '43',
46
- type: 'bios',
47
- attributes: {
48
- content: 'AMS Contributor',
49
- rating: nil
50
- },
51
- relationships: {
52
- author: { data: { type: 'authors', id: '1' } }
53
- }
42
+ expected = [
43
+ {
44
+ id: '43',
45
+ type: 'bios',
46
+ attributes: {
47
+ content: 'AMS Contributor',
48
+ rating: nil
49
+ },
50
+ relationships: {
51
+ author: { data: { type: 'authors', id: '1' } }
54
52
  }
55
- ]
53
+ }
54
+ ]
56
55
 
57
- assert_equal(expected, @adapter.serializable_hash[:included])
58
- end
56
+ assert_equal(expected, @adapter.serializable_hash[:included])
57
+ end
59
58
 
60
- def test_has_one_with_virtual_value
61
- serializer = VirtualValueSerializer.new(@virtual_value)
62
- adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
59
+ def test_has_one_with_virtual_value
60
+ serializer = VirtualValueSerializer.new(@virtual_value)
61
+ adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
63
62
 
64
- expected = {
65
- data: {
66
- id: '1',
67
- type: 'virtual_values',
68
- relationships: {
69
- maker: { data: { id: 1 } },
70
- reviews: { data: [{ id: 1 }, { id: 2 }] }
71
- }
63
+ expected = {
64
+ data: {
65
+ id: '1',
66
+ type: 'virtual-values',
67
+ relationships: {
68
+ maker: { data: { id: 1 } },
69
+ reviews: { data: [{ id: 1 }, { id: 2 }] }
72
70
  }
73
71
  }
72
+ }
74
73
 
75
- assert_equal(expected, adapter.serializable_hash)
76
- end
74
+ assert_equal(expected, adapter.serializable_hash)
77
75
  end
78
76
  end
79
77
  end