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
@@ -0,0 +1,41 @@
1
+ require 'test_helper'
2
+
3
+ module ActionController
4
+ module Serialization
5
+ class JsonApi
6
+ class ErrorsTest < ActionController::TestCase
7
+ def test_active_model_with_multiple_errors
8
+ get :render_resource_with_errors
9
+
10
+ expected_errors_object =
11
+ { :errors =>
12
+ [
13
+ { :source => { :pointer => '/data/attributes/name' }, :detail => 'cannot be nil' },
14
+ { :source => { :pointer => '/data/attributes/name' }, :detail => 'must be longer' },
15
+ { :source => { :pointer => '/data/attributes/id' }, :detail => 'must be a uuid' }
16
+ ]
17
+ }.to_json
18
+ assert_equal json_reponse_body.to_json, expected_errors_object
19
+ end
20
+
21
+ def json_reponse_body
22
+ JSON.load(@response.body)
23
+ end
24
+
25
+ class ErrorsTestController < ActionController::Base
26
+ def render_resource_with_errors
27
+ resource = Profile.new(name: 'Name 1',
28
+ description: 'Description 1',
29
+ comments: 'Comments 1')
30
+ resource.errors.add(:name, 'cannot be nil')
31
+ resource.errors.add(:name, 'must be longer')
32
+ resource.errors.add(:id, 'must be a uuid')
33
+ render json: resource, adapter: :json_api, serializer: ActiveModel::Serializer::ErrorSerializer
34
+ end
35
+ end
36
+
37
+ tests ErrorsTestController
38
+ end
39
+ end
40
+ end
41
+ end
@@ -5,6 +5,7 @@ module ActionController
5
5
  class JsonApi
6
6
  class LinkedTest < ActionController::TestCase
7
7
  class LinkedTestController < ActionController::Base
8
+ require 'active_model_serializers/register_jsonapi_renderer'
8
9
  def setup_post
9
10
  ActionController::Base.cache_store.clear
10
11
  @role1 = Role.new(id: 1, name: 'admin')
@@ -38,49 +39,49 @@ module ActionController
38
39
 
39
40
  def render_resource_without_include
40
41
  setup_post
41
- render json: @post, adapter: :json_api
42
+ render jsonapi: @post
42
43
  end
43
44
 
44
45
  def render_resource_with_include
45
46
  setup_post
46
- render json: @post, include: [:author], adapter: :json_api
47
+ render jsonapi: @post, include: [:author]
47
48
  end
48
49
 
49
50
  def render_resource_with_include_of_custom_key_by_original
50
51
  setup_post
51
- render json: @post, include: [:reviews], adapter: :json_api, serializer: PostWithCustomKeysSerializer
52
+ render jsonapi: @post, include: [:reviews], serializer: PostWithCustomKeysSerializer
52
53
  end
53
54
 
54
55
  def render_resource_with_nested_include
55
56
  setup_post
56
- render json: @post, include: [comments: [:author]], adapter: :json_api
57
+ render jsonapi: @post, include: [comments: [:author]]
57
58
  end
58
59
 
59
60
  def render_resource_with_nested_has_many_include_wildcard
60
61
  setup_post
61
- render json: @post, include: 'author.*', adapter: :json_api
62
+ render jsonapi: @post, include: 'author.*'
62
63
  end
63
64
 
64
65
  def render_resource_with_missing_nested_has_many_include
65
66
  setup_post
66
67
  @post.author = @author2 # author2 has no roles.
67
- render json: @post, include: [author: [:roles]], adapter: :json_api
68
+ render jsonapi: @post, include: [author: [:roles]]
68
69
  end
69
70
 
70
71
  def render_collection_with_missing_nested_has_many_include
71
72
  setup_post
72
73
  @post.author = @author2
73
- render json: [@post, @post2], include: [author: [:roles]], adapter: :json_api
74
+ render jsonapi: [@post, @post2], include: [author: [:roles]]
74
75
  end
75
76
 
76
77
  def render_collection_without_include
77
78
  setup_post
78
- render json: [@post], adapter: :json_api
79
+ render jsonapi: [@post]
79
80
  end
80
81
 
