active_model_serializers 0.8.3 → 0.10.0.rc4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (184) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +6 -0
  3. data/.rubocop.yml +86 -0
  4. data/.rubocop_todo.yml +240 -0
  5. data/.simplecov +111 -0
  6. data/.travis.yml +33 -22
  7. data/CHANGELOG.md +358 -6
  8. data/CONTRIBUTING.md +220 -0
  9. data/Gemfile +46 -1
  10. data/{MIT-LICENSE.txt → LICENSE.txt} +3 -2
  11. data/README.md +81 -591
  12. data/Rakefile +68 -11
  13. data/active_model_serializers.gemspec +57 -23
  14. data/appveyor.yml +27 -0
  15. data/docs/ARCHITECTURE.md +120 -0
  16. data/docs/DESIGN.textile +8 -0
  17. data/docs/README.md +35 -0
  18. data/docs/general/adapters.md +162 -0
  19. data/docs/general/caching.md +52 -0
  20. data/docs/general/configuration_options.md +27 -0
  21. data/docs/general/getting_started.md +98 -0
  22. data/docs/general/instrumentation.md +40 -0
  23. data/docs/general/logging.md +14 -0
  24. data/docs/general/rendering.md +153 -0
  25. data/docs/general/serializers.md +207 -0
  26. data/docs/how-open-source-maintained.jpg +0 -0
  27. data/docs/howto/add_pagination_links.md +121 -0
  28. data/docs/howto/add_root_key.md +51 -0
  29. data/docs/howto/outside_controller_use.md +58 -0
  30. data/docs/howto/test.md +152 -0
  31. data/docs/integrations/ember-and-json-api.md +112 -0
  32. data/docs/integrations/grape.md +19 -0
  33. data/docs/jsonapi/schema/schema.json +366 -0
  34. data/docs/jsonapi/schema.md +140 -0
  35. data/lib/action_controller/serialization.rb +41 -37
  36. data/lib/active_model/serializable_resource.rb +72 -0
  37. data/lib/active_model/serializer/adapter/attributes.rb +66 -0
  38. data/lib/active_model/serializer/adapter/base.rb +58 -0
  39. data/lib/active_model/serializer/adapter/cached_serializer.rb +45 -0
  40. data/lib/active_model/serializer/adapter/fragment_cache.rb +111 -0
  41. data/lib/active_model/serializer/adapter/json/fragment_cache.rb +13 -0
  42. data/lib/active_model/serializer/adapter/json.rb +21 -0
  43. data/lib/active_model/serializer/adapter/json_api/deserialization.rb +207 -0
  44. data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +21 -0
  45. data/lib/active_model/serializer/adapter/json_api/link.rb +44 -0
  46. data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +58 -0
  47. data/lib/active_model/serializer/adapter/json_api.rb +223 -0
  48. data/lib/active_model/serializer/adapter/null.rb +11 -0
  49. data/lib/active_model/serializer/adapter.rb +91 -0
  50. data/lib/active_model/serializer/array_serializer.rb +9 -0
  51. data/lib/active_model/serializer/association.rb +20 -0
  52. data/lib/active_model/serializer/associations.rb +87 -220
  53. data/lib/active_model/serializer/attribute.rb +25 -0
  54. data/lib/active_model/serializer/attributes.rb +82 -0
  55. data/lib/active_model/serializer/belongs_to_reflection.rb +10 -0
  56. data/lib/active_model/serializer/caching.rb +100 -0
  57. data/lib/active_model/serializer/collection_reflection.rb +7 -0
  58. data/lib/active_model/serializer/collection_serializer.rb +47 -0
  59. data/lib/active_model/serializer/configuration.rb +28 -0
  60. data/lib/active_model/serializer/field.rb +56 -0
  61. data/lib/active_model/serializer/fieldset.rb +31 -0
  62. data/lib/active_model/serializer/has_many_reflection.rb +10 -0
  63. data/lib/active_model/serializer/has_one_reflection.rb +10 -0
  64. data/lib/active_model/serializer/include_tree.rb +111 -0
  65. data/lib/active_model/serializer/links.rb +33 -0
  66. data/lib/active_model/serializer/lint.rb +142 -0
  67. data/lib/active_model/serializer/reflection.rb +91 -0
  68. data/lib/active_model/serializer/singular_reflection.rb +7 -0
  69. data/lib/active_model/serializer/type.rb +25 -0
  70. data/lib/active_model/{serializers → serializer}/version.rb +1 -1
  71. data/lib/active_model/serializer.rb +99 -479
  72. data/lib/active_model_serializers/callbacks.rb +55 -0
  73. data/lib/active_model_serializers/deserialization.rb +13 -0
  74. data/lib/active_model_serializers/logging.rb +119 -0
  75. data/lib/active_model_serializers/model.rb +39 -0
  76. data/lib/active_model_serializers/railtie.rb +38 -0
  77. data/lib/active_model_serializers/serialization_context.rb +10 -0
  78. data/lib/active_model_serializers/test/schema.rb +103 -0
  79. data/lib/active_model_serializers/test/serializer.rb +125 -0
  80. data/lib/active_model_serializers/test.rb +7 -0
  81. data/lib/active_model_serializers.rb +20 -92
  82. data/lib/generators/rails/USAGE +6 -0
  83. data/lib/generators/rails/resource_override.rb +10 -0
  84. data/lib/generators/rails/serializer_generator.rb +36 -0
  85. data/lib/generators/rails/templates/serializer.rb.erb +8 -0
  86. data/lib/grape/active_model_serializers.rb +14 -0
  87. data/lib/grape/formatters/active_model_serializers.rb +15 -0
  88. data/lib/grape/helpers/active_model_serializers.rb +16 -0
  89. data/test/action_controller/adapter_selector_test.rb +53 -0
  90. data/test/action_controller/explicit_serializer_test.rb +134 -0
  91. data/test/action_controller/json/include_test.rb +167 -0
  92. data/test/action_controller/json_api/deserialization_test.rb +59 -0
  93. data/test/action_controller/json_api/linked_test.rb +196 -0
  94. data/test/action_controller/json_api/pagination_test.rb +116 -0
  95. data/test/{serialization_scope_name_test.rb → action_controller/serialization_scope_name_test.rb} +11 -15
  96. data/test/action_controller/serialization_test.rb +435 -0
  97. data/test/active_model_serializers/logging_test.rb +77 -0
  98. data/test/active_model_serializers/model_test.rb +9 -0
  99. data/test/active_model_serializers/railtie_test_isolated.rb +57 -0
  100. data/test/active_model_serializers/serialization_context_test.rb +18 -0
  101. data/test/active_model_serializers/test/schema_test.rb +128 -0
  102. data/test/active_model_serializers/test/serializer_test.rb +63 -0
  103. data/test/active_record_test.rb +9 -0
  104. data/test/adapter/fragment_cache_test.rb +38 -0
  105. data/test/adapter/json/belongs_to_test.rb +47 -0
  106. data/test/adapter/json/collection_test.rb +92 -0
  107. data/test/adapter/json/has_many_test.rb +47 -0
  108. data/test/adapter/json_api/belongs_to_test.rb +157 -0
  109. data/test/adapter/json_api/collection_test.rb +97 -0
  110. data/test/adapter/json_api/fields_test.rb +89 -0
  111. data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
  112. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
  113. data/test/adapter/json_api/has_many_test.rb +145 -0
  114. data/test/adapter/json_api/has_one_test.rb +81 -0
  115. data/test/adapter/json_api/json_api_test.rb +37 -0
  116. data/test/adapter/json_api/linked_test.rb +394 -0
  117. data/test/adapter/json_api/links_test.rb +68 -0
  118. data/test/adapter/json_api/pagination_links_test.rb +115 -0
  119. data/test/adapter/json_api/parse_test.rb +139 -0
  120. data/test/adapter/json_api/resource_type_config_test.rb +71 -0
  121. data/test/adapter/json_api/toplevel_jsonapi_test.rb +84 -0
  122. data/test/adapter/json_test.rb +47 -0
  123. data/test/adapter/null_test.rb +25 -0
  124. data/test/adapter_test.rb +42 -0
  125. data/test/array_serializer_test.rb +36 -73
  126. data/test/collection_serializer_test.rb +100 -0
  127. data/test/fixtures/active_record.rb +56 -0
  128. data/test/fixtures/poro.rb +229 -0
  129. data/test/generators/scaffold_controller_generator_test.rb +24 -0
  130. data/test/generators/serializer_generator_test.rb +57 -0
  131. data/test/grape_test.rb +82 -0
  132. data/test/include_tree/from_include_args_test.rb +26 -0
  133. data/test/include_tree/from_string_test.rb +94 -0
  134. data/test/include_tree/include_args_to_hash_test.rb +64 -0
  135. data/test/lint_test.rb +40 -0
  136. data/test/logger_test.rb +18 -0
  137. data/test/poro_test.rb +9 -0
  138. data/test/serializable_resource_test.rb +27 -0
  139. data/test/serializers/adapter_for_test.rb +166 -0
  140. data/test/serializers/association_macros_test.rb +36 -0
  141. data/test/serializers/associations_test.rb +267 -0
  142. data/test/serializers/attribute_test.rb +123 -0
  143. data/test/serializers/attributes_test.rb +52 -0
  144. data/test/serializers/cache_test.rb +209 -0
  145. data/test/serializers/configuration_test.rb +32 -0
  146. data/test/serializers/fieldset_test.rb +14 -0
  147. data/test/serializers/meta_test.rb +130 -0
  148. data/test/serializers/options_test.rb +21 -0
  149. data/test/serializers/root_test.rb +21 -0
  150. data/test/serializers/serializer_for_test.rb +134 -0
  151. data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
  152. data/test/support/isolated_unit.rb +77 -0
  153. data/test/support/rails5_shims.rb +29 -0
  154. data/test/support/rails_app.rb +25 -0
  155. data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
  156. data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +6 -0
  157. data/test/support/schemas/custom/show.json +7 -0
  158. data/test/support/schemas/hyper_schema.json +93 -0
  159. data/test/support/schemas/render_using_json_api.json +43 -0
  160. data/test/support/schemas/simple_json_pointers.json +10 -0
  161. data/test/support/serialization_testing.rb +53 -0
  162. data/test/support/simplecov.rb +6 -0
  163. data/test/support/stream_capture.rb +50 -0
  164. data/test/support/test_case.rb +19 -0
  165. data/test/test_helper.rb +55 -24
  166. metadata +358 -42
  167. data/DESIGN.textile +0 -586
  168. data/Gemfile.edge +0 -9
  169. data/bench/perf.rb +0 -43
  170. data/cruft.md +0 -19
  171. data/lib/active_model/array_serializer.rb +0 -104
  172. data/lib/active_record/serializer_override.rb +0 -16
  173. data/lib/generators/resource_override.rb +0 -13
  174. data/lib/generators/serializer/USAGE +0 -9
  175. data/lib/generators/serializer/serializer_generator.rb +0 -42
  176. data/lib/generators/serializer/templates/serializer.rb +0 -19
  177. data/test/association_test.rb +0 -592
  178. data/test/caching_test.rb +0 -96
  179. data/test/generators_test.rb +0 -85
  180. data/test/no_serialization_scope_test.rb +0 -34
  181. data/test/serialization_test.rb +0 -392
  182. data/test/serializer_support_test.rb +0 -51
  183. data/test/serializer_test.rb +0 -1465
  184. data/test/test_fakes.rb +0 -217
