active_model_serializers 0.10.0.rc4 → 0.10.0.rc5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ISSUE_TEMPLATE.md +29 -0
  3. data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
  4. data/.gitignore +1 -0
  5. data/.rubocop.yml +19 -1
  6. data/.rubocop_todo.yml +30 -103
  7. data/.simplecov +0 -1
  8. data/.travis.yml +20 -8
  9. data/CHANGELOG.md +89 -5
  10. data/CONTRIBUTING.md +54 -179
  11. data/Gemfile +7 -2
  12. data/{LICENSE.txt → MIT-LICENSE} +0 -0
  13. data/README.md +27 -5
  14. data/Rakefile +44 -16
  15. data/active_model_serializers.gemspec +9 -1
  16. data/appveyor.yml +1 -0
  17. data/bin/bench +171 -0
  18. data/bin/bench_regression +316 -0
  19. data/bin/serve_benchmark +39 -0
  20. data/docs/ARCHITECTURE.md +13 -7
  21. data/docs/README.md +5 -1
  22. data/docs/STYLE.md +58 -0
  23. data/docs/general/adapters.md +99 -16
  24. data/docs/general/configuration_options.md +87 -14
  25. data/docs/general/deserialization.md +100 -0
  26. data/docs/general/getting_started.md +35 -0
  27. data/docs/general/instrumentation.md +1 -1
  28. data/docs/general/key_transforms.md +40 -0
  29. data/docs/general/rendering.md +115 -13
  30. data/docs/general/serializers.md +138 -6
  31. data/docs/howto/add_pagination_links.md +36 -18
  32. data/docs/howto/outside_controller_use.md +4 -4
  33. data/docs/howto/passing_arbitrary_options.md +27 -0
  34. data/docs/jsonapi/errors.md +56 -0
  35. data/docs/jsonapi/schema.md +29 -18
  36. data/docs/rfcs/0000-namespace.md +106 -0
  37. data/docs/rfcs/template.md +15 -0
  38. data/lib/action_controller/serialization.rb +10 -19
  39. data/lib/active_model/serializable_resource.rb +4 -65
  40. data/lib/active_model/serializer.rb +73 -18
  41. data/lib/active_model/serializer/adapter.rb +15 -82
  42. data/lib/active_model/serializer/adapter/attributes.rb +5 -56
  43. data/lib/active_model/serializer/adapter/base.rb +5 -47
  44. data/lib/active_model/serializer/adapter/json.rb +6 -12
  45. data/lib/active_model/serializer/adapter/json_api.rb +5 -213
  46. data/lib/active_model/serializer/adapter/null.rb +7 -3
  47. data/lib/active_model/serializer/array_serializer.rb +3 -3
  48. data/lib/active_model/serializer/association.rb +4 -5
  49. data/lib/active_model/serializer/attributes.rb +1 -1
  50. data/lib/active_model/serializer/caching.rb +56 -5
  51. data/lib/active_model/serializer/collection_serializer.rb +30 -13
  52. data/lib/active_model/serializer/configuration.rb +7 -0
  53. data/lib/active_model/serializer/error_serializer.rb +10 -0
  54. data/lib/active_model/serializer/errors_serializer.rb +27 -0
  55. data/lib/active_model/serializer/links.rb +4 -2
  56. data/lib/active_model/serializer/lint.rb +14 -0
  57. data/lib/active_model/serializer/meta.rb +29 -0
  58. data/lib/active_model/serializer/null.rb +17 -0
  59. data/lib/active_model/serializer/reflection.rb +57 -1
  60. data/lib/active_model/serializer/type.rb +1 -1
  61. data/lib/active_model/serializer/version.rb +1 -1
  62. data/lib/active_model_serializers.rb +17 -0
  63. data/lib/active_model_serializers/adapter.rb +92 -0
  64. data/lib/active_model_serializers/adapter/attributes.rb +94 -0
  65. data/lib/active_model_serializers/adapter/base.rb +90 -0
  66. data/lib/active_model_serializers/adapter/json.rb +11 -0
  67. data/lib/active_model_serializers/adapter/json_api.rb +513 -0
  68. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +213 -0
  69. data/lib/active_model_serializers/adapter/json_api/error.rb +96 -0
  70. data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +49 -0
  71. data/lib/active_model_serializers/adapter/json_api/link.rb +83 -0
  72. data/lib/active_model_serializers/adapter/json_api/meta.rb +37 -0
  73. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +57 -0
  74. data/lib/active_model_serializers/adapter/json_api/relationship.rb +52 -0
  75. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +37 -0
  76. data/lib/active_model_serializers/adapter/null.rb +10 -0
  77. data/lib/active_model_serializers/cached_serializer.rb +87 -0
  78. data/lib/active_model_serializers/callbacks.rb +1 -1
  79. data/lib/active_model_serializers/deprecate.rb +55 -0
  80. data/lib/active_model_serializers/deserialization.rb +2 -2
  81. data/lib/active_model_serializers/fragment_cache.rb +118 -0
  82. data/lib/active_model_serializers/json_pointer.rb +14 -0
  83. data/lib/active_model_serializers/key_transform.rb +70 -0
  84. data/lib/active_model_serializers/logging.rb +4 -1
  85. data/lib/active_model_serializers/model.rb +11 -1
  86. data/lib/active_model_serializers/railtie.rb +9 -1
  87. data/lib/active_model_serializers/register_jsonapi_renderer.rb +64 -0
  88. data/lib/active_model_serializers/serializable_resource.rb +81 -0
  89. data/lib/active_model_serializers/serialization_context.rb +24 -2
  90. data/lib/active_model_serializers/test/schema.rb +2 -2
  91. data/lib/grape/formatters/active_model_serializers.rb +1 -1
  92. data/test/action_controller/adapter_selector_test.rb +1 -1
  93. data/test/action_controller/json_api/deserialization_test.rb +56 -3
  94. data/test/action_controller/json_api/errors_test.rb +41 -0
  95. data/test/action_controller/json_api/linked_test.rb +10 -9
  96. data/test/action_controller/json_api/pagination_test.rb +2 -2
  97. data/test/action_controller/json_api/transform_test.rb +180 -0
  98. data/test/action_controller/serialization_scope_name_test.rb +201 -35
  99. data/test/action_controller/serialization_test.rb +39 -7
  100. data/test/active_model_serializers/adapter_for_test.rb +208 -0
  101. data/test/active_model_serializers/cached_serializer_test.rb +80 -0
  102. data/test/active_model_serializers/fragment_cache_test.rb +34 -0
  103. data/test/active_model_serializers/json_pointer_test.rb +20 -0
  104. data/test/active_model_serializers/key_transform_test.rb +263 -0
  105. data/test/active_model_serializers/logging_test.rb +8 -8
  106. data/test/active_model_serializers/railtie_test_isolated.rb +6 -0
  107. data/test/active_model_serializers/serialization_context_test_isolated.rb +58 -0
  108. data/test/adapter/deprecation_test.rb +100 -0
  109. data/test/adapter/json/belongs_to_test.rb +32 -34
  110. data/test/adapter/json/collection_test.rb +73 -75
  111. data/test/adapter/json/has_many_test.rb +36 -38
  112. data/test/adapter/json/transform_test.rb +93 -0
  113. data/test/adapter/json_api/belongs_to_test.rb +127 -129
  114. data/test/adapter/json_api/collection_test.rb +80 -82
  115. data/test/adapter/json_api/errors_test.rb +78 -0
  116. data/test/adapter/json_api/fields_test.rb +68 -70
  117. data/test/adapter/json_api/has_many_embed_ids_test.rb +32 -34
  118. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +75 -77
  119. data/test/adapter/json_api/has_many_test.rb +121 -123
  120. data/test/adapter/json_api/has_one_test.rb +59 -61
  121. data/test/adapter/json_api/json_api_test.rb +28 -30
  122. data/test/adapter/json_api/linked_test.rb +319 -321
  123. data/test/adapter/json_api/links_test.rb +75 -50
  124. data/test/adapter/json_api/pagination_links_test.rb +115 -82
  125. data/test/adapter/json_api/parse_test.rb +114 -116
  126. data/test/adapter/json_api/relationship_test.rb +161 -0
  127. data/test/adapter/json_api/relationships_test.rb +199 -0
  128. data/test/adapter/json_api/resource_identifier_test.rb +85 -0
  129. data/test/adapter/json_api/resource_meta_test.rb +100 -0
  130. data/test/adapter/json_api/toplevel_jsonapi_test.rb +61 -63
  131. data/test/adapter/json_api/transform_test.rb +500 -0
  132. data/test/adapter/json_api/type_test.rb +61 -0
  133. data/test/adapter/json_test.rb +35 -37
  134. data/test/adapter/null_test.rb +13 -15
  135. data/test/adapter/polymorphic_test.rb +72 -0
  136. data/test/adapter_test.rb +27 -29
  137. data/test/array_serializer_test.rb +7 -8
  138. data/test/benchmark/app.rb +65 -0
  139. data/test/benchmark/benchmarking_support.rb +67 -0
  140. data/test/benchmark/bm_caching.rb +117 -0
  141. data/test/benchmark/bm_transform.rb +34 -0
  142. data/test/benchmark/config.ru +3 -0
  143. data/test/benchmark/controllers.rb +77 -0
  144. data/test/benchmark/fixtures.rb +167 -0
  145. data/test/cache_test.rb +388 -0
  146. data/test/collection_serializer_test.rb +10 -0
  147. data/test/fixtures/active_record.rb +12 -0
  148. data/test/fixtures/poro.rb +28 -3
  149. data/test/grape_test.rb +5 -5
  150. data/test/lint_test.rb +9 -0
  151. data/test/serializable_resource_test.rb +59 -3
  152. data/test/serializers/associations_test.rb +8 -8
  153. data/test/serializers/attribute_test.rb +7 -7
  154. data/test/serializers/caching_configuration_test_isolated.rb +170 -0
  155. data/test/serializers/meta_test.rb +74 -6
  156. data/test/serializers/read_attribute_for_serialization_test.rb +79 -0
  157. data/test/serializers/serialization_test.rb +55 -0
  158. data/test/support/isolated_unit.rb +3 -0
  159. data/test/support/rails5_shims.rb +26 -8
  160. data/test/support/rails_app.rb +38 -18
  161. data/test/support/serialization_testing.rb +5 -5
  162. data/test/test_helper.rb +6 -10
  163. metadata +132 -37
  164. data/docs/DESIGN.textile +7 -1
  165. data/lib/active_model/serializer/adapter/cached_serializer.rb +0 -45
  166. data/lib/active_model/serializer/adapter/fragment_cache.rb +0 -111
  167. data/lib/active_model/serializer/adapter/json/fragment_cache.rb +0 -13
  168. data/lib/active_model/serializer/adapter/json_api/deserialization.rb +0 -207
  169. data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +0 -21
  170. data/lib/active_model/serializer/adapter/json_api/link.rb +0 -44
  171. data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +0 -58
  172. data/test/active_model_serializers/serialization_context_test.rb +0 -18
  173. data/test/adapter/fragment_cache_test.rb +0 -38
  174. data/test/adapter/json_api/resource_type_config_test.rb +0 -71
  175. data/test/serializers/adapter_for_test.rb +0 -166
  176. data/test/serializers/cache_test.rb +0 -209
  177. data/test/support/simplecov.rb +0 -6
  178. data/test/support/stream_capture.rb +0 -50
  179. data/test/support/test_case.rb +0 -19