81
82
  def render_collection_with_include
82
83
  setup_post
83
- render json: [@post], include: 'author, comments', adapter: :json_api
84
+ render jsonapi: [@post], include: 'author, comments'
84
85
  end
85
86
  end
86
87
 
@@ -8,8 +8,8 @@ module ActionController
8
8
  module Serialization
9
9
  class JsonApi
10
10
  class PaginationTest < ActionController::TestCase
11
- KAMINARI_URI = 'http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_kaminari'
12
- WILL_PAGINATE_URI = 'http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_will_paginate'
11
+ KAMINARI_URI = 'http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_kaminari'.freeze
12
+ WILL_PAGINATE_URI = 'http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_will_paginate'.freeze
13
13
 
14
14
  class PaginationTestController < ActionController::Base
15
15
  def setup
@@ -0,0 +1,180 @@
1
+ require 'test_helper'
2
+
3
+ module ActionController
4
+ module Serialization
5
+ class JsonApi
6
+ class KeyTransformTest < ActionController::TestCase
7
+ class KeyTransformTestController < ActionController::Base
8
+ Post = Class.new(::Model)
9
+ class PostSerializer < ActiveModel::Serializer
10
+ type 'posts'
11
+ attributes :title, :body, :publish_at
12
+ belongs_to :author
13
+ has_many :top_comments
14
+
15
+ link(:post_authors) { 'https://example.com/post_authors' }
16
+
17
+ meta do
18
+ {
19
+ rating: 5,
20
+ favorite_count: 10
21
+ }
22
+ end
23
+ end
24
+
25
+ Author = Class.new(::Model)
26
+ class AuthorSerializer < ActiveModel::Serializer
27
+ type 'authors'
28
+ attributes :first_name, :last_name
29
+ end
30
+
31
+ TopComment = Class.new(::Model)
32
+ class TopCommentSerializer < ActiveModel::Serializer
33
+ type 'top_comments'
34
+ attributes :body
35
+ belongs_to :author
36
+ end
37
+
38
+ def setup_post
39
+ ActionController::Base.cache_store.clear
40
+ @author = Author.new(id: 1, first_name: 'Bob', last_name: 'Jones')
41
+ @comment1 = TopComment.new(id: 7, body: 'cool', author: @author)
42
+ @comment2 = TopComment.new(id: 12, body: 'awesome', author: @author)
43
+ @post = Post.new(id: 1337, title: 'Title 1', body: 'Body 1',
44
+ author: @author, top_comments: [@comment1, @comment2],
45
+ publish_at: '2020-03-16T03:55:25.291Z')
46
+ @comment1.post = @post
47
+ @comment2.post = @post
48
+ end
49
+
50
+ def render_resource_with_transform
51
+ setup_post
52
+ render json: @post, serializer: PostSerializer, adapter: :json_api,
53
+ key_transform: :camel
54
+ end
55
+
56
+ def render_resource_with_transform_nil
57
+ setup_post
58
+ render json: @post, serializer: PostSerializer, adapter: :json_api,
59
+ key_transform: nil
60
+ end
61
+
62
+ def render_resource_with_transform_with_global_config
63
+ setup_post
64
+ old_transform = ActiveModelSerializers.config.key_transform
65
+ ActiveModelSerializers.config.key_transform = :camel_lower
66
+ render json: @post, serializer: PostSerializer, adapter: :json_api
67
+ ActiveModelSerializers.config.key_transform = old_transform
68
+ end
69
+ end
70
+
71
+ tests KeyTransformTestController
72
+
73
+ def test_render_resource_with_transform
74
+ get :render_resource_with_transform
75
+ response = JSON.parse(@response.body)
76
+ expected = {
77
+ 'Data' => {
78
+ 'Id' => '1337',
79
+ 'Type' => 'Posts',
80
+ 'Attributes' => {
81
+ 'Title' => 'Title 1',
82
+ 'Body' => 'Body 1',
83
+ 'PublishAt' => '2020-03-16T03:55:25.291Z'
84
+ },
85
+ 'Relationships' => {
86
+ 'Author' => {
87
+ 'Data' => {
88
+ 'Id' => '1',
89
+ 'Type' => 'Authors'
90
+ }
91
+ },
92
+ 'TopComments' => {
93
+ 'Data' => [
94
+ { 'Id' => '7', 'Type' => 'TopComments' },
95
+ { 'Id' => '12', 'Type' => 'TopComments' }
96
+ ]
97
+ }
98
+ },
99
+ 'Links' => {
100
+ 'PostAuthors' => 'https://example.com/post_authors'
101
+ },
102
+ 'Meta' => { 'Rating' => 5, 'FavoriteCount' => 10 }
103
+ }
104
+ }
105
+ assert_equal expected, response
106
+ end
107
+
108
+ def test_render_resource_with_transform_nil
109
+ get :render_resource_with_transform_nil
110
+ response = JSON.parse(@response.body)
111
+ expected = {
112
+ 'data' => {
113
+ 'id' => '1337',
114
+ 'type' => 'posts',
115
+ 'attributes' => {
116
+ 'title' => 'Title 1',
117
+ 'body' => 'Body 1',
118
+ 'publish-at' => '2020-03-16T03:55:25.291Z'
119
+ },
120
+ 'relationships' => {
121
+ 'author' => {
122
+ 'data' => {
123
+ 'id' => '1',
124
+ 'type' => 'authors'
125
+ }
126
+ },
127
+ 'top-comments' => {
128
+ 'data' => [
129
+ { 'id' => '7', 'type' => 'top-comments' },
130
+ { 'id' => '12', 'type' => 'top-comments' }
131
+ ]
132
+ }
133
+ },
134
+ 'links' => {
135
+ 'post-authors' => 'https://example.com/post_authors'
136
+ },
137
+ 'meta' => { 'rating' => 5, 'favorite-count' => 10 }
138
+ }
139
+ }
140
+ assert_equal expected, response
141
+ end
142
+
143
+ def test_render_resource_with_transform_with_global_config
144
+ get :render_resource_with_transform_with_global_config
145
+ response = JSON.parse(@response.body)
146
+ expected = {
147
+ 'data' => {
148
+ 'id' => '1337',
149
+ 'type' => 'posts',
150
+ 'attributes' => {
151
+ 'title' => 'Title 1',
152
+ 'body' => 'Body 1',
153
+ 'publishAt' => '2020-03-16T03:55:25.291Z'
154
+ },
155
+ 'relationships' => {
156
+ 'author' => {
157
+ 'data' => {
158
+ 'id' => '1',
159
+ 'type' => 'authors'
160
+ }
161
+ },
162
+ 'topComments' => {
163
+ 'data' => [
164
+ { 'id' => '7', 'type' => 'topComments' },
165
+ { 'id' => '12', 'type' => 'topComments' }
166
+ ]
167
+ }
168
+ },
169
+ 'links' => {
170
+ 'postAuthors' => 'https://example.com/post_authors'
171
+ },
172
+ 'meta' => { 'rating' => 5, 'favoriteCount' => 10 }
173
+ }
174
+ }
175
+ assert_equal expected, response
176
+ end
177
+ end
178
+ end
179
+ end
180
+ end
@@ -1,63 +1,229 @@
1
1
  require 'test_helper'
