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,81 @@
1
+ require 'test_helper'
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 = []
27
+
28
+ @virtual_value = VirtualValue.new(id: 1)
29
+
30
+ @serializer = AuthorSerializer.new(@author)
31
+ @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: [:bio, :posts])
32
+ end
33
+
34
+ def test_includes_bio_id
35
+ expected = { data: { type: 'bios', id: '43' } }
36
+
37
+ assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:bio])
38
+ end
39
+
40
+ def test_includes_linked_bio
41
+ @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: [:bio])
42
+
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
+ }
54
+ }
55
+ ]
56
+
57
+ assert_equal(expected, @adapter.serializable_hash[:included])
58
+ end
59
+
60
+ def test_has_one_with_virtual_value
61
+ serializer = VirtualValueSerializer.new(@virtual_value)
62
+ adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
63
+
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
+ }
72
+ }
73
+ }
74
+
75
+ assert_equal(expected, adapter.serializable_hash)
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,37 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ module Adapter
6
+ class JsonApiTest < 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: 1, 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
+ @first_comment.post = @post
15
+ @second_comment.post = @post
16
+ @post.author = @author
17
+ @blog = Blog.new(id: 1, name: 'My Blog!!')
18
+ @post.blog = @blog
19
+ end
20
+
21
+ def test_custom_keys
22
+ serializer = PostWithCustomKeysSerializer.new(@post)
23
+ adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
24
+
25
+ assert_equal({
26
+ reviews: { data: [
27
+ { type: 'comments', id: '1' },
28
+ { type: 'comments', id: '2' }
29
+ ] },
30
+ writer: { data: { type: 'authors', id: '1' } },
31
+ site: { data: { type: 'blogs', id: '1' } }
32
+ }, adapter.serializable_hash[:data][:relationships])
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,394 @@
1
+ require 'test_helper'
2
+
3
+ NestedPost = Class.new(Model)
4
+ class NestedPostSerializer < ActiveModel::Serializer
5
+ has_many :nested_posts
6
+ end
7
+
8
+ module ActiveModel
9
+ class Serializer
10
+ module Adapter
11
+ class JsonApi
12
+ class LinkedTest < ActiveSupport::TestCase
13
+ def setup
14
+ @author1 = Author.new(id: 1, name: 'Steve K.')
15
+ @author2 = Author.new(id: 2, name: 'Tenderlove')
16
+ @bio1 = Bio.new(id: 1, content: 'AMS Contributor')
17
+ @bio2 = Bio.new(id: 2, content: 'Rails Contributor')
18
+ @first_post = Post.new(id: 10, title: 'Hello!!', body: 'Hello, world!!')
19
+ @second_post = Post.new(id: 20, title: 'New Post', body: 'Body')
20
+ @third_post = Post.new(id: 30, title: 'Yet Another Post', body: 'Body')
21
+ @blog = Blog.new({ name: 'AMS Blog' })
22
+ @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
23
+ @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
24
+ @first_post.blog = @blog
25
+ @second_post.blog = @blog
26
+ @third_post.blog = nil
27
+ @first_post.comments = [@first_comment, @second_comment]
28
+ @second_post.comments = []
29
+ @third_post.comments = []
30
+ @first_post.author = @author1
31
+ @second_post.author = @author2
32
+ @third_post.author = @author1
33
+ @first_comment.post = @first_post
34
+ @first_comment.author = nil
35
+ @second_comment.post = @first_post
36
+ @second_comment.author = nil
37
+ @author1.posts = [@first_post, @third_post]
38
+ @author1.bio = @bio1
39
+ @author1.roles = []
40
+ @author2.posts = [@second_post]
41
+ @author2.bio = @bio2
42
+ @author2.roles = []
43
+ @bio1.author = @author1
44
+ @bio2.author = @author2
45
+ end
46
+
47
+ def test_include_multiple_posts_and_linked_array
48
+ serializer = CollectionSerializer.new([@first_post, @second_post])
49
+ adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
50
+ serializer,
51
+ include: [:comments, author: [:bio]]
52
+ )
53
+ alt_adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
54
+ serializer,
55
+ include: [:comments, author: [:bio]]
56
+ )
57
+
58
+ expected = {
59
+ data: [
60
+ {
61
+ id: '10',
62
+ type: 'posts',
63
+ attributes: {
64
+ title: 'Hello!!',
65
+ body: 'Hello, world!!'
66
+ },
67
+ relationships: {
68
+ comments: { data: [{ type: 'comments', id: '1' }, { type: 'comments', id: '2' }] },
69
+ blog: { data: { type: 'blogs', id: '999' } },
70
+ author: { data: { type: 'authors', id: '1' } }
71
+ }
72
+ },
73
+ {
74
+ id: '20',
75
+ type: 'posts',
76
+ attributes: {
77
+ title: 'New Post',
78
+ body: 'Body'
79
+ },
80
+ relationships: {
81
+ comments: { data: [] },
82
+ blog: { data: { type: 'blogs', id: '999' } },
83
+ author: { data: { type: 'authors', id: '2' } }
84
+ }
85
+ }
86
+ ],
87
+ included: [
88
+ {
89
+ id: '1',
90
+ type: 'comments',
91
+ attributes: {
92
+ body: 'ZOMG A COMMENT'
93
+ },
94
+ relationships: {
95
+ post: { data: { type: 'posts', id: '10' } },
96
+ author: { data: nil }
97
+ }
98
+ }, {
99
+ id: '2',
100
+ type: 'comments',
101
+ attributes: {
102
+ body: 'ZOMG ANOTHER COMMENT',
103
+ },
104
+ relationships: {
105
+ post: { data: { type: 'posts', id: '10' } },
106
+ author: { data: nil }
107
+ }
108
+ }, {
109
+ id: '1',
110
+ type: 'authors',
111
+ attributes: {
112
+ name: 'Steve K.'
113
+ },
114
+ relationships: {
115
+ posts: { data: [{ type: 'posts', id: '10' }, { type: 'posts', id: '30' }] },
116
+ roles: { data: [] },
117
+ bio: { data: { type: 'bios', id: '1' } }
118
+ }
119
+ }, {
120
+ id: '1',
121
+ type: 'bios',
122
+ attributes: {
123
+ content: 'AMS Contributor',
124
+ rating: nil
125
+ },
126
+ relationships: {
127
+ author: { data: { type: 'authors', id: '1' } }
128
+ }
129
+ }, {
130
+ id: '2',
131
+ type: 'authors',
132
+ attributes: {
133
+ name: 'Tenderlove'
134
+ },
135
+ relationships: {
136
+ posts: { data: [{ type: 'posts', id: '20' }] },
137
+ roles: { data: [] },
138
+ bio: { data: { type: 'bios', id: '2' } }
139
+ }
140
+ }, {
141
+ id: '2',
142
+ type: 'bios',
143
+ attributes: {
144
+ rating: nil,
145
+ content: 'Rails Contributor',
146
+ },
147
+ relationships: {
148
+ author: { data: { type: 'authors', id: '2' } }
149
+ }
150
+ }
151
+ ]
152
+ }
153
+ assert_equal expected, adapter.serializable_hash
154
+ assert_equal expected, alt_adapter.serializable_hash
155
+ end
156
+
157
+ def test_include_multiple_posts_and_linked
158
+ serializer = BioSerializer.new @bio1
159
+ adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
160
+ serializer,
161
+ include: [author: [:posts]]
162
+ )
163
+ alt_adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
164
+ serializer,
165
+ include: [author: [:posts]]
166
+ )
167
+
168
+ expected = [
169
+ {
170
+ id: '1',
171
+ type: 'authors',
172
+ attributes: {
173
+ name: 'Steve K.'
174
+ },
175
+ relationships: {
176
+ posts: { data: [{ type: 'posts', id: '10' }, { type: 'posts', id: '30' }] },
177
+ roles: { data: [] },
178
+ bio: { data: { type: 'bios', id: '1' } }
179
+ }
180
+ }, {
181
+ id: '10',
182
+ type: 'posts',
183
+ attributes: {
184
+ title: 'Hello!!',
185
+ body: 'Hello, world!!'
186
+ },
187
+ relationships: {
188
+ comments: { data: [{ type: 'comments', id: '1' }, { type: 'comments', id: '2' }] },
189
+ blog: { data: { type: 'blogs', id: '999' } },
190
+ author: { data: { type: 'authors', id: '1' } }
191
+ }
192
+ }, {
193
+ id: '30',
194
+ type: 'posts',
195
+ attributes: {
196
+ title: 'Yet Another Post',
197
+ body: 'Body'
198
+ },
199
+ relationships: {
200
+ comments: { data: [] },
201
+ blog: { data: { type: 'blogs', id: '999' } },
202
+ author: { data: { type: 'authors', id: '1' } }
203
+ }
204
+ }
205
+ ]
206
+
207
+ assert_equal expected, adapter.serializable_hash[:included]
208
+ assert_equal expected, alt_adapter.serializable_hash[:included]
209
+ end
210
+
211
+ def test_underscore_model_namespace_for_linked_resource_type
212
+ spammy_post = Post.new(id: 123)
213
+ spammy_post.related = [Spam::UnrelatedLink.new(id: 456)]
214
+ serializer = SpammyPostSerializer.new(spammy_post)
215
+ adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
216
+ relationships = adapter.serializable_hash[:data][:relationships]
217
+ expected = {
218
+ related: {
219
+ data: [{
220
+ type: 'spam_unrelated_links',
221
+ id: '456'
222
+ }]
223
+ }
224
+ }
225
+ assert_equal expected, relationships
226
+ end
227
+
228
+ def test_multiple_references_to_same_resource
229
+ serializer = CollectionSerializer.new([@first_comment, @second_comment])
230
+ adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
231
+ serializer,
232
+ include: [:post]
233
+ )
234
+
235
+ expected = [
236
+ {
237
+ id: '10',
238
+ type: 'posts',
239
+ attributes: {
240
+ title: 'Hello!!',
241
+ body: 'Hello, world!!'
242
+ },
243
+ relationships: {
244
+ comments: {
245
+ data: [{ type: 'comments', id: '1' }, { type: 'comments', id: '2' }]
246
+ },
247
+ blog: {
248
+ data: { type: 'blogs', id: '999' }
249
+ },
250
+ author: {
251
+ data: { type: 'authors', id: '1' }
252
+ }
253
+ }
254
+ }
255
+ ]
256
+
257
+ assert_equal expected, adapter.serializable_hash[:included]
258
+ end
259
+
260
+ def test_nil_link_with_specified_serializer
261
+ @first_post.author = nil
262
+ serializer = PostPreviewSerializer.new(@first_post)
263
+ adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
264
+ serializer,
265
+ include: [:author]
266
+ )
267
+
268
+ expected = {
269
+ data: {
270
+ id: '10',
271
+ type: 'posts',
272
+ attributes: {
273
+ title: 'Hello!!',
274
+ body: 'Hello, world!!'
275
+ },
276
+ relationships: {
277
+ comments: { data: [{ type: 'comments', id: '1' }, { type: 'comments', id: '2' }] },
278
+ author: { data: nil }
279
+ }
280
+ }
281
+ }
282
+ assert_equal expected, adapter.serializable_hash
283
+ end
284
+ end
285
+
286
+ class NoDuplicatesTest < ActiveSupport::TestCase
287
+ Post = Class.new(::Model)
288
+ Author = Class.new(::Model)
289
+
290
+ class PostSerializer < ActiveModel::Serializer
291
+ type 'posts'
292
+ belongs_to :author
293
+ end
294
+
295
+ class AuthorSerializer < ActiveModel::Serializer
296
+ type 'authors'
297
+ has_many :posts
298
+ end
299
+
300
+ def setup
301
+ @author = Author.new(id: 1, posts: [], roles: [], bio: nil)
302
+ @post1 = Post.new(id: 1, author: @author)
303
+ @post2 = Post.new(id: 2, author: @author)
304
+ @author.posts << @post1
305
+ @author.posts << @post2
306
+
307
+ @nestedpost1 = ::NestedPost.new(id: 1, nested_posts: [])
308
+ @nestedpost2 = ::NestedPost.new(id: 2, nested_posts: [])
309
+ @nestedpost1.nested_posts << @nestedpost1
310
+ @nestedpost1.nested_posts << @nestedpost2
311
+ @nestedpost2.nested_posts << @nestedpost1
312
+ @nestedpost2.nested_posts << @nestedpost2
313
+ end
314
+
315
+ def test_no_duplicates
316
+ hash = ActiveModel::SerializableResource.new(@post1, adapter: :json_api,
317
+ include: '*.*')
318
+ .serializable_hash
319
+ expected = [
320
+ {
321
+ type: 'authors', id: '1',
322
+ relationships: {
323
+ posts: {
324
+ data: [
325
+ { type: 'posts', id: '1' },
326
+ { type: 'posts', id: '2' }
327
+ ]
328
+ }
329
+ }
330
+ },
331
+ {
332
+ type: 'posts', id: '2',
333
+ relationships: {
334
+ author: {
335
+ data: { type: 'authors', id: '1' }
336
+ }
337
+ }
338
+ }
339
+ ]
340
+ assert_equal(expected, hash[:included])
341
+ end
342
+
343
+ def test_no_duplicates_collection
344
+ hash = ActiveModel::SerializableResource.new(
345
+ [@post1, @post2], adapter: :json_api,
346
+ include: '*.*')
347
+ .serializable_hash
348
+ expected = [
349
+ {
350
+ type: 'authors', id: '1',
351
+ relationships: {
352
+ posts: {
353
+ data: [
354
+ { type: 'posts', id: '1' },
355
+ { type: 'posts', id: '2' }
356
+ ]
357
+ }
358
+ }
359
+ }
360
+ ]
361
+ assert_equal(expected, hash[:included])
362
+ end
363
+
364
+ def test_no_duplicates_global
365
+ hash = ActiveModel::SerializableResource.new(
366
+ @nestedpost1,
367
+ adapter: :json_api,
368
+ include: '*').serializable_hash
369
+ expected = [
370
+ type: 'nested_posts', id: '2',
371
+ relationships: {
372
+ nested_posts: {
373
+ data: [
374
+ { type: 'nested_posts', id: '1' },
375
+ { type: 'nested_posts', id: '2' }
376
+ ]
377
+ }
378
+ }
379
+ ]
380
+ assert_equal(expected, hash[:included])
381
+ end
382
+
383
+ def test_no_duplicates_collection_global
384
+ hash = ActiveModel::SerializableResource.new(
385
+ [@nestedpost1, @nestedpost2],
386
+ adapter: :json_api,
387
+ include: '*').serializable_hash
388
+ assert_nil(hash[:included])
389
+ end
390
+ end
391
+ end
392
+ end
393
+ end
394
+ end
@@ -0,0 +1,68 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ module Adapter
6
+ class JsonApi
7
+ class LinksTest < ActiveSupport::TestCase
8
+ LinkAuthor = Class.new(::Model)
9
+ class LinkAuthorSerializer < ActiveModel::Serializer
10
+ link :self do
11
+ href "//example.com/link_author/#{object.id}"
12
+ meta stuff: 'value'
13
+ end
14
+
15
+ link :other, '//example.com/resource'
16
+
17
+ link :yet_another do
18
+ "//example.com/resource/#{object.id}"
19
+ end
20
+ end
21
+
22
+ def setup
23
+ @post = Post.new(id: 1337, comments: [], author: nil)
24
+ @author = LinkAuthor.new(id: 1337)
25
+ end
26
+
27
+ def test_toplevel_links
28
+ hash = ActiveModel::SerializableResource.new(
29
+ @post,
30
+ adapter: :json_api,
31
+ links: {
32
+ self: {
33
+ href: '//example.com/posts',
34
+ meta: {
35
+ stuff: 'value'
36
+ }
37
+ }
38
+ }).serializable_hash
39
+ expected = {
40
+ self: {
41
+ href: '//example.com/posts',
42
+ meta: {
43
+ stuff: 'value'
44
+ }
45
+ }
46
+ }
47
+ assert_equal(expected, hash[:links])
48
+ end
49
+
50
+ def test_resource_links
51
+ hash = serializable(@author, adapter: :json_api).serializable_hash
52
+ expected = {
53
+ self: {
54
+ href: '//example.com/link_author/1337',
55
+ meta: {
56
+ stuff: 'value'
57
+ }
58
+ },
59
+ other: '//example.com/resource',
60
+ yet_another: '//example.com/resource/1337'
61
+ }
62
+ assert_equal(expected, hash[:data][:links])
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,115 @@
1
+ require 'test_helper'
2
+ require 'will_paginate/array'
3
+ require 'kaminari'
4
+ require 'kaminari/hooks'
5
+ ::Kaminari::Hooks.init
6
+
7
+ module ActiveModel
8
+ class Serializer
9
+ module Adapter
10
+ class JsonApi
11
+ class PaginationLinksTest < ActiveSupport::TestCase
12
+ URI = 'http://example.com'
13
+
14
+ def setup
15
+ ActionController::Base.cache_store.clear
16
+ @array = [
17
+ Profile.new({ id: 1, name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
18
+ Profile.new({ id: 2, name: 'Name 2', description: 'Description 2', comments: 'Comments 2' }),
19
+ Profile.new({ id: 3, name: 'Name 3', description: 'Description 3', comments: 'Comments 3' })
20
+ ]
21
+ end
22
+
23
+ def mock_request(query_parameters = {}, original_url = URI)
24
+ context = Minitest::Mock.new
25
+ context.expect(:request_url, original_url)
26
+ context.expect(:query_parameters, query_parameters)
27
+ @options = {}
28
+ @options[:serialization_context] = context
29
+ end
30
+
31
+ def load_adapter(paginated_collection, options = {})
32
+ options = options.merge(adapter: :json_api)
33
+ ActiveModel::SerializableResource.new(paginated_collection, options)
34
+ end
35
+
36
+ def using_kaminari
37
+ Kaminari.paginate_array(@array).page(2).per(1)
38
+ end
39
+
40
+ def using_will_paginate
41
+ @array.paginate(page: 2, per_page: 1)
42
+ end
43
+
44
+ def data
45
+ { data: [
46
+ { id: '1', type: 'profiles', attributes: { name: 'Name 1', description: 'Description 1' } },
47
+ { id: '2', type: 'profiles', attributes: { name: 'Name 2', description: 'Description 2' } },
48
+ { id: '3', type: 'profiles', attributes: { name: 'Name 3', description: 'Description 3' } }
49
+ ]
50
+ }
51
+ end
52
+
53
+ def links
54
+ {
55
+ links: {
56
+ self: "#{URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
57
+ first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
58
+ prev: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
59
+ next: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
60
+ last: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1"
61
+ }
62
+ }
63
+ end
64
+
65
+ def expected_response_without_pagination_links
66
+ data
67
+ end
68
+
69
+ def expected_response_with_pagination_links
70
+ {}.tap do |hash|
71
+ hash[:data] = [data.values.flatten.second]
72
+ hash.merge! links
73
+ end
74
+ end
75
+
76
+ def expected_response_with_pagination_links_and_additional_params
77
+ new_links = links[:links].each_with_object({}) { |(key, value), hash| hash[key] = "#{value}&test=test" }
78
+ {}.tap do |hash|
79
+ hash[:data] = [data.values.flatten.second]
80
+ hash.merge! links: new_links
81
+ end
82
+ end
83
+
84
+ def test_pagination_links_using_kaminari
85
+ adapter = load_adapter(using_kaminari)
86
+
87
+ mock_request
88
+ assert_equal expected_response_with_pagination_links, adapter.serializable_hash(@options)
89
+ end
90
+
91
+ def test_pagination_links_using_will_paginate
92
+ adapter = load_adapter(using_will_paginate)
93
+
94
+ mock_request
95
+ assert_equal expected_response_with_pagination_links, adapter.serializable_hash(@options)
96
+ end
97
+
98
+ def test_pagination_links_with_additional_params
99
+ adapter = load_adapter(using_will_paginate)
100
+
101
+ mock_request({ test: 'test' })
102
+ assert_equal expected_response_with_pagination_links_and_additional_params,
103
+ adapter.serializable_hash(@options)
104
+ end
105
+
106
+ def test_not_showing_pagination_links
107
+ adapter = load_adapter(@array)
108
+
109
+ assert_equal expected_response_without_pagination_links, adapter.serializable_hash
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end