@@ -39,37 +39,37 @@ module ActiveModel
39
39
  end
40
40
 
41
41
  def test_uses_ams_as_tag
42
- ActiveModel::SerializableResource.new(@post).serializable_hash
42
+ ActiveModelSerializers::SerializableResource.new(@post).serializable_hash
43
43
  assert_match(/\[active_model_serializers\]/, @logger.messages)
44
44
  end
45
45
 
46
46
  def test_logs_when_call_serializable_hash
47
- ActiveModel::SerializableResource.new(@post).serializable_hash
47
+ ActiveModelSerializers::SerializableResource.new(@post).serializable_hash
48
48
  assert_match(/Rendered/, @logger.messages)
49
49
  end
50
50
 
51
51
  def test_logs_when_call_as_json
52
- ActiveModel::SerializableResource.new(@post).as_json
52
+ ActiveModelSerializers::SerializableResource.new(@post).as_json
53
53
  assert_match(/Rendered/, @logger.messages)
54
54
  end
55
55
 
56
56
  def test_logs_when_call_to_json
57
- ActiveModel::SerializableResource.new(@post).to_json
57
+ ActiveModelSerializers::SerializableResource.new(@post).to_json
58
58
  assert_match(/Rendered/, @logger.messages)
59
59
  end
60
60
 