2
- require 'pathname'
3
2
 
4
- class DefaultScopeNameTest < ActionController::TestCase
5
- class UserSerializer < ActiveModel::Serializer
3
+ module SerializationScopeTesting
4
+ class User < ActiveModelSerializers::Model
5
+ attr_accessor :id, :name, :admin
6
6
  def admin?
7
- current_user.admin
7
+ admin
8
8
  end
9
- attributes :admin?
10
9
  end
10
+ class Comment < ActiveModelSerializers::Model
11
+ attr_accessor :id, :body
12
+ end
13
+ class Post < ActiveModelSerializers::Model
14
+ attr_accessor :id, :title, :body, :comments
15
+ end
16
+ class PostSerializer < ActiveModel::Serializer
17
+ attributes :id, :title, :body, :comments
11
18
 
12
- class UserTestController < ActionController::Base
13
- protect_from_forgery
14
-
15
- before_action { request.format = :json }
19
+ def body
20
+ "The 'scope' is the 'current_user': #{scope == current_user}"
21
+ end
16
22
 
17
- def current_user
18
- User.new(id: 1, name: 'Pete', admin: false)
23
+ def comments
24
+ if current_user.admin?
25
+ [Comment.new(id: 1, body: 'Admin')]
26
+ else
27
+ [Comment.new(id: 2, body: 'Scoped')]
28
+ end
19
29
  end