@@ -0,0 +1,157 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ module Adapter
6
+ class JsonApi
7
+ class BelongsToTest < ActiveSupport::TestCase
8
+ def setup
9
+ @author = Author.new(id: 1, name: 'Steve K.')
10
+ @author.bio = nil
11
+ @author.roles = []
12
+ @blog = Blog.new(id: 23, name: 'AMS Blog')
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
+ @post.blog = @blog
18
+ @anonymous_post.comments = []
19
+ @anonymous_post.blog = nil
20
+ @comment.post = @post
21
+ @comment.author = nil
22
+ @post.author = @author
23
+ @anonymous_post.author = nil
24
+ @blog = Blog.new(id: 1, name: 'My Blog!!')
25
+ @blog.writer = @author
26
+ @blog.articles = [@post, @anonymous_post]
27
+ @author.posts = []
28
+
29
+ @serializer = CommentSerializer.new(@comment)
30
+ @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
31
+ ActionController::Base.cache_store.clear
32
+ end
33
+
34
+ def test_includes_post_id
35
+ expected = { data: { type: 'posts', id: '42' } }
36
+
37
+ assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:post])
38
+ end
39
+
40
+ def test_includes_linked_post
41
+ @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: [:post])
42
+ expected = [{
43
+ id: '42',
44
+ type: 'posts',
45
+ attributes: {
46
+ title: 'New Post',
47
+ body: 'Body',
48
+ },
49
+ relationships: {
50
+ comments: { data: [{ type: 'comments', id: '1' }] },
51
+ blog: { data: { type: 'blogs', id: '999' } },
52
+ author: { data: { type: 'authors', id: '1' } }
53
+ }
54
+ }]
55
+ assert_equal expected, @adapter.serializable_hash[:included]
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',
62
+ type: 'posts',
63
+ attributes: {
64
+ title: 'New Post'
65
+ },
66
+ relationships: {
67
+ comments: { data: [{ type: 'comments', id: '1' }] },
68
+ blog: { data: { type: 'blogs', id: '999' } },
69
+ author: { data: { type: 'authors', id: '1' } }
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
+ ]
104
+ }
105
+ }
106
+ assert_equal expected, relationships
107
+ end
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
153
+ end
154
+ end
155
+ end
156
+ end
157
+ end
@@ -0,0 +1,97 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ module Adapter
6
+ class JsonApi
7
+ class CollectionTest < ActiveSupport::TestCase
8
+ def setup
9
+ @author = Author.new(id: 1, name: 'Steve K.')
10
+ @author.bio = nil
11
+ @blog = Blog.new(id: 23, name: 'AMS Blog')
12
+ @first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
13
+ @second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
14
+ @first_post.comments = []
15
+ @second_post.comments = []
16
+ @first_post.blog = @blog
17
+ @second_post.blog = nil
18
+ @first_post.author = @author
19
+ @second_post.author = @author
20
+ @author.posts = [@first_post, @second_post]
21
+
22
+ @serializer = CollectionSerializer.new([@first_post, @second_post])
23
+ @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
24
+ ActionController::Base.cache_store.clear
25
+ end
26
+
27
+ def test_include_multiple_posts
28
+ expected = [
29
+ {
30
+ id: '1',
31
+ type: 'posts',
32
+ attributes: {
33
+ title: 'Hello!!',
34
+ body: 'Hello, world!!'
35
+ },
36
+ relationships: {
37
+ comments: { data: [] },
38
+ blog: { data: { type: 'blogs', id: '999' } },
39
+ author: { data: { type: 'authors', id: '1' } }
40
+ }
41
+ },
42
+ {
43
+ id: '2',
44
+ type: 'posts',
45
+ attributes: {
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
+ }
54
+ }
55
+ ]
56
+
57
+ assert_equal(expected, @adapter.serializable_hash[:data])
58
+ end
59
+
60
+ def test_limiting_fields
61
+ actual = ActiveModel::SerializableResource.new(
62
+ [@first_post, @second_post], adapter: :json_api,
63
+ fields: { posts: %w(title comments blog author) })
64
+ .serializable_hash
65
+ expected = [
66
+ {
67
+ id: '1',
68
+ type: 'posts',
69
+ attributes: {
70
+ title: 'Hello!!'
71
+ },
72
+ relationships: {
73
+ comments: { data: [] },
74
+ blog: { data: { type: 'blogs', id: '999' } },
75
+ author: { data: { type: 'authors', id: '1' } }
76
+ }
77
+ },
78
+ {
79
+ id: '2',
80
+ type: 'posts',
81
+ attributes: {
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
+ }
89
+ }
90
+ ]
91
+ assert_equal(expected, actual[:data])
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,89 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ module Adapter
6
+ class JsonApi
7
+ class FieldsTest < ActiveSupport::TestCase
8
+ Post = Class.new(::Model)
9
+ class PostSerializer < ActiveModel::Serializer
10
+ type 'posts'
11
+ attributes :title, :body
12
+ belongs_to :author
13
+ has_many :comments
14
+ end
15
+
16
+ Author = Class.new(::Model)
17
+ class AuthorSerializer < ActiveModel::Serializer
18
+ type 'authors'
19
+ attributes :name, :birthday
20
+ end
21
+
22
+ Comment = Class.new(::Model)
23
+ class CommentSerializer < ActiveModel::Serializer
24
+ type 'comments'
25
+ attributes :body
26
+ belongs_to :author
27
+ end
28
+
29
+ def setup
30
+ @author = Author.new(id: 1, name: 'Lucas', birthday: '10.01.1990')
31
+ @comment1 = Comment.new(id: 7, body: 'cool', author: @author)
32
+ @comment2 = Comment.new(id: 12, body: 'awesome', author: @author)
33
+ @post = Post.new(id: 1337, title: 'Title 1', body: 'Body 1',
34
+ author: @author, comments: [@comment1, @comment2])
35
+ @comment1.post = @post
36
+ @comment2.post = @post
37
+ end
38
+
39
+ def test_fields_attributes
40
+ fields = { posts: [:title] }
41
+ hash = serializable(@post, adapter: :json_api, fields: fields).serializable_hash
42
+ expected = {
43
+ title: 'Title 1'
44
+ }
45
+
46
+ assert_equal(expected, hash[:data][:attributes])
47
+ end
48
+
49
+ def test_fields_relationships
50
+ fields = { posts: [:author] }
51
+ hash = serializable(@post, adapter: :json_api, fields: fields).serializable_hash
52
+ expected = {
53
+ author: {
54
+ data: {
55
+ type: 'authors',
56
+ id: '1'
57
+ }
58
+ }
59
+ }
60
+
61
+ assert_equal(expected, hash[:data][:relationships])
62
+ end
63
+
64
+ def test_fields_included
65
+ fields = { posts: [:author], comments: [:body] }
66
+ hash = serializable(@post, adapter: :json_api, fields: fields, include: 'comments').serializable_hash
67
+ expected = [
68
+ {
69
+ type: 'comments',
70
+ id: '7',
71
+ attributes: {
72
+ body: 'cool'
73
+ }
74
+ }, {
75
+ type: 'comments',
76
+ id: '12',
77
+ attributes: {
78
+ body: 'awesome'
79
+ }
80
+ }
81
+ ]
82
+
83
+ assert_equal(expected, hash[:included])
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,45 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ module Adapter
6
+ class JsonApi
7
+ class HasManyEmbedIdsTest < ActiveSupport::TestCase
8
+ def setup
9
+ @author = Author.new(name: 'Steve K.')
10
+ @author.bio = nil
11
+ @author.roles = nil
12
+ @first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
13
+ @second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
14
+ @author.posts = [@first_post, @second_post]
15
+ @first_post.author = @author
16
+ @second_post.author = @author
17
+ @first_post.comments = []
18
+ @second_post.comments = []
19
+ @blog = Blog.new(id: 23, name: 'AMS Blog')
20
+ @first_post.blog = @blog
21
+ @second_post.blog = nil
22
+
23
+ @serializer = AuthorSerializer.new(@author)
24
+ @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
25
+ end
26
+
27
+ def test_includes_comment_ids
28
+ expected = {
29
+ data: [
30
+ { type: 'posts', id: '1' },
31
+ { type: 'posts', id: '2' }
32
+ ]
33
+ }
34
+
35
+ assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:posts])
36
+ end
37
+
38
+ def test_no_includes_linked_comments
39
+ assert_nil @adapter.serializable_hash[:linked]
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,98 @@
1
+ require 'test_helper'
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
23
+
24
+ @serializer = PostPreviewSerializer.new(@post)
25
+ @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
26
+ @serializer,
27
+ include: [:comments, :author]
28
+ )
29
+ end
30
+
31
+ def test_includes_comment_ids
32
+ expected = {
33
+ data: [
34
+ { type: 'comments', id: '1' },
35
+ { type: 'comments', id: '2' }
36
+ ]
37
+ }
38
+
39
+ assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
40
+ end
41
+
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
+ }
65
+ }
66
+ ]
67
+
68
+ assert_equal(expected, @adapter.serializable_hash[:included])
69
+ end
70
+
71
+ def test_includes_author_id
72
+ expected = {
73
+ data: { type: 'authors', id: @author.id.to_s }
74
+ }
75
+
76
+ assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:author])
77
+ end
78
+
79
+ def test_explicit_serializer_with_null_resource
80
+ @post.author = nil
81
+
82
+ expected = { data: nil }
83
+
84
+ assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:author])
85
+ end
86
+
87
+ def test_explicit_serializer_with_null_collection
88
+ @post.comments = []
89
+
90
+ expected = { data: [] }
91
+
92
+ assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,145 @@
1
+ require 'test_helper'
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)
34
+
35
+ @virtual_value = VirtualValue.new(id: 1)
36
+ end
37
+
38
+ def test_includes_comment_ids
39
+ expected = { data: [{ type: 'comments', id: '1' }, { type: 'comments', id: '2' }] }
40
+
41
+ assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
42
+ end
43
+
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
69
+
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
89
+
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)
93
+
94
+ assert_nil adapter.serializable_hash[:linked]
95
+ end
96
+
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]
101
+
102
+ expected = {
103
+ data: [{
104
+ type: 'posts',
105
+ id: '1'
106
+ }]
107
+ }
108
+ assert_equal expected, actual
109
+ end
110
+
111
+ def test_has_many_with_no_serializer
112
+ serializer = PostWithTagsSerializer.new(@post)
113
+ adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
114
+
115
+ assert_equal({
116
+ data: {
117
+ id: '1',
118
+ type: 'posts',
119
+ relationships: {
120
+ tags: { data: [@tag.as_json] }
121
+ }
122
+ }
123
+ }, adapter.serializable_hash)
124
+ end
125
+
126
+ def test_has_many_with_virtual_value
127
+ serializer = VirtualValueSerializer.new(@virtual_value)
128
+ adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
129
+
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
+ }
138
+ }
139
+ }, adapter.serializable_hash)
140
+ end
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end