61
61
  def test_logs_correct_serializer
62
- ActiveModel::SerializableResource.new(@post).serializable_hash
62
+ ActiveModelSerializers::SerializableResource.new(@post).serializable_hash
63
63
  assert_match(/PostSerializer/, @logger.messages)
64
64
  end
65
65
 
66
66
  def test_logs_correct_adapter
67
- ActiveModel::SerializableResource.new(@post).serializable_hash
68
- assert_match(/ActiveModel::Serializer::Adapter::Attributes/, @logger.messages)
67
+ ActiveModelSerializers::SerializableResource.new(@post).serializable_hash
68
+ assert_match(/ActiveModelSerializers::Adapter::Attributes/, @logger.messages)
69
69
  end
70
70
 
71
71
  def test_logs_the_duration
72
- ActiveModel::SerializableResource.new(@post).serializable_hash
72
+ ActiveModelSerializers::SerializableResource.new(@post).serializable_hash
73
73
  assert_match(/\(\d+\.\d+ms\)/, @logger.messages)
74
74
  end
75
75
  end
@@ -18,6 +18,12 @@ class RailtieTest < ActiveSupport::TestCase
18
18
  "ActionController::Serialization should be included in ActionController::Base, but isn't"
19
19
  end
20
20
 
21
+ test 'prepares url_helpers for SerializationContext' do
22
+ assert ActiveModelSerializers::SerializationContext.url_helpers.respond_to? :url_for
23
+ assert_equal Rails.application.routes.default_url_options,
24
+ ActiveModelSerializers::SerializationContext.default_url_options
25
+ end
26
+
21
27
  test 'sets the ActiveModelSerializers.logger to Rails.logger' do
