active_model_serializers 0.10.7 → 0.10.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (213) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +82 -1
  3. data/README.md +20 -21
  4. data/lib/action_controller/serialization.rb +11 -1
  5. data/lib/active_model/serializable_resource.rb +2 -0
  6. data/lib/active_model/serializer/adapter/attributes.rb +2 -0
  7. data/lib/active_model/serializer/adapter/base.rb +2 -0
  8. data/lib/active_model/serializer/adapter/json.rb +2 -0
  9. data/lib/active_model/serializer/adapter/json_api.rb +2 -0
  10. data/lib/active_model/serializer/adapter/null.rb +2 -0
  11. data/lib/active_model/serializer/adapter.rb +2 -0
  12. data/lib/active_model/serializer/array_serializer.rb +2 -0
  13. data/lib/active_model/serializer/association.rb +2 -0
  14. data/lib/active_model/serializer/attribute.rb +2 -0
  15. data/lib/active_model/serializer/belongs_to_reflection.rb +2 -0
  16. data/lib/active_model/serializer/collection_serializer.rb +16 -7
  17. data/lib/active_model/serializer/concerns/caching.rb +7 -2
  18. data/lib/active_model/serializer/error_serializer.rb +2 -0
  19. data/lib/active_model/serializer/errors_serializer.rb +2 -0
  20. data/lib/active_model/serializer/field.rb +2 -0
  21. data/lib/active_model/serializer/fieldset.rb +3 -1
  22. data/lib/active_model/serializer/has_many_reflection.rb +2 -0
  23. data/lib/active_model/serializer/has_one_reflection.rb +2 -0
  24. data/lib/active_model/serializer/lazy_association.rb +6 -2
  25. data/lib/active_model/serializer/link.rb +23 -0
  26. data/lib/active_model/serializer/lint.rb +2 -0
  27. data/lib/active_model/serializer/null.rb +2 -0
  28. data/lib/active_model/serializer/reflection.rb +9 -4
  29. data/lib/active_model/serializer/version.rb +3 -1
  30. data/lib/active_model/serializer.rb +35 -18
  31. data/lib/active_model_serializers/adapter/attributes.rb +23 -0
  32. data/lib/active_model_serializers/adapter/base.rb +2 -0
  33. data/lib/active_model_serializers/adapter/json.rb +2 -0
  34. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +2 -0
  35. data/lib/active_model_serializers/adapter/json_api/error.rb +2 -0
  36. data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +2 -0
  37. data/lib/active_model_serializers/adapter/json_api/link.rb +2 -0
  38. data/lib/active_model_serializers/adapter/json_api/meta.rb +2 -0
  39. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +7 -1
  40. data/lib/active_model_serializers/adapter/json_api/relationship.rb +2 -0
  41. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +3 -1
  42. data/lib/active_model_serializers/adapter/json_api.rb +14 -9
  43. data/lib/active_model_serializers/adapter/null.rb +2 -0
  44. data/lib/active_model_serializers/adapter.rb +3 -1
  45. data/lib/active_model_serializers/callbacks.rb +2 -0
  46. data/lib/active_model_serializers/deprecate.rb +2 -0
  47. data/lib/active_model_serializers/deserialization.rb +2 -0
  48. data/lib/active_model_serializers/json_pointer.rb +2 -0
  49. data/lib/active_model_serializers/logging.rb +2 -0
  50. data/lib/active_model_serializers/lookup_chain.rb +2 -0
  51. data/lib/active_model_serializers/model/caching.rb +26 -0
  52. data/lib/active_model_serializers/model.rb +2 -0
  53. data/lib/active_model_serializers/railtie.rb +4 -0
  54. data/lib/active_model_serializers/register_jsonapi_renderer.rb +2 -0
  55. data/lib/active_model_serializers/serializable_resource.rb +4 -2
  56. data/lib/active_model_serializers/serialization_context.rb +2 -0
  57. data/lib/active_model_serializers/test/schema.rb +2 -0
  58. data/lib/active_model_serializers/test/serializer.rb +2 -0
  59. data/lib/active_model_serializers/test.rb +2 -0
  60. data/lib/active_model_serializers.rb +20 -10
  61. data/lib/generators/rails/resource_override.rb +2 -0
  62. data/lib/generators/rails/serializer_generator.rb +2 -0
  63. data/lib/grape/active_model_serializers.rb +2 -0
  64. data/lib/grape/formatters/active_model_serializers.rb +2 -0
  65. data/lib/grape/helpers/active_model_serializers.rb +2 -0
  66. data/lib/tasks/rubocop.rake +8 -1
  67. metadata +53 -328
  68. data/.github/ISSUE_TEMPLATE.md +0 -29
  69. data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
  70. data/.gitignore +0 -35
  71. data/.rubocop.yml +0 -105
  72. data/.simplecov +0 -110
  73. data/.travis.yml +0 -52
  74. data/CODE_OF_CONDUCT.md +0 -74
  75. data/CONTRIBUTING.md +0 -105
  76. data/Gemfile +0 -72
  77. data/Rakefile +0 -74
  78. data/active_model_serializers.gemspec +0 -63
  79. data/appveyor.yml +0 -28
  80. data/bin/bench +0 -171
  81. data/bin/bench_regression +0 -316
  82. data/bin/rubocop +0 -38
  83. data/bin/serve_benchmark +0 -39
  84. data/docs/README.md +0 -41
  85. data/docs/STYLE.md +0 -58
  86. data/docs/general/adapters.md +0 -269
  87. data/docs/general/caching.md +0 -58
  88. data/docs/general/configuration_options.md +0 -185
  89. data/docs/general/deserialization.md +0 -100
  90. data/docs/general/fields.md +0 -31
  91. data/docs/general/getting_started.md +0 -133
  92. data/docs/general/instrumentation.md +0 -40
  93. data/docs/general/key_transforms.md +0 -40
  94. data/docs/general/logging.md +0 -21
  95. data/docs/general/rendering.md +0 -293
  96. data/docs/general/serializers.md +0 -483
  97. data/docs/how-open-source-maintained.jpg +0 -0
  98. data/docs/howto/add_pagination_links.md +0 -138
  99. data/docs/howto/add_relationship_links.md +0 -140
  100. data/docs/howto/add_root_key.md +0 -62
  101. data/docs/howto/grape_integration.md +0 -42
  102. data/docs/howto/outside_controller_use.md +0 -66
  103. data/docs/howto/passing_arbitrary_options.md +0 -27
  104. data/docs/howto/serialize_poro.md +0 -73
  105. data/docs/howto/test.md +0 -154
  106. data/docs/howto/upgrade_from_0_8_to_0_10.md +0 -265
  107. data/docs/integrations/ember-and-json-api.md +0 -147
  108. data/docs/integrations/grape.md +0 -19
  109. data/docs/jsonapi/errors.md +0 -56
  110. data/docs/jsonapi/schema/schema.json +0 -366
  111. data/docs/jsonapi/schema.md +0 -151
  112. data/docs/rfcs/0000-namespace.md +0 -106
  113. data/docs/rfcs/template.md +0 -15
  114. data/test/action_controller/adapter_selector_test.rb +0 -62
  115. data/test/action_controller/explicit_serializer_test.rb +0 -135
  116. data/test/action_controller/json/include_test.rb +0 -246
  117. data/test/action_controller/json_api/deserialization_test.rb +0 -112
  118. data/test/action_controller/json_api/errors_test.rb +0 -40
  119. data/test/action_controller/json_api/fields_test.rb +0 -66
  120. data/test/action_controller/json_api/linked_test.rb +0 -202
  121. data/test/action_controller/json_api/pagination_test.rb +0 -124
  122. data/test/action_controller/json_api/transform_test.rb +0 -189
  123. data/test/action_controller/lookup_proc_test.rb +0 -49
  124. data/test/action_controller/namespace_lookup_test.rb +0 -232
  125. data/test/action_controller/serialization_scope_name_test.rb +0 -235
  126. data/test/action_controller/serialization_test.rb +0 -472
  127. data/test/active_model_serializers/adapter_for_test.rb +0 -208
  128. data/test/active_model_serializers/json_pointer_test.rb +0 -22
  129. data/test/active_model_serializers/logging_test.rb +0 -77
  130. data/test/active_model_serializers/model_test.rb +0 -142
  131. data/test/active_model_serializers/railtie_test_isolated.rb +0 -68
  132. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +0 -161
  133. data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -71
  134. data/test/active_model_serializers/test/schema_test.rb +0 -131
  135. data/test/active_model_serializers/test/serializer_test.rb +0 -62
  136. data/test/active_record_test.rb +0 -9
  137. data/test/adapter/attributes_test.rb +0 -40
  138. data/test/adapter/deprecation_test.rb +0 -100
  139. data/test/adapter/json/belongs_to_test.rb +0 -45
  140. data/test/adapter/json/collection_test.rb +0 -104
  141. data/test/adapter/json/has_many_test.rb +0 -53
  142. data/test/adapter/json/transform_test.rb +0 -93
  143. data/test/adapter/json_api/belongs_to_test.rb +0 -155
  144. data/test/adapter/json_api/collection_test.rb +0 -96
  145. data/test/adapter/json_api/errors_test.rb +0 -76
  146. data/test/adapter/json_api/fields_test.rb +0 -96
  147. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -96
  148. data/test/adapter/json_api/has_many_test.rb +0 -173
  149. data/test/adapter/json_api/has_one_test.rb +0 -80
  150. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +0 -213
  151. data/test/adapter/json_api/json_api_test.rb +0 -33
  152. data/test/adapter/json_api/linked_test.rb +0 -413
  153. data/test/adapter/json_api/links_test.rb +0 -95
  154. data/test/adapter/json_api/pagination_links_test.rb +0 -206
  155. data/test/adapter/json_api/parse_test.rb +0 -137
  156. data/test/adapter/json_api/relationship_test.rb +0 -397
  157. data/test/adapter/json_api/resource_meta_test.rb +0 -100
  158. data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -82
  159. data/test/adapter/json_api/transform_test.rb +0 -512
  160. data/test/adapter/json_api/type_test.rb +0 -193
  161. data/test/adapter/json_test.rb +0 -46
  162. data/test/adapter/null_test.rb +0 -22
  163. data/test/adapter/polymorphic_test.rb +0 -218
  164. data/test/adapter_test.rb +0 -67
  165. data/test/array_serializer_test.rb +0 -22
  166. data/test/benchmark/app.rb +0 -65
  167. data/test/benchmark/benchmarking_support.rb +0 -67
  168. data/test/benchmark/bm_active_record.rb +0 -81
  169. data/test/benchmark/bm_adapter.rb +0 -38
  170. data/test/benchmark/bm_caching.rb +0 -119
  171. data/test/benchmark/bm_lookup_chain.rb +0 -83
  172. data/test/benchmark/bm_transform.rb +0 -45
  173. data/test/benchmark/config.ru +0 -3
  174. data/test/benchmark/controllers.rb +0 -83
  175. data/test/benchmark/fixtures.rb +0 -219
  176. data/test/cache_test.rb +0 -651
  177. data/test/collection_serializer_test.rb +0 -127
  178. data/test/fixtures/active_record.rb +0 -113
  179. data/test/fixtures/poro.rb +0 -225
  180. data/test/generators/scaffold_controller_generator_test.rb +0 -24
  181. data/test/generators/serializer_generator_test.rb +0 -75
  182. data/test/grape_test.rb +0 -196
  183. data/test/lint_test.rb +0 -49
  184. data/test/logger_test.rb +0 -20
  185. data/test/poro_test.rb +0 -9
  186. data/test/serializable_resource_test.rb +0 -79
  187. data/test/serializers/association_macros_test.rb +0 -37
  188. data/test/serializers/associations_test.rb +0 -468
  189. data/test/serializers/attribute_test.rb +0 -153
  190. data/test/serializers/attributes_test.rb +0 -52
  191. data/test/serializers/caching_configuration_test_isolated.rb +0 -170
  192. data/test/serializers/configuration_test.rb +0 -32
  193. data/test/serializers/fieldset_test.rb +0 -14
  194. data/test/serializers/meta_test.rb +0 -202
  195. data/test/serializers/options_test.rb +0 -32
  196. data/test/serializers/read_attribute_for_serialization_test.rb +0 -79
  197. data/test/serializers/reflection_test.rb +0 -427
  198. data/test/serializers/root_test.rb +0 -21
  199. data/test/serializers/serialization_test.rb +0 -55
  200. data/test/serializers/serializer_for_test.rb +0 -136
  201. data/test/serializers/serializer_for_with_namespace_test.rb +0 -88
  202. data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
  203. data/test/support/isolated_unit.rb +0 -84
  204. data/test/support/rails5_shims.rb +0 -53
  205. data/test/support/rails_app.rb +0 -38
  206. data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
  207. data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +0 -6
  208. data/test/support/schemas/custom/show.json +0 -7
  209. data/test/support/schemas/hyper_schema.json +0 -93
  210. data/test/support/schemas/render_using_json_api.json +0 -43
  211. data/test/support/schemas/simple_json_pointers.json +0 -10
  212. data/test/support/serialization_testing.rb +0 -79
  213. data/test/test_helper.rb +0 -70