20
30
 
21
- def render_new_user
22
- render json: User.new(id: 1, name: 'Pete', admin: false), serializer: UserSerializer, adapter: :json_api
31
+ def json_key
32
+ 'post'
23
33
  end
24
34
  end
35
+ class PostTestController < ActionController::Base
36
+ attr_accessor :current_user
37
+ def render_post_by_non_admin
38
+ self.current_user = User.new(id: 3, name: 'Pete', admin: false)
39
+ render json: new_post, serializer: serializer, adapter: :json
40
+ end
25
41
 
26
- tests UserTestController
42
+ def render_post_by_admin
43
+ self.current_user = User.new(id: 3, name: 'Pete', admin: true)
44
+ render json: new_post, serializer: serializer, adapter: :json
45
+ end
27
46
 
28
- def test_default_scope_name
29
- get :render_new_user
30
- assert_equal '{"data":{"id":"1","type":"users","attributes":{"admin?":false}}}', @response.body
47
+ private
48
+
49
+ def new_post
50
+ Post.new(id: 4, title: 'Title')
51
+ end
52
+
53
+ def serializer
54
+ PostSerializer
55
+ end
31
56
  end
32
- end
57
+ class PostViewContextSerializer < PostSerializer
58
+ def body
59
+ "The 'scope' is the 'view_context': #{scope == view_context}"
60
+ end
33
61
 
34
- class SerializationScopeNameTest < ActionController::TestCase
35
- class AdminUserSerializer < ActiveModel::Serializer
36
- def admin?
37
- current_admin.admin
62
+ def comments
63
+ if view_context.controller.current_user.admin?
64
+ [Comment.new(id: 1, body: 'Admin')]
65
+ else
66
+ [Comment.new(id: 2, body: 'Scoped')]
67
+ end
68
+ end
69
+ end
70
+ class DefaultScopeTest < ActionController::TestCase
71
+ tests PostTestController
72
+
73
+ def test_default_serialization_scope
74
+ assert_equal :current_user, @controller._serialization_scope
75
+ end
76
+
77
+ def test_default_serialization_scope_object
78
+ assert_equal @controller.current_user, @controller.serialization_scope
79
+ end
80
+
81
+ def test_default_scope_non_admin
82
+ get :render_post_by_non_admin
83
+ expected_json = {
84
+ post: {
85
+ id: 4,
86
+ title: 'Title',
87
+ body: "The 'scope' is the 'current_user': true",
88
+ comments: [
89
+ { id: 2, body: 'Scoped' }
90
+ ]
91
+ }
92
+ }.to_json
93
+ assert_equal expected_json, @response.body
94
+ end
95
+
96
+ def test_default_scope_admin
97
+ get :render_post_by_admin
98
+ expected_json = {
99
+ post: {
100
+ id: 4,
101
+ title: 'Title',
102
+ body: "The 'scope' is the 'current_user': true",
103
+ comments: [
104
+ { id: 1, body: 'Admin' }
105
+ ]
106
+ }
107
+ }.to_json
108
+ assert_equal expected_json, @response.body
38
109
  end
39
- attributes :admin?
40
110
  end
111
+ class SerializationScopeTest < ActionController::TestCase
112
+ class PostViewContextTestController < PostTestController
113
+ serialization_scope :view_context
114
+
115
+ private
116
+
117
+ def serializer
118
+ PostViewContextSerializer
119
+ end
120
+ end
121
+ tests PostViewContextTestController
41
122
 
42
- class AdminUserTestController < ActionController::Base
43
- protect_from_forgery
123
+ def test_defined_serialization_scope
124
+ assert_equal :view_context, @controller._serialization_scope
125
+ end
44
126
 