22
28
  refute_nil Rails.logger
23
29
  refute_nil ActiveModelSerializers.logger
@@ -0,0 +1,58 @@
1
+ # Execute this test in isolation
2
+ require 'support/isolated_unit'
3
+ require 'minitest/mock'
4
+
5
+ class SerializationContextTest < ActiveSupport::TestCase
6
+ include ActiveSupport::Testing::Isolation
7
+
8
+ def create_request
9
+ request = Minitest::Mock.new
10
+ request.expect(:original_url, 'original_url')
11
+ request.expect(:query_parameters, 'query_parameters')
12
+ end
13
+
14
+ class WithRails < SerializationContextTest
15
+ setup do
16
+ require 'rails'
17
+ require 'active_model_serializers'
18
+ make_basic_app
19
+ @context = ActiveModelSerializers::SerializationContext.new(create_request)
20
+ end
21
+
22
+ test 'create context with request url and query parameters' do
23
+ assert_equal @context.request_url, 'original_url'
24
+ assert_equal @context.query_parameters, 'query_parameters'
25
+ end
26
+
27
+ test 'url_helpers is set up for Rails url_helpers' do
28
+ assert_equal Module, ActiveModelSerializers::SerializationContext.url_helpers.class
29
+ assert ActiveModelSerializers::SerializationContext.url_helpers.respond_to? :url_for
30
+ end
31
+
32
+ test 'default_url_options returns Rails.application.routes.default_url_options' do
33
+ assert_equal Rails.application.routes.default_url_options,
34
+ ActiveModelSerializers::SerializationContext.default_url_options
35
+ end
36
+ end
37
+
38
+ class WithoutRails < SerializationContextTest
39
+ setup do
40
+ require 'active_model_serializers/serialization_context'
41
+ @context = ActiveModelSerializers::SerializationContext.new(create_request)
42
+ end
43
+
44
+ test 'create context with request url and query parameters' do
45
+ assert_equal @context.request_url, 'original_url'
46
+ assert_equal @context.query_parameters, 'query_parameters'
47
+ end
48
+
49
+ test 'url_helpers is a module when Rails is not present' do
50
+ assert_equal Module, ActiveModelSerializers::SerializationContext.url_helpers.class
51
+ refute ActiveModelSerializers::SerializationContext.url_helpers.respond_to? :url_for
52
+ end
53
+
54
+ test 'default_url_options return a Hash' do
55
+ assert Hash, ActiveModelSerializers::SerializationContext.default_url_options.class
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,100 @@
1
+ require 'test_helper'
2
+ module ActiveModel
3
+ class Serializer
4
+ module Adapter
5
+ class DeprecationTest < ActiveSupport::TestCase
6
+ class PostSerializer < ActiveModel::Serializer
7
+ attribute :body
8
+ end
9
+ setup do
10
+ post = Post.new(id: 1, body: 'Hello')
11
+ @serializer = PostSerializer.new(post)
12
+ end
13
+
14
+ def test_null_adapter_serialization_deprecation
15
+ expected = {}
16
+ assert_deprecated do
17
+ assert_equal(expected, Null.new(@serializer).as_json)
18
+ end
19
+ end
20
+
21
+ def test_json_adapter_serialization_deprecation
22
+ expected = { post: { body: 'Hello' } }
23
+ assert_deprecated do
24
+ assert_equal(expected, Json.new(@serializer).as_json)
25
+ end
26
+ end
27
+
28
+ def test_jsonapi_adapter_serialization_deprecation
29
+ expected = {
30
+ data: {
31
+ id: '1',
32
+ type: 'posts',
33
+ attributes: {
34
+ body: 'Hello'
35
+ }
36
+ }
37
+ }
38
+ assert_deprecated do
39
+ assert_equal(expected, JsonApi.new(@serializer).as_json)
40
+ end
41
+ end
42
+
43
+ def test_attributes_adapter_serialization_deprecation
44
+ expected = { body: 'Hello' }
45
+ assert_deprecated do
46
+ assert_equal(expected, Attributes.new(@serializer).as_json)
47
+ end
48
+ end
49
+
50
+ def test_adapter_create_deprecation
51
+ assert_deprecated do
52
+ Adapter.create(@serializer)
53
+ end
54
+ end
55
+
56
+ def test_adapter_adapter_map_deprecation
57
+ assert_deprecated do
58
+ Adapter.adapter_map
59
+ end
60
+ end
61
+
62
+ def test_adapter_adapters_deprecation
63
+ assert_deprecated do
64
+ Adapter.adapters
65
+ end
66
+ end
67
+
68
+ def test_adapter_adapter_class_deprecation
69
+ assert_deprecated do
70
+ Adapter.adapter_class(:json_api)
71
+ end
72
+ end
73
+
74
+ def test_adapter_register_deprecation
75
+ assert_deprecated do
76
+ begin
77
+ Adapter.register(:test, Class.new)
78
+ ensure
79
+ Adapter.adapter_map.delete('test')
80
+ end
81
+ end
82
+ end
83
+
84
+ def test_adapter_lookup_deprecation
85
+ assert_deprecated do
86
+ Adapter.lookup(:json_api)
87
+ end
88
+ end
89
+
90
+ private
91
+
92
+ def assert_deprecated
93
+ assert_output(nil, /deprecated/) do
94
+ yield
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
@@ -1,45 +1,43 @@
1
1
  require 'test_helper'