@@ -1,468 +0,0 @@
1
- require 'test_helper'
2
- module ActiveModel
3
- class Serializer
4
- class AssociationsTest < ActiveSupport::TestCase
5
- class ModelWithoutSerializer < ::Model
6
- attributes :id, :name
7
- end
8
-
9
- def setup
10
- @author = Author.new(name: 'Steve K.')
11
- @author.bio = nil
12
- @author.roles = []
13
- @blog = Blog.new(name: 'AMS Blog')
14
- @post = Post.new(title: 'New Post', body: 'Body')
15
- @tag = ModelWithoutSerializer.new(id: 'tagid', name: '#hashtagged')
16
- @comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
17
- @post.comments = [@comment]
18
- @post.tags = [@tag]
19
- @post.blog = @blog
20
- @comment.post = @post
21
- @comment.author = nil
22
- @post.author = @author
23
- @author.posts = [@post]
24
-
25
- @post_serializer = PostSerializer.new(@post, custom_options: true)
26
- @author_serializer = AuthorSerializer.new(@author)
27
- @comment_serializer = CommentSerializer.new(@comment)
28
- end
29
-
30
- def test_has_many_and_has_one
31
- @author_serializer.associations.each do |association|
32
- key = association.key
33
- serializer = association.lazy_association.serializer
34
-
35
- case key
36
- when :posts
37
- assert_equal true, association.include_data?
38
- assert_kind_of(ActiveModelSerializers.config.collection_serializer, serializer)
39
- when :bio
40
- assert_equal true, association.include_data?
41
- assert_nil serializer
42
- when :roles
43
- assert_equal true, association.include_data?
44
- assert_kind_of(ActiveModelSerializers.config.collection_serializer, serializer)
45
- else
46
- flunk "Unknown association: #{key}"
47
- end
48
- end
49
- end
50
-
51
- def test_has_many_with_no_serializer
52
- post_serializer_class = Class.new(ActiveModel::Serializer) do
53
- attributes :id
54
- has_many :tags
55
- end
56
- post_serializer_class.new(@post).associations.each do |association|
57
- key = association.key
58
- serializer = association.lazy_association.serializer
59
-
60
- assert_equal :tags, key
61
- assert_nil serializer
62
- assert_equal [{ id: 'tagid', name: '#hashtagged' }].to_json, association.virtual_value.to_json
63
- end
64
- end
65
-
66
- def test_serializer_options_are_passed_into_associations_serializers
67
- association = @post_serializer
68
- .associations
69
- .detect { |assoc| assoc.key == :comments }
70
-
71
- comment_serializer = association.lazy_association.serializer.first
72
- class << comment_serializer
73
- def custom_options
74
- instance_options
75
- end
76
- end
77
- assert comment_serializer.custom_options.fetch(:custom_options)
78
- end
79
-
80
- def test_belongs_to
81
- @comment_serializer.associations.each do |association|
82
- key = association.key
83
- serializer = association.lazy_association.serializer
84
-
85
- case key
86
- when :post
87
- assert_kind_of(PostSerializer, serializer)
88
- when :author
89
- assert_nil serializer
90
- else
91
- flunk "Unknown association: #{key}"
92
- end
93
-
94
- assert_equal true, association.include_data?
95
- end
96
- end
97
-
98
- def test_belongs_to_with_custom_method
99
- assert(
100
- @post_serializer.associations.any? do |association|
101
- association.key == :blog
102
- end
103
- )
104
- end
105
-
106
- def test_associations_inheritance
107
- inherited_klass = Class.new(PostSerializer)
108
-
109
- assert_equal(PostSerializer._reflections, inherited_klass._reflections)
110
- end
111
-
112
- def test_associations_inheritance_with_new_association
113
- inherited_klass = Class.new(PostSerializer) do
114
- has_many :top_comments, serializer: CommentSerializer
115
- end
116
-
117
- assert(
118
- PostSerializer._reflections.values.all? do |reflection|
119
- inherited_klass._reflections.values.include?(reflection)
120
- end
121
- )
122
-
123
- assert(
124
- inherited_klass._reflections.values.any? do |reflection|
125
- reflection.name == :top_comments
126
- end
127
- )
128
- end
129
-
130
- def test_associations_custom_keys
131
- serializer = PostWithCustomKeysSerializer.new(@post)
132
-
133
- expected_association_keys = serializer.associations.map(&:key)
134
-
135
- assert expected_association_keys.include? :reviews
136
- assert expected_association_keys.include? :writer
137
- assert expected_association_keys.include? :site
138
- end
139
-
140
- class BelongsToBlogModel < ::Model
141
- attributes :id, :title
142
- associations :blog
143
- end
144
- class BelongsToBlogModelSerializer < ActiveModel::Serializer
145
- type :posts
146
- belongs_to :blog
147
- end
148
-
149
- def test_belongs_to_doesnt_load_record
150
- attributes = { id: 1, title: 'Belongs to Blog', blog: Blog.new(id: 5) }
151
- post = BelongsToBlogModel.new(attributes)
152
- class << post
153
- def blog
154
- fail 'should use blog_id'
155
- end
156
-
157
- def blog_id
158
- 5
159
- end
160
- end
161
-
162
- actual =
163
- begin
164
- original_option = BelongsToBlogModelSerializer.config.jsonapi_use_foreign_key_on_belongs_to_relationship
165
- BelongsToBlogModelSerializer.config.jsonapi_use_foreign_key_on_belongs_to_relationship = true
166
- serializable(post, adapter: :json_api, serializer: BelongsToBlogModelSerializer).as_json
167
- ensure
168
- BelongsToBlogModelSerializer.config.jsonapi_use_foreign_key_on_belongs_to_relationship = original_option
169
- end
170
- expected = { data: { id: '1', type: 'posts', relationships: { blog: { data: { id: '5', type: 'blogs' } } } } }
171
-
172
- assert_equal expected, actual
173
- end
174
-
175
- class ExternalBlog < Blog
176
- attributes :external_id
177
- end
178
- class BelongsToExternalBlogModel < ::Model
179
- attributes :id, :title, :external_blog_id
180
- associations :external_blog
181
- end
182
- class BelongsToExternalBlogModelSerializer < ActiveModel::Serializer
183
- type :posts
184
- belongs_to :external_blog
185
-
186
- def external_blog_id
187
- object.external_blog.external_id
188
- end
189
- end
190
-
191
- def test_belongs_to_allows_id_overwriting
192
- attributes = {
193
- id: 1,
194
- title: 'Title',
195
- external_blog: ExternalBlog.new(id: 5, external_id: 6)
196
- }
197
- post = BelongsToExternalBlogModel.new(attributes)
198
-
199
- actual =
200
- begin
201
- original_option = BelongsToExternalBlogModelSerializer.config.jsonapi_use_foreign_key_on_belongs_to_relationship
202
- BelongsToExternalBlogModelSerializer.config.jsonapi_use_foreign_key_on_belongs_to_relationship = true
203
- serializable(post, adapter: :json_api, serializer: BelongsToExternalBlogModelSerializer).as_json
204
- ensure
205
- BelongsToExternalBlogModelSerializer.config.jsonapi_use_foreign_key_on_belongs_to_relationship = original_option
206
- end
207
- expected = { data: { id: '1', type: 'posts', relationships: { :'external-blog' => { data: { id: '6', type: 'external-blogs' } } } } }
208
-
209
- assert_equal expected, actual
210
- end
211
-
212
- class InlineAssociationTestPostSerializer < ActiveModel::Serializer
213
- has_many :comments
214
- has_many :comments, key: :last_comments do
215
- object.comments.last(1)
216
- end
217
- end
218
-
219
- def test_virtual_attribute_block
220
- comment1 = ::ARModels::Comment.create!(contents: 'first comment')
221
- comment2 = ::ARModels::Comment.create!(contents: 'last comment')
222
- post = ::ARModels::Post.create!(
223
- title: 'inline association test',
224
- body: 'etc',
225
- comments: [comment1, comment2]
226
- )
227
- actual = serializable(post, adapter: :attributes, serializer: InlineAssociationTestPostSerializer).as_json
228
- expected = {
229
- comments: [
230
- { id: 1, contents: 'first comment' },
231
- { id: 2, contents: 'last comment' }
232
- ],
233
- last_comments: [
234
- { id: 2, contents: 'last comment' }
235
- ]
236
- }
237
-
238
- assert_equal expected, actual
239
- ensure
240
- ::ARModels::Post.delete_all
241
- ::ARModels::Comment.delete_all
242
- end
243
-
244
- class NamespacedResourcesTest < ActiveSupport::TestCase
245
- class ResourceNamespace
246
- class Post < ::Model
247
- associations :comments, :author, :description
248
- end
249
- class Comment < ::Model; end
250
- class Author < ::Model; end
251
- class Description < ::Model; end
252
- class PostSerializer < ActiveModel::Serializer
253
- has_many :comments
254
- belongs_to :author
255
- has_one :description
256
- end
257
- class CommentSerializer < ActiveModel::Serializer; end
258
- class AuthorSerializer < ActiveModel::Serializer; end
259
- class DescriptionSerializer < ActiveModel::Serializer; end
260
- end
261
-
262
- def setup
263
- @comment = ResourceNamespace::Comment.new
264
- @author = ResourceNamespace::Author.new
265
- @description = ResourceNamespace::Description.new
266
- @post = ResourceNamespace::Post.new(comments: [@comment],
267
- author: @author,
268
- description: @description)
269
- @post_serializer = ResourceNamespace::PostSerializer.new(@post)
270
- end
271
-
272
- def test_associations_namespaced_resources
273
- @post_serializer.associations.each do |association|
274
- case association.key
275
- when :comments
276
- assert_instance_of(ResourceNamespace::CommentSerializer, association.lazy_association.serializer.first)
277
- when :author
278
- assert_instance_of(ResourceNamespace::AuthorSerializer, association.lazy_association.serializer)
279
- when :description
280
- assert_instance_of(ResourceNamespace::DescriptionSerializer, association.lazy_association.serializer)
281
- else
282
- flunk "Unknown association: #{key}"
283
- end
284
- end
285
- end
286
- end
287
-
288
- class NestedSerializersTest < ActiveSupport::TestCase
289
- class Post < ::Model
290
- associations :comments, :author, :description
291
- end
292
- class Comment < ::Model; end
293
- class Author < ::Model; end
294
- class Description < ::Model; end
295
- class PostSerializer < ActiveModel::Serializer
296
- has_many :comments
297
- class CommentSerializer < ActiveModel::Serializer; end
298
- belongs_to :author
299
- class AuthorSerializer < ActiveModel::Serializer; end
300
- has_one :description
301
- class DescriptionSerializer < ActiveModel::Serializer; end
302
- end
303
-
304
- def setup
305
- @comment = Comment.new
306
- @author = Author.new
307
- @description = Description.new
308
- @post = Post.new(comments: [@comment],
309
- author: @author,
310
- description: @description)
311
- @post_serializer = PostSerializer.new(@post)
312
- end
313
-
314
- def test_associations_namespaced_resources
315
- @post_serializer.associations.each do |association|
316
- case association.key
317
- when :comments
318
- assert_instance_of(PostSerializer::CommentSerializer, association.lazy_association.serializer.first)
319
- when :author
320
- assert_instance_of(PostSerializer::AuthorSerializer, association.lazy_association.serializer)
321
- when :description
322
- assert_instance_of(PostSerializer::DescriptionSerializer, association.lazy_association.serializer)
323
- else
324
- flunk "Unknown association: #{key}"
325
- end
326
- end
327
- end
328
-
329
- # rubocop:disable Metrics/AbcSize
330
- def test_conditional_associations
331
- model = Class.new(::Model) do
332
- attributes :true, :false
333
- associations :something
334
- end.new(true: true, false: false)
335
-
336
- scenarios = [
337
- { options: { if: :true }, included: true },
338
- { options: { if: :false }, included: false },
339
- { options: { unless: :false }, included: true },
340
- { options: { unless: :true }, included: false },
341
- { options: { if: 'object.true' }, included: true },
342
- { options: { if: 'object.false' }, included: false },
343
- { options: { unless: 'object.false' }, included: true },
344
- { options: { unless: 'object.true' }, included: false },
345
- { options: { if: -> { object.true } }, included: true },
346
- { options: { if: -> { object.false } }, included: false },
347
- { options: { unless: -> { object.false } }, included: true },
348
- { options: { unless: -> { object.true } }, included: false },
349
- { options: { if: -> (s) { s.object.true } }, included: true },
350
- { options: { if: -> (s) { s.object.false } }, included: false },
351
- { options: { unless: -> (s) { s.object.false } }, included: true },
352
- { options: { unless: -> (s) { s.object.true } }, included: false }
353
- ]
354
-
355
- scenarios.each do |s|
356
- serializer = Class.new(ActiveModel::Serializer) do
357
- belongs_to :something, s[:options]
358
-
359
- def true
360
- true
361
- end
362
-
363
- def false
364
- false
365
- end
366
- end
367
-
368
- hash = serializable(model, serializer: serializer).serializable_hash
369
- assert_equal(s[:included], hash.key?(:something), "Error with #{s[:options]}")
370
- end
371
- end
372
-
373
- def test_illegal_conditional_associations
374
- exception = assert_raises(TypeError) do
375
- Class.new(ActiveModel::Serializer) do
376
- belongs_to :x, if: nil
377
- end
378
- end
379
-
380
- assert_match(/:if should be a Symbol, String or Proc/, exception.message)
381
- end
382
- end
383
-
384
- class InheritedSerializerTest < ActiveSupport::TestCase
385
- class PostSerializer < ActiveModel::Serializer
386
- belongs_to :author
387
- has_many :comments
388
- belongs_to :blog
389
- end
390
-
391
- class InheritedPostSerializer < PostSerializer
392
- belongs_to :author, polymorphic: true
393
- has_many :comments, key: :reviews
394
- end
395
-
396
- class AuthorSerializer < ActiveModel::Serializer
397
- has_many :posts
398
- has_many :roles
399
- has_one :bio
400
- end
401
-
402
- class InheritedAuthorSerializer < AuthorSerializer
403
- has_many :roles, polymorphic: true
404
- has_one :bio, polymorphic: true
405
- end
406
-
407
- def setup
408
- @author = Author.new(name: 'Steve K.')
409
- @post = Post.new(title: 'New Post', body: 'Body')
410
- @post_serializer = PostSerializer.new(@post)
411
- @author_serializer = AuthorSerializer.new(@author)
412
- @inherited_post_serializer = InheritedPostSerializer.new(@post)
413
- @inherited_author_serializer = InheritedAuthorSerializer.new(@author)
414
- @author_associations = @author_serializer.associations.to_a.sort_by(&:name)
415
- @inherited_author_associations = @inherited_author_serializer.associations.to_a.sort_by(&:name)
416
- @post_associations = @post_serializer.associations.to_a
417
- @inherited_post_associations = @inherited_post_serializer.associations.to_a
418
- end
419
-
420
- test 'an author serializer must have [posts,roles,bio] associations' do
421
- expected = [:posts, :roles, :bio].sort
422
- result = @author_serializer.associations.map(&:name).sort
423
- assert_equal(result, expected)
424
- end
425
-
426
- test 'a post serializer must have [author,comments,blog] associations' do
427
- expected = [:author, :comments, :blog].sort
428
- result = @post_serializer.associations.map(&:name).sort
429
- assert_equal(result, expected)
430
- end
431
-
432
- test 'a serializer inheriting from another serializer can redefine has_many and has_one associations' do
433
- expected = [:roles, :bio].sort
434
- result = (@inherited_author_associations.map(&:reflection) - @author_associations.map(&:reflection)).map(&:name)
435
- assert_equal(result, expected)
436
- assert_equal [true, false, true], @inherited_author_associations.map(&:polymorphic?)
437
- assert_equal [false, false, false], @author_associations.map(&:polymorphic?)
438
- end
439
-
440
- test 'a serializer inheriting from another serializer can redefine belongs_to associations' do
441
- assert_equal [:author, :comments, :blog], @post_associations.map(&:name)
442
- assert_equal [:author, :comments, :blog, :comments], @inherited_post_associations.map(&:name)
443
-
444
- refute @post_associations.detect { |assoc| assoc.name == :author }.polymorphic?
445
- assert @inherited_post_associations.detect { |assoc| assoc.name == :author }.polymorphic?
446
-
447
- refute @post_associations.detect { |assoc| assoc.name == :comments }.key?
448
- original_comment_assoc, new_comments_assoc = @inherited_post_associations.select { |assoc| assoc.name == :comments }
449
- refute original_comment_assoc.key?
450
- assert_equal :reviews, new_comments_assoc.key
451
-
452
- original_blog = @post_associations.detect { |assoc| assoc.name == :blog }
453
- inherited_blog = @inherited_post_associations.detect { |assoc| assoc.name == :blog }
454
- original_parent_serializer = original_blog.lazy_association.association_options.delete(:parent_serializer)
455
- inherited_parent_serializer = inherited_blog.lazy_association.association_options.delete(:parent_serializer)
456
- assert_equal PostSerializer, original_parent_serializer.class
457
- assert_equal InheritedPostSerializer, inherited_parent_serializer.class
458
- end
459
-
460
- test 'a serializer inheriting from another serializer can have an additional association with the same name but with different key' do
461
- expected = [:author, :comments, :blog, :reviews].sort
462
- result = @inherited_post_serializer.associations.map(&:key).sort
463
- assert_equal(result, expected)
464
- end
465
- end
466
- end
467
- end
468
- end
@@ -1,153 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ActiveModel
4
- class Serializer
5
- class AttributeTest < ActiveSupport::TestCase
6
- def setup
7
- @blog = Blog.new(id: 1, name: 'AMS Hints', type: 'stuff')
8
- @blog_serializer = AlternateBlogSerializer.new(@blog)
9
- end
10
-
11
- def test_attributes_definition
12
- assert_equal([:id, :title],
13
- @blog_serializer.class._attributes)
14
- end
15
-
16
- def test_json_serializable_hash
17
- adapter = ActiveModelSerializers::Adapter::Json.new(@blog_serializer)
18
- assert_equal({ blog: { id: 1, title: 'AMS Hints' } }, adapter.serializable_hash)
19
- end
20
-
21
- def test_attribute_inheritance_with_key
22
- inherited_klass = Class.new(AlternateBlogSerializer)
23
- blog_serializer = inherited_klass.new(@blog)
24
- adapter = ActiveModelSerializers::Adapter::Attributes.new(blog_serializer)
25
- assert_equal({ id: 1, title: 'AMS Hints' }, adapter.serializable_hash)
26
- end
27
-
28
- def test_multiple_calls_with_the_same_attribute
29
- serializer_class = Class.new(ActiveModel::Serializer) do
30
- attribute :title
31
- attribute :title
32
- end
33
-
34
- assert_equal([:title], serializer_class._attributes)
35
- end
36
-
37
- def test_id_attribute_override
38
- serializer = Class.new(ActiveModel::Serializer) do
39
- attribute :name, key: :id
40
- end
41
-
42
- adapter = ActiveModelSerializers::Adapter::Json.new(serializer.new(@blog))
43
- assert_equal({ blog: { id: 'AMS Hints' } }, adapter.serializable_hash)
44
- end
45
-
46
- def test_object_attribute_override
47
- serializer = Class.new(ActiveModel::Serializer) do
48
- attribute :name, key: :object
49
- end
50
-
51
- adapter = ActiveModelSerializers::Adapter::Json.new(serializer.new(@blog))
52
- assert_equal({ blog: { object: 'AMS Hints' } }, adapter.serializable_hash)
53
- end
54
-
55
- def test_type_attribute
56
- attribute_serializer = Class.new(ActiveModel::Serializer) do
57
- attribute :id, key: :type
58
- end
59
- attributes_serializer = Class.new(ActiveModel::Serializer) do
60
- attributes :type
61
- end
62
-
63
- adapter = ActiveModelSerializers::Adapter::Json.new(attribute_serializer.new(@blog))
64
- assert_equal({ blog: { type: 1 } }, adapter.serializable_hash)
65
-
66
- adapter = ActiveModelSerializers::Adapter::Json.new(attributes_serializer.new(@blog))
67
- assert_equal({ blog: { type: 'stuff' } }, adapter.serializable_hash)
68
- end
69
-
70
- def test_id_attribute_override_before
71
- serializer = Class.new(ActiveModel::Serializer) do
72
- def id
73
- 'custom'
74
- end
75
-
76
- attribute :id
77
- end
78
-
79
- hash = ActiveModelSerializers::SerializableResource.new(@blog, adapter: :json, serializer: serializer).serializable_hash
80
-
81
- assert_equal('custom', hash[:blog][:id])
82
- end
83
-
84
- class PostWithVirtualAttribute < ::Model; attributes :first_name, :last_name end
85
- class PostWithVirtualAttributeSerializer < ActiveModel::Serializer
86
- attribute :name do
87
- "#{object.first_name} #{object.last_name}"
88
- end
89
- end
90
-
91
- def test_virtual_attribute_block
92
- post = PostWithVirtualAttribute.new(first_name: 'Lucas', last_name: 'Hosseini')
93
- hash = serializable(post).serializable_hash
94
- expected = { name: 'Lucas Hosseini' }
95
-
96
- assert_equal(expected, hash)
97
- end
98
-
99
- # rubocop:disable Metrics/AbcSize
100
- def test_conditional_associations
101
- model = Class.new(::Model) do
102
- attributes :true, :false, :attribute
103
- end.new(true: true, false: false)
104
-
105
- scenarios = [
106
- { options: { if: :true }, included: true },
107
- { options: { if: :false }, included: false },
108
- { options: { unless: :false }, included: true },
109
- { options: { unless: :true }, included: false },
110
- { options: { if: 'object.true' }, included: true },
111
- { options: { if: 'object.false' }, included: false },
112
- { options: { unless: 'object.false' }, included: true },
113
- { options: { unless: 'object.true' }, included: false },
114
- { options: { if: -> { object.true } }, included: true },
115
- { options: { if: -> { object.false } }, included: false },
116
- { options: { unless: -> { object.false } }, included: true },
117
- { options: { unless: -> { object.true } }, included: false },
118
- { options: { if: -> (s) { s.object.true } }, included: true },
119
- { options: { if: -> (s) { s.object.false } }, included: false },
120
- { options: { unless: -> (s) { s.object.false } }, included: true },
121
- { options: { unless: -> (s) { s.object.true } }, included: false }
122
- ]
123
-
124
- scenarios.each do |s|
125
- serializer = Class.new(ActiveModel::Serializer) do
126
- attribute :attribute, s[:options]
127
-
128
- def true
129
- true
130
- end
131
-
132
- def false
133
- false
134
- end
135
- end
136
-
137
- hash = serializable(model, serializer: serializer).serializable_hash
138
- assert_equal(s[:included], hash.key?(:attribute), "Error with #{s[:options]}")
139
- end
140
- end
141
-
142
- def test_illegal_conditional_attributes
143
- exception = assert_raises(TypeError) do
144
- Class.new(ActiveModel::Serializer) do
145
- attribute :x, if: nil
146
- end
147
- end
148
-
149
- assert_match(/:if should be a Symbol, String or Proc/, exception.message)
150
- end
151
- end
152
- end
153
- end
@@ -1,52 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ActiveModel
4
- class Serializer
5
- class AttributesTest < ActiveSupport::TestCase
6
- def setup
7
- @profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
8
- @profile_serializer = ProfileSerializer.new(@profile)
9
- @comment = Comment.new(id: 1, body: 'ZOMG!!', date: '2015')
10
- @serializer_klass = Class.new(CommentSerializer)
11
- @serializer_klass_with_new_attributes = Class.new(CommentSerializer) do
12
- attributes :date, :likes
13
- end
14
- end
15
-
16
- def test_attributes_definition
17
- assert_equal([:name, :description],
18
- @profile_serializer.class._attributes)
19
- end
20
-
21
- def test_attributes_inheritance_definition
22
- assert_equal([:id, :body], @serializer_klass._attributes)
23
- end
24
-
25
- def test_attributes_inheritance
26
- serializer = @serializer_klass.new(@comment)
27
- assert_equal({ id: 1, body: 'ZOMG!!' },
28
- serializer.attributes)
29
- end
30
-
31
- def test_attribute_inheritance_with_new_attribute_definition
32
- assert_equal([:id, :body, :date, :likes], @serializer_klass_with_new_attributes._attributes)
33
- assert_equal([:id, :body], CommentSerializer._attributes)
34
- end
35
-
36
- def test_attribute_inheritance_with_new_attribute
37
- serializer = @serializer_klass_with_new_attributes.new(@comment)
38
- assert_equal({ id: 1, body: 'ZOMG!!', date: '2015', likes: nil },
39
- serializer.attributes)
40
- end
41
-
42
- def test_multiple_calls_with_the_same_attribute
43
- serializer_class = Class.new(ActiveModel::Serializer) do
44
- attributes :id, :title
45
- attributes :id, :title, :title, :body
46
- end
47
-
48
- assert_equal([:id, :title, :body], serializer_class._attributes)
49
- end
50
- end
51
- end
52
- end