45
- serialization_scope :current_admin
46
- before_action { request.format = :json }
127
+ def test_defined_serialization_scope_object
128
+ assert_equal @controller.view_context.class, @controller.serialization_scope.class
129
+ end
47
130
 
48
- def current_admin
49
- User.new(id: 2, name: 'Bob', admin: true)
131
+ def test_serialization_scope_non_admin
132
+ get :render_post_by_non_admin
133
+ expected_json = {
134
+ post: {
135
+ id: 4,
136
+ title: 'Title',
137
+ body: "The 'scope' is the 'view_context': true",
138
+ comments: [
139
+ { id: 2, body: 'Scoped' }
140
+ ]
141
+ }
142
+ }.to_json
143
+ assert_equal expected_json, @response.body
50
144
  end
51
145
 
52
- def render_new_user
53
- render json: User.new(id: 1, name: 'Pete', admin: false), serializer: AdminUserSerializer, adapter: :json_api
146
+ def test_serialization_scope_admin
147
+ get :render_post_by_admin
148
+ expected_json = {
149
+ post: {
150
+ id: 4,
151
+ title: 'Title',
152
+ body: "The 'scope' is the 'view_context': true",
153
+ comments: [
154
+ { id: 1, body: 'Admin' }
155
+ ]
156
+ }
157
+ }.to_json
158
+ assert_equal expected_json, @response.body
54
159
  end
55
160
  end
161
+ class NilSerializationScopeTest < ActionController::TestCase
162
+ class PostViewContextTestController < ActionController::Base
163
+ serialization_scope nil
56
164
 
57
- tests AdminUserTestController
165
+ attr_accessor :current_user
58
166
 
59
- def test_override_scope_name_with_controller
60
- get :render_new_user
61
- assert_equal '{"data":{"id":"1","type":"users","attributes":{"admin?":true}}}', @response.body
167
+ def render_post_with_no_scope
168
+ self.current_user = User.new(id: 3, name: 'Pete', admin: false)
169
+ render json: new_post, serializer: PostSerializer, adapter: :json
170
+ end
171
+
172
+ def render_post_with_passed_in_scope
173
+ self.current_user = User.new(id: 3, name: 'Pete', admin: false)
174
+ render json: new_post, serializer: PostSerializer, adapter: :json, scope: current_user, scope_name: :current_user
175
+ end
176
+
177
+ def render_post_with_passed_in_scope_without_scope_name
178
+ self.current_user = User.new(id: 3, name: 'Pete', admin: false)
179
+ render json: new_post, serializer: PostSerializer, adapter: :json, scope: current_user
180
+ end
181
+
182
+ private
183
+
184
+ def new_post
185
+ Post.new(id: 4, title: 'Title')
186
+ end
187
+ end
188
+ tests PostViewContextTestController
189
+
190
+ def test_nil_serialization_scope
191
+ assert_nil @controller._serialization_scope
192
+ end
193
+
194
+ def test_nil_serialization_scope_object
195
+ assert_nil @controller.serialization_scope
196
+ end
197
+
198
+ def test_nil_scope
199
+ exception_matcher = /current_user/
200
+ exception = assert_raises(NameError) do
201
+ get :render_post_with_no_scope
202
+ end
203
+ assert_match exception_matcher, exception.message
204
+ end
205
+
206
+ def test_serialization_scope_is_and_nil_scope_passed_in_current_user
207
+ get :render_post_with_passed_in_scope
208
+ expected_json = {
209
+ post: {
210
+ id: 4,
211
+ title: 'Title',
212
+ body: "The 'scope' is the 'current_user': true",
213
+ comments: [
214
+ { id: 2, body: 'Scoped' }
215
+ ]
216
+ }
217
+ }.to_json
218
+ assert_equal expected_json, @response.body
219
+ end
220
+
221
+ def test_serialization_scope_is_nil_and_scope_passed_in_current_user_without_scope_name
222
+ exception_matcher = /current_user/
223
+ exception = assert_raises(NameError) do
224
+ get :render_post_with_passed_in_scope_without_scope_name
225
+ end
226
+ assert_match exception_matcher, exception.message
227
+ end
62
228
  end
63
229
  end