2
2
 
3
- module ActiveModel
4
- class Serializer
5
- module Adapter
6
- class Json
7
- class BelongsToTest < ActiveSupport::TestCase
8
- def setup
9
- @post = Post.new(id: 42, title: 'New Post', body: 'Body')
10
- @anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
11
- @comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
12
- @post.comments = [@comment]
13
- @anonymous_post.comments = []
14
- @comment.post = @post
15
- @comment.author = nil
16
- @anonymous_post.author = nil
17
- @blog = Blog.new(id: 1, name: 'My Blog!!')
18
- @post.blog = @blog
19
- @anonymous_post.blog = nil
3
+ module ActiveModelSerializers
4
+ module Adapter
5
+ class Json
6
+ class BelongsToTest < ActiveSupport::TestCase
7
+ def setup
8
+ @post = Post.new(id: 42, title: 'New Post', body: 'Body')
9
+ @anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
10
+ @comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
11
+ @post.comments = [@comment]
12
+ @anonymous_post.comments = []
13
+ @comment.post = @post
14
+ @comment.author = nil
15
+ @anonymous_post.author = nil
16
+ @blog = Blog.new(id: 1, name: 'My Blog!!')
17
+ @post.blog = @blog
18
+ @anonymous_post.blog = nil
20
19
 
21
- @serializer = CommentSerializer.new(@comment)
22
- @adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
23
- ActionController::Base.cache_store.clear
24
- end
20
+ @serializer = CommentSerializer.new(@comment)
21
+ @adapter = ActiveModelSerializers::Adapter::Json.new(@serializer)
22
+ ActionController::Base.cache_store.clear
23
+ end
25
24
 
26
- def test_includes_post
27
- assert_equal({ id: 42, title: 'New Post', body: 'Body' }, @adapter.serializable_hash[:comment][:post])
28
- end
25
+ def test_includes_post
26
+ assert_equal({ id: 42, title: 'New Post', body: 'Body' }, @adapter.serializable_hash[:comment][:post])
27
+ end
29
28
 
30
- def test_include_nil_author
31
- serializer = PostSerializer.new(@anonymous_post)
32
- adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
29
+ def test_include_nil_author
30
+ serializer = PostSerializer.new(@anonymous_post)
31
+ adapter = ActiveModelSerializers::Adapter::Json.new(serializer)
33
32
 
34
- assert_equal({ post: { title: 'Hello!!', body: 'Hello, world!!', id: 43, comments: [], blog: { id: 999, name: 'Custom blog' }, author: nil } }, adapter.serializable_hash)
35
- end
33
+ assert_equal({ post: { title: 'Hello!!', body: 'Hello, world!!', id: 43, comments: [], blog: { id: 999, name: 'Custom blog' }, author: nil } }, adapter.serializable_hash)
34
+ end
36
35
 
37
- def test_include_nil_author_with_specified_serializer
38
- serializer = PostPreviewSerializer.new(@anonymous_post)
39
- adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
36
+ def test_include_nil_author_with_specified_serializer
37
+ serializer = PostPreviewSerializer.new(@anonymous_post)
38
+ adapter = ActiveModelSerializers::Adapter::Json.new(serializer)
40
39
 
41
- assert_equal({ post: { title: 'Hello!!', body: 'Hello, world!!', id: 43, comments: [], author: nil } }, adapter.serializable_hash)
42
- end
40
+ assert_equal({ post: { title: 'Hello!!', body: 'Hello, world!!', id: 43, comments: [], author: nil } }, adapter.serializable_hash)
43
41
  end
44
42
  end
45
43
  end
@@ -1,90 +1,88 @@
1
1
  require 'test_helper'
2
2
 
3
- module ActiveModel
4
- class Serializer
5
- module Adapter
6
- class Json
7
- class Collection < ActiveSupport::TestCase
8
- def setup
9
- @author = Author.new(id: 1, name: 'Steve K.')
10
- @first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
11
- @second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
12
- @first_post.comments = []
13
- @second_post.comments = []
14
- @first_post.author = @author
15
- @second_post.author = @author
16
- @blog = Blog.new(id: 1, name: 'My Blog!!')
17
- @first_post.blog = @blog
18
- @second_post.blog = nil
3
+ module ActiveModelSerializers
4
+ module Adapter
5
+ class Json
6
+ class Collection < ActiveSupport::TestCase
7
+ def setup
8
+ @author = Author.new(id: 1, name: 'Steve K.')
9
+ @first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
10
+ @second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
11
+ @first_post.comments = []
12
+ @second_post.comments = []
13
+ @first_post.author = @author
14
+ @second_post.author = @author
15
+ @blog = Blog.new(id: 1, name: 'My Blog!!')
16
+ @first_post.blog = @blog
17
+ @second_post.blog = nil
19
18
 
20
- ActionController::Base.cache_store.clear
21
- end
19
+ ActionController::Base.cache_store.clear
20
+ end
22
21
 
23
- def test_with_serializer_option
24
- @blog.special_attribute = 'Special'
25
- @blog.articles = [@first_post, @second_post]
26
- serializer = CollectionSerializer.new([@blog], serializer: CustomBlogSerializer)
27
- adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
22
+ def test_with_serializer_option
23
+ @blog.special_attribute = 'Special'
24
+ @blog.articles = [@first_post, @second_post]
25
+ serializer = ActiveModel::Serializer::CollectionSerializer.new([@blog], serializer: CustomBlogSerializer)
26
+ adapter = ActiveModelSerializers::Adapter::Json.new(serializer)
28
27
 
29
- expected = { blogs: [{
30
- id: 1,
31
- special_attribute: 'Special',
32
- articles: [{ id: 1, title: 'Hello!!', body: 'Hello, world!!' }, { id: 2, title: 'New Post', body: 'Body' }]
33
- }] }
34
- assert_equal expected, adapter.serializable_hash
35
- end
28
+ expected = { blogs: [{
29
+ id: 1,
30
+ special_attribute: 'Special',
31
+ articles: [{ id: 1, title: 'Hello!!', body: 'Hello, world!!' }, { id: 2, title: 'New Post', body: 'Body' }]
32
+ }] }
33
+ assert_equal expected, adapter.serializable_hash
34
+ end
36
35
 
37
- def test_include_multiple_posts
38
- serializer = CollectionSerializer.new([@first_post, @second_post])
39
- adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
36
+ def test_include_multiple_posts
37
+ serializer = ActiveModel::Serializer::CollectionSerializer.new([@first_post, @second_post])
38
+ adapter = ActiveModelSerializers::Adapter::Json.new(serializer)
40
39
 
41
- expected = { posts: [{
42
- title: 'Hello!!',
43
- body: 'Hello, world!!',
40
+ expected = { posts: [{
41
+ title: 'Hello!!',
42
+ body: 'Hello, world!!',
43
+ id: 1,
44
+ comments: [],
45
+ author: {
44
46
  id: 1,
45
- comments: [],
46
- author: {
47
- id: 1,
48
- name: 'Steve K.'
49
- },
50
- blog: {
51
- id: 999,
52
- name: 'Custom blog'
53
- }
54
- }, {
55
- title: 'New Post',
56
- body: 'Body',
57
- id: 2,
58
- comments: [],
59
- author: {
60
- id: 1,
61
- name: 'Steve K.'
62
- },
63
- blog: {
64
- id: 999,
65
- name: 'Custom blog'
66
- }
67
- }] }
68
- assert_equal expected, adapter.serializable_hash
69
- end
47
+ name: 'Steve K.'
48
+ },
49
+ blog: {
50
+ id: 999,
51
+ name: 'Custom blog'
52
+ }
53
+ }, {
54
+ title: 'New Post',
55
+ body: 'Body',
56
+ id: 2,
57
+ comments: [],
58
+ author: {
59
+ id: 1,
60
+ name: 'Steve K.'
61
+ },
62
+ blog: {
63
+ id: 999,
64
+ name: 'Custom blog'
65
+ }
66
+ }] }
67
+ assert_equal expected, adapter.serializable_hash
68
+ end
70
69
 
71
- def test_root_is_underscored
72
- virtual_value = VirtualValue.new(id: 1)
73
- serializer = CollectionSerializer.new([virtual_value])
74
- adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
70
+ def test_root_is_underscored
71
+ virtual_value = VirtualValue.new(id: 1)
72
+ serializer = ActiveModel::Serializer::CollectionSerializer.new([virtual_value])
73
+ adapter = ActiveModelSerializers::Adapter::Json.new(serializer)
75
74
 
76
- assert_equal 1, adapter.serializable_hash[:virtual_values].length
77
- end
75
+ assert_equal 1, adapter.serializable_hash[:virtual_values].length
76
+ end
78
77
 
79
- def test_include_option
80
- serializer = CollectionSerializer.new([@first_post, @second_post])
81
- adapter = ActiveModel::Serializer::Adapter::Json.new(serializer, include: '')
82
- actual = adapter.serializable_hash
83
- expected = { posts: [{ id: 1, title: 'Hello!!', body: 'Hello, world!!' },
84
- { id: 2, title: 'New Post', body: 'Body' }] }
78
+ def test_include_option
79
+ serializer = ActiveModel::Serializer::CollectionSerializer.new([@first_post, @second_post])
80
+ adapter = ActiveModelSerializers::Adapter::Json.new(serializer, include: '')
81
+ actual = adapter.serializable_hash
82
+ expected = { posts: [{ id: 1, title: 'Hello!!', body: 'Hello, world!!' },
83
+ { id: 2, title: 'New Post', body: 'Body' }] }
85
84
 
86
- assert_equal(expected, actual)
87
- end
85
+ assert_equal(expected, actual)
88
86
  end
89
87
  end
90
88
  end