active_model_serializers 0.10.0 → 0.10.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (206) hide show
  1. checksums.yaml +5 -5
  2. data/.rubocop.yml +10 -5
  3. data/.travis.yml +41 -21
  4. data/CHANGELOG.md +200 -2
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +25 -4
  7. data/README.md +166 -28
  8. data/Rakefile +5 -32
  9. data/active_model_serializers.gemspec +23 -25
  10. data/appveyor.yml +10 -6
  11. data/bin/rubocop +38 -0
  12. data/docs/README.md +2 -1
  13. data/docs/general/adapters.md +35 -11
  14. data/docs/general/caching.md +7 -1
  15. data/docs/general/configuration_options.md +86 -1
  16. data/docs/general/deserialization.md +1 -1
  17. data/docs/general/fields.md +31 -0
  18. data/docs/general/getting_started.md +1 -1
  19. data/docs/general/logging.md +7 -0
  20. data/docs/general/rendering.md +63 -25
  21. data/docs/general/serializers.md +137 -14
  22. data/docs/howto/add_pagination_links.md +16 -17
  23. data/docs/howto/add_relationship_links.md +140 -0
  24. data/docs/howto/add_root_key.md +11 -0
  25. data/docs/howto/grape_integration.md +42 -0
  26. data/docs/howto/outside_controller_use.md +12 -4
  27. data/docs/howto/passing_arbitrary_options.md +2 -2
  28. data/docs/howto/serialize_poro.md +46 -5
  29. data/docs/howto/test.md +2 -0
  30. data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
  31. data/docs/integrations/ember-and-json-api.md +67 -32
  32. data/docs/jsonapi/schema.md +1 -1
  33. data/lib/action_controller/serialization.rb +15 -3
  34. data/lib/active_model/serializable_resource.rb +2 -0
  35. data/lib/active_model/serializer/adapter/attributes.rb +2 -0
  36. data/lib/active_model/serializer/adapter/base.rb +4 -0
  37. data/lib/active_model/serializer/adapter/json.rb +2 -0
  38. data/lib/active_model/serializer/adapter/json_api.rb +2 -0
  39. data/lib/active_model/serializer/adapter/null.rb +2 -0
  40. data/lib/active_model/serializer/adapter.rb +2 -0
  41. data/lib/active_model/serializer/array_serializer.rb +10 -5
  42. data/lib/active_model/serializer/association.rb +64 -10
  43. data/lib/active_model/serializer/attribute.rb +2 -0
  44. data/lib/active_model/serializer/belongs_to_reflection.rb +6 -3
  45. data/lib/active_model/serializer/collection_serializer.rb +39 -13
  46. data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +87 -116
  47. data/lib/active_model/serializer/error_serializer.rb +13 -7
  48. data/lib/active_model/serializer/errors_serializer.rb +27 -20
  49. data/lib/active_model/serializer/field.rb +2 -0
  50. data/lib/active_model/serializer/fieldset.rb +2 -0
  51. data/lib/active_model/serializer/has_many_reflection.rb +5 -3
  52. data/lib/active_model/serializer/has_one_reflection.rb +3 -4
  53. data/lib/active_model/serializer/lazy_association.rb +99 -0
  54. data/lib/active_model/serializer/link.rb +23 -0
  55. data/lib/active_model/serializer/lint.rb +136 -130
  56. data/lib/active_model/serializer/null.rb +2 -0
  57. data/lib/active_model/serializer/reflection.rb +132 -67
  58. data/lib/active_model/serializer/version.rb +3 -1
  59. data/lib/active_model/serializer.rb +308 -82
  60. data/lib/active_model_serializers/adapter/attributes.rb +5 -66
  61. data/lib/active_model_serializers/adapter/base.rb +41 -39
  62. data/lib/active_model_serializers/adapter/json.rb +2 -0
  63. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +4 -2
  64. data/lib/active_model_serializers/adapter/json_api/error.rb +2 -0
  65. data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +2 -0
  66. data/lib/active_model_serializers/adapter/json_api/link.rb +3 -1
  67. data/lib/active_model_serializers/adapter/json_api/meta.rb +2 -0
  68. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +49 -21
  69. data/lib/active_model_serializers/adapter/json_api/relationship.rb +77 -23
  70. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +41 -10
  71. data/lib/active_model_serializers/adapter/json_api.rb +84 -65
  72. data/lib/active_model_serializers/adapter/null.rb +2 -0
  73. data/lib/active_model_serializers/adapter.rb +9 -1
  74. data/lib/active_model_serializers/callbacks.rb +2 -0
  75. data/lib/active_model_serializers/deprecate.rb +3 -2
  76. data/lib/active_model_serializers/deserialization.rb +4 -0
  77. data/lib/active_model_serializers/json_pointer.rb +2 -0
  78. data/lib/active_model_serializers/logging.rb +2 -0
  79. data/lib/active_model_serializers/lookup_chain.rb +82 -0
  80. data/lib/active_model_serializers/model.rb +111 -28
  81. data/lib/active_model_serializers/railtie.rb +7 -1
  82. data/lib/active_model_serializers/register_jsonapi_renderer.rb +46 -31
  83. data/lib/active_model_serializers/serializable_resource.rb +10 -7
  84. data/lib/active_model_serializers/serialization_context.rb +12 -3
  85. data/lib/active_model_serializers/test/schema.rb +4 -2
  86. data/lib/active_model_serializers/test/serializer.rb +2 -0
  87. data/lib/active_model_serializers/test.rb +2 -0
  88. data/lib/active_model_serializers.rb +35 -10
  89. data/lib/generators/rails/resource_override.rb +3 -1
  90. data/lib/generators/rails/serializer_generator.rb +6 -4
  91. data/lib/grape/active_model_serializers.rb +9 -5
  92. data/lib/grape/formatters/active_model_serializers.rb +21 -2
  93. data/lib/grape/helpers/active_model_serializers.rb +3 -0
  94. data/lib/tasks/rubocop.rake +55 -0
  95. data/test/action_controller/adapter_selector_test.rb +16 -5
  96. data/test/action_controller/explicit_serializer_test.rb +7 -4
  97. data/test/action_controller/json/include_test.rb +108 -27
  98. data/test/action_controller/json_api/deserialization_test.rb +3 -1
  99. data/test/action_controller/json_api/errors_test.rb +10 -9
  100. data/test/action_controller/json_api/fields_test.rb +68 -0
  101. data/test/action_controller/json_api/linked_test.rb +31 -24
  102. data/test/action_controller/json_api/pagination_test.rb +33 -23
  103. data/test/action_controller/json_api/transform_test.rb +13 -3
  104. data/test/action_controller/lookup_proc_test.rb +51 -0
  105. data/test/action_controller/namespace_lookup_test.rb +234 -0
  106. data/test/action_controller/serialization_scope_name_test.rb +14 -6
  107. data/test/action_controller/serialization_test.rb +23 -12
  108. data/test/active_model_serializers/adapter_for_test.rb +2 -0
  109. data/test/active_model_serializers/json_pointer_test.rb +17 -13
  110. data/test/active_model_serializers/logging_test.rb +2 -0
  111. data/test/active_model_serializers/model_test.rb +139 -4
  112. data/test/active_model_serializers/railtie_test_isolated.rb +14 -7
  113. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +163 -0
  114. data/test/active_model_serializers/serialization_context_test_isolated.rb +25 -10
  115. data/test/active_model_serializers/test/schema_test.rb +5 -2
  116. data/test/active_model_serializers/test/serializer_test.rb +2 -0
  117. data/test/active_record_test.rb +2 -0
  118. data/test/adapter/attributes_test.rb +42 -0
  119. data/test/adapter/deprecation_test.rb +2 -0
  120. data/test/adapter/json/belongs_to_test.rb +2 -0
  121. data/test/adapter/json/collection_test.rb +16 -0
  122. data/test/adapter/json/has_many_test.rb +12 -2
  123. data/test/adapter/json/transform_test.rb +17 -15
  124. data/test/adapter/json_api/belongs_to_test.rb +2 -0
  125. data/test/adapter/json_api/collection_test.rb +6 -3
  126. data/test/adapter/json_api/errors_test.rb +19 -19
  127. data/test/adapter/json_api/fields_test.rb +14 -3
  128. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +2 -0
  129. data/test/adapter/json_api/has_many_test.rb +51 -20
  130. data/test/adapter/json_api/has_one_test.rb +2 -0
  131. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +215 -0
  132. data/test/adapter/json_api/json_api_test.rb +7 -7
  133. data/test/adapter/json_api/linked_test.rb +35 -12
  134. data/test/adapter/json_api/links_test.rb +22 -3
  135. data/test/adapter/json_api/pagination_links_test.rb +55 -13
  136. data/test/adapter/json_api/parse_test.rb +3 -1
  137. data/test/adapter/json_api/relationship_test.rb +311 -73
  138. data/test/adapter/json_api/resource_meta_test.rb +5 -3
  139. data/test/adapter/json_api/toplevel_jsonapi_test.rb +2 -0
  140. data/test/adapter/json_api/transform_test.rb +265 -253
  141. data/test/adapter/json_api/type_test.rb +170 -36
  142. data/test/adapter/json_test.rb +10 -7
  143. data/test/adapter/null_test.rb +3 -2
  144. data/test/adapter/polymorphic_test.rb +54 -5
  145. data/test/adapter_test.rb +3 -1
  146. data/test/array_serializer_test.rb +2 -0
  147. data/test/benchmark/app.rb +3 -1
  148. data/test/benchmark/benchmarking_support.rb +3 -1
  149. data/test/benchmark/bm_active_record.rb +83 -0
  150. data/test/benchmark/bm_adapter.rb +40 -0
  151. data/test/benchmark/bm_caching.rb +18 -16
  152. data/test/benchmark/bm_lookup_chain.rb +85 -0
  153. data/test/benchmark/bm_transform.rb +23 -10
  154. data/test/benchmark/controllers.rb +18 -17
  155. data/test/benchmark/fixtures.rb +74 -72
  156. data/test/cache_test.rb +301 -69
  157. data/test/collection_serializer_test.rb +33 -14
  158. data/test/fixtures/active_record.rb +47 -10
  159. data/test/fixtures/poro.rb +128 -183
  160. data/test/generators/scaffold_controller_generator_test.rb +2 -0
  161. data/test/generators/serializer_generator_test.rb +25 -5
  162. data/test/grape_test.rb +172 -56
  163. data/test/lint_test.rb +3 -1
  164. data/test/logger_test.rb +15 -11
  165. data/test/poro_test.rb +2 -0
  166. data/test/serializable_resource_test.rb +20 -22
  167. data/test/serializers/association_macros_test.rb +5 -2
  168. data/test/serializers/associations_test.rb +274 -49
  169. data/test/serializers/attribute_test.rb +7 -3
  170. data/test/serializers/attributes_test.rb +3 -1
  171. data/test/serializers/caching_configuration_test_isolated.rb +8 -6
  172. data/test/serializers/configuration_test.rb +2 -0
  173. data/test/serializers/fieldset_test.rb +3 -1
  174. data/test/serializers/meta_test.rb +14 -6
  175. data/test/serializers/options_test.rb +19 -6
  176. data/test/serializers/read_attribute_for_serialization_test.rb +5 -3
  177. data/test/serializers/reflection_test.rb +481 -0
  178. data/test/serializers/root_test.rb +3 -1
  179. data/test/serializers/serialization_test.rb +4 -2
  180. data/test/serializers/serializer_for_test.rb +14 -10
  181. data/test/serializers/serializer_for_with_namespace_test.rb +90 -0
  182. data/test/support/isolated_unit.rb +11 -4
  183. data/test/support/rails5_shims.rb +10 -2
  184. data/test/support/rails_app.rb +4 -9
  185. data/test/support/serialization_testing.rb +33 -5
  186. data/test/test_helper.rb +15 -0
  187. metadata +126 -46
  188. data/.rubocop_todo.yml +0 -167
  189. data/docs/ARCHITECTURE.md +0 -126
  190. data/lib/active_model/serializer/associations.rb +0 -100
  191. data/lib/active_model/serializer/attributes.rb +0 -82
  192. data/lib/active_model/serializer/collection_reflection.rb +0 -7
  193. data/lib/active_model/serializer/configuration.rb +0 -35
  194. data/lib/active_model/serializer/include_tree.rb +0 -111
  195. data/lib/active_model/serializer/links.rb +0 -35
  196. data/lib/active_model/serializer/meta.rb +0 -29
  197. data/lib/active_model/serializer/singular_reflection.rb +0 -7
  198. data/lib/active_model/serializer/type.rb +0 -25
  199. data/lib/active_model_serializers/key_transform.rb +0 -70
  200. data/test/active_model_serializers/key_transform_test.rb +0 -263
  201. data/test/adapter/json_api/has_many_embed_ids_test.rb +0 -43
  202. data/test/adapter/json_api/relationships_test.rb +0 -199
  203. data/test/adapter/json_api/resource_identifier_test.rb +0 -85
  204. data/test/include_tree/from_include_args_test.rb +0 -26
  205. data/test/include_tree/from_string_test.rb +0 -94
  206. data/test/include_tree/include_args_to_hash_test.rb +0 -64
@@ -1,10 +1,23 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModelSerializers
4
6
  module Adapter
5
7
  class JsonApi
6
8
  class KeyCaseTest < ActiveSupport::TestCase
7
- Post = Class.new(::Model)
9
+ class Post < ::Model
10
+ attributes :title, :body, :publish_at
11
+ associations :author, :comments
12
+ end
13
+ class Author < ::Model
14
+ attributes :first_name, :last_name
15
+ end
16
+ class Comment < ::Model
17
+ attributes :body
18
+ associations :author, :post
19
+ end
20
+
8
21
  class PostSerializer < ActiveModel::Serializer
9
22
  type 'posts'
10
23
  attributes :title, :body, :publish_at
@@ -23,13 +36,11 @@ module ActiveModelSerializers
23
36
  end
24
37
  end
25
38
 
26
- Author = Class.new(::Model)
27
39
  class AuthorSerializer < ActiveModel::Serializer
28
40
  type 'authors'
29
41
  attributes :first_name, :last_name
30
42
  end
31
43
 
32
- Comment = Class.new(::Model)
33
44
  class CommentSerializer < ActiveModel::Serializer
34
45
  type 'comments'
35
46
  attributes :body
@@ -70,32 +81,33 @@ module ActiveModelSerializers
70
81
  adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
71
82
  result = adapter.serializable_hash
72
83
  assert_equal({
73
- data: {
74
- id: '1337',
75
- type: 'posts',
76
- attributes: {
77
- title: 'Title 1',
78
- body: 'Body 1',
79
- :"publish-at" => @publish_at
80
- },
81
- relationships: {
82
- author: {
83
- data: { id: '1', type: 'authors' }
84
- },
85
- comments: {
86
- data: [
87
- { id: '7', type: 'comments' },
88
- { id: '12', type: 'comments' }
89
- ] }
90
- },
91
- links: {
92
- self: 'http://example.com/posts/1337',
93
- :"post-authors" => 'http://example.com/posts/1337/authors',
94
- :"subscriber-comments" => 'http://example.com/posts/1337/comments'
95
- },
96
- meta: { rating: 5, :"favorite-count" => 10 }
97
- }
98
- }, result)
84
+ data: {
85
+ id: '1337',
86
+ type: 'posts',
87
+ attributes: {
88
+ title: 'Title 1',
89
+ body: 'Body 1',
90
+ :"publish-at" => @publish_at
91
+ },
92
+ relationships: {
93
+ author: {
94
+ data: { id: '1', type: 'authors' }
95
+ },
96
+ comments: {
97
+ data: [
98
+ { id: '7', type: 'comments' },
99
+ { id: '12', type: 'comments' }
100
+ ]
101
+ }
102
+ },
103
+ links: {
104
+ self: 'http://example.com/posts/1337',
105
+ :"post-authors" => 'http://example.com/posts/1337/authors',
106
+ :"subscriber-comments" => 'http://example.com/posts/1337/comments'
107
+ },
108
+ meta: { rating: 5, :"favorite-count" => 10 }
109
+ }
110
+ }, result)
99
111
  end
100
112
 
101
113
  def test_success_document_transform_global_config
@@ -106,32 +118,33 @@ module ActiveModelSerializers
106
118
  adapter.serializable_hash
107
119
  end
108
120
  assert_equal({
109
- data: {
110
- id: '1337',
111
- type: 'posts',
112
- attributes: {
113
- title: 'Title 1',
114
- body: 'Body 1',
115
- publishAt: @publish_at
116
- },
117
- relationships: {
118
- author: {
119
- data: { id: '1', type: 'authors' }
120
- },
121
- comments: {
122
- data: [
123
- { id: '7', type: 'comments' },
124
- { id: '12', type: 'comments' }
125
- ] }
126
- },
127
- links: {
128
- self: 'http://example.com/posts/1337',
129
- postAuthors: 'http://example.com/posts/1337/authors',
130
- subscriberComments: 'http://example.com/posts/1337/comments'
131
- },
132
- meta: { rating: 5, favoriteCount: 10 }
133
- }
134
- }, result)
121
+ data: {
122
+ id: '1337',
123
+ type: 'posts',
124
+ attributes: {
125
+ title: 'Title 1',
126
+ body: 'Body 1',
127
+ publishAt: @publish_at
128
+ },
129
+ relationships: {
130
+ author: {
131
+ data: { id: '1', type: 'authors' }
132
+ },
133
+ comments: {
134
+ data: [
135
+ { id: '7', type: 'comments' },
136
+ { id: '12', type: 'comments' }
137
+ ]
138
+ }
139
+ },
140
+ links: {
141
+ self: 'http://example.com/posts/1337',
142
+ postAuthors: 'http://example.com/posts/1337/authors',
143
+ subscriberComments: 'http://example.com/posts/1337/comments'
144
+ },
145
+ meta: { rating: 5, favoriteCount: 10 }
146
+ }
147
+ }, result)
135
148
  end
136
149
 
137
150
  def test_success_doc_transform_serialization_ctx_overrides_global
@@ -142,32 +155,33 @@ module ActiveModelSerializers
142
155
  adapter.serializable_hash
143
156
  end
144
157
  assert_equal({
145
- Data: {
146
- Id: '1337',
147
- Type: 'Posts',
148
- Attributes: {
149
- Title: 'Title 1',
150
- Body: 'Body 1',
151
- PublishAt: @publish_at
152
- },
153
- Relationships: {
154
- Author: {
155
- Data: { Id: '1', Type: 'Authors' }
156
- },
157
- Comments: {
158
- Data: [
159
- { Id: '7', Type: 'Comments' },
160
- { Id: '12', Type: 'Comments' }
161
- ] }
162
- },
163
- Links: {
164
- Self: 'http://example.com/posts/1337',
165
- PostAuthors: 'http://example.com/posts/1337/authors',
166
- SubscriberComments: 'http://example.com/posts/1337/comments'
167
- },
168
- Meta: { Rating: 5, FavoriteCount: 10 }
169
- }
170
- }, result)
158
+ Data: {
159
+ Id: '1337',
160
+ Type: 'Posts',
161
+ Attributes: {
162
+ Title: 'Title 1',
163
+ Body: 'Body 1',
164
+ PublishAt: @publish_at
165
+ },
166
+ Relationships: {
167
+ Author: {
168
+ Data: { Id: '1', Type: 'Authors' }
169
+ },
170
+ Comments: {
171
+ Data: [
172
+ { Id: '7', Type: 'Comments' },
173
+ { Id: '12', Type: 'Comments' }
174
+ ]
175
+ }
176
+ },
177
+ Links: {
178
+ Self: 'http://example.com/posts/1337',
179
+ PostAuthors: 'http://example.com/posts/1337/authors',
180
+ SubscriberComments: 'http://example.com/posts/1337/comments'
181
+ },
182
+ Meta: { Rating: 5, FavoriteCount: 10 }
183
+ }
184
+ }, result)
171
185
  end
172
186
 
173
187
  def test_success_document_transform_dash
@@ -176,32 +190,33 @@ module ActiveModelSerializers
176
190
  adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
177
191
  result = adapter.serializable_hash
178
192
  assert_equal({
179
- data: {
180
- id: '1337',
181
- type: 'posts',
182
- attributes: {
183
- title: 'Title 1',
184
- body: 'Body 1',
185
- :"publish-at" => @publish_at
186
- },
187
- relationships: {
188
- author: {
189
- data: { id: '1', type: 'authors' }
190
- },
191
- comments: {
192
- data: [
193
- { id: '7', type: 'comments' },
194
- { id: '12', type: 'comments' }
195
- ] }
196
- },
197
- links: {
198
- self: 'http://example.com/posts/1337',
199
- :"post-authors" => 'http://example.com/posts/1337/authors',
200
- :"subscriber-comments" => 'http://example.com/posts/1337/comments'
201
- },
202
- meta: { rating: 5, :"favorite-count" => 10 }
203
- }
204
- }, result)
193
+ data: {
194
+ id: '1337',
195
+ type: 'posts',
196
+ attributes: {
197
+ title: 'Title 1',
198
+ body: 'Body 1',
199
+ :"publish-at" => @publish_at
200
+ },
201
+ relationships: {
202
+ author: {
203
+ data: { id: '1', type: 'authors' }
204
+ },
205
+ comments: {
206
+ data: [
207
+ { id: '7', type: 'comments' },
208
+ { id: '12', type: 'comments' }
209
+ ]
210
+ }
211
+ },
212
+ links: {
213
+ self: 'http://example.com/posts/1337',
214
+ :"post-authors" => 'http://example.com/posts/1337/authors',
215
+ :"subscriber-comments" => 'http://example.com/posts/1337/comments'
216
+ },
217
+ meta: { rating: 5, :"favorite-count" => 10 }
218
+ }
219
+ }, result)
205
220
  end
206
221
 
207
222
  def test_success_document_transform_unaltered
@@ -210,32 +225,33 @@ module ActiveModelSerializers
210
225
  adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
211
226
  result = adapter.serializable_hash
212
227
  assert_equal({
213
- data: {
214
- id: '1337',
215
- type: 'posts',
216
- attributes: {
217
- title: 'Title 1',
218
- body: 'Body 1',
219
- publish_at: @publish_at
220
- },
221
- relationships: {
222
- author: {
223
- data: { id: '1', type: 'authors' }
224
- },
225
- comments: {
226
- data: [
227
- { id: '7', type: 'comments' },
228
- { id: '12', type: 'comments' }
229
- ] }
230
- },
231
- links: {
232
- self: 'http://example.com/posts/1337',
233
- post_authors: 'http://example.com/posts/1337/authors',
234
- subscriber_comments: 'http://example.com/posts/1337/comments'
235
- },
236
- meta: { rating: 5, favorite_count: 10 }
237
- }
238
- }, result)
228
+ data: {
229
+ id: '1337',
230
+ type: 'posts',
231
+ attributes: {
232
+ title: 'Title 1',
233
+ body: 'Body 1',
234
+ publish_at: @publish_at
235
+ },
236
+ relationships: {
237
+ author: {
238
+ data: { id: '1', type: 'authors' }
239
+ },
240
+ comments: {
241
+ data: [
242
+ { id: '7', type: 'comments' },
243
+ { id: '12', type: 'comments' }
244
+ ]
245
+ }
246
+ },
247
+ links: {
248
+ self: 'http://example.com/posts/1337',
249
+ post_authors: 'http://example.com/posts/1337/authors',
250
+ subscriber_comments: 'http://example.com/posts/1337/comments'
251
+ },
252
+ meta: { rating: 5, favorite_count: 10 }
253
+ }
254
+ }, result)
239
255
  end
240
256
 
241
257
  def test_success_document_transform_undefined
@@ -254,32 +270,33 @@ module ActiveModelSerializers
254
270
  adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
255
271
  result = adapter.serializable_hash
256
272
  assert_equal({
257
- Data: {
258
- Id: '1337',
259
- Type: 'Posts',
260
- Attributes: {
261
- Title: 'Title 1',
262
- Body: 'Body 1',
263
- PublishAt: @publish_at
264
- },
265
- Relationships: {
266
- Author: {
267
- Data: { Id: '1', Type: 'Authors' }
268
- },
269
- Comments: {
270
- Data: [
271
- { Id: '7', Type: 'Comments' },
272
- { Id: '12', Type: 'Comments' }
273
- ] }
274
- },
275
- Links: {
276
- Self: 'http://example.com/posts/1337',
277
- PostAuthors: 'http://example.com/posts/1337/authors',
278
- SubscriberComments: 'http://example.com/posts/1337/comments'
279
- },
280
- Meta: { Rating: 5, FavoriteCount: 10 }
281
- }
282
- }, result)
273
+ Data: {
274
+ Id: '1337',
275
+ Type: 'Posts',
276
+ Attributes: {
277
+ Title: 'Title 1',
278
+ Body: 'Body 1',
279
+ PublishAt: @publish_at
280
+ },
281
+ Relationships: {
282
+ Author: {
283
+ Data: { Id: '1', Type: 'Authors' }
284
+ },
285
+ Comments: {
286
+ Data: [
287
+ { Id: '7', Type: 'Comments' },
288
+ { Id: '12', Type: 'Comments' }
289
+ ]
290
+ }
291
+ },
292
+ Links: {
293
+ Self: 'http://example.com/posts/1337',
294
+ PostAuthors: 'http://example.com/posts/1337/authors',
295
+ SubscriberComments: 'http://example.com/posts/1337/comments'
296
+ },
297
+ Meta: { Rating: 5, FavoriteCount: 10 }
298
+ }
299
+ }, result)
283
300
  end
284
301
 
285
302
  def test_success_document_transform_camel_lower
@@ -288,32 +305,33 @@ module ActiveModelSerializers
288
305
  adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
289
306
  result = adapter.serializable_hash
290
307
  assert_equal({
291
- data: {
292
- id: '1337',
293
- type: 'posts',
294
- attributes: {
295
- title: 'Title 1',
296
- body: 'Body 1',
297
- publishAt: @publish_at
298
- },
299
- relationships: {
300
- author: {
301
- data: { id: '1', type: 'authors' }
302
- },
303
- comments: {
304
- data: [
305
- { id: '7', type: 'comments' },
306
- { id: '12', type: 'comments' }
307
- ] }
308
- },
309
- links: {
310
- self: 'http://example.com/posts/1337',
311
- postAuthors: 'http://example.com/posts/1337/authors',
312
- subscriberComments: 'http://example.com/posts/1337/comments'
313
- },
314
- meta: { rating: 5, favoriteCount: 10 }
315
- }
316
- }, result)
308
+ data: {
309
+ id: '1337',
310
+ type: 'posts',
311
+ attributes: {
312
+ title: 'Title 1',
313
+ body: 'Body 1',
314
+ publishAt: @publish_at
315
+ },
316
+ relationships: {
317
+ author: {
318
+ data: { id: '1', type: 'authors' }
319
+ },
320
+ comments: {
321
+ data: [
322
+ { id: '7', type: 'comments' },
323
+ { id: '12', type: 'comments' }
324
+ ]
325
+ }
326
+ },
327
+ links: {
328
+ self: 'http://example.com/posts/1337',
329
+ postAuthors: 'http://example.com/posts/1337/authors',
330
+ subscriberComments: 'http://example.com/posts/1337/comments'
331
+ },
332
+ meta: { rating: 5, favoriteCount: 10 }
333
+ }
334
+ }, result)
317
335
  end
318
336
 
319
337
  def test_error_document_transform_default
@@ -324,18 +342,18 @@ module ActiveModelSerializers
324
342
  serializer = ActiveModel::Serializer::ErrorSerializer.new(resource)
325
343
  adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
326
344
  result = adapter.serializable_hash
327
- expected_errors_object =
328
- { :errors =>
329
- [
330
- {
331
- :source => { :pointer => '/data/attributes/published-at' },
332
- :detail => 'must be in the future' },
333
- {
334
- :source => { :pointer => '/data/attributes/title' },
335
- :detail => 'must be longer'
336
- }
337
- ]
338
- }
345
+ expected_errors_object = {
346
+ errors: [
347
+ {
348
+ source: { pointer: '/data/attributes/published-at' },
349
+ detail: 'must be in the future'
350
+ },
351
+ {
352
+ source: { pointer: '/data/attributes/title' },
353
+ detail: 'must be longer'
354
+ }
355
+ ]
356
+ }
339
357
  assert_equal expected_errors_object, result
340
358
  end
341
359
 
@@ -349,19 +367,18 @@ module ActiveModelSerializers
349
367
  adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
350
368
  adapter.serializable_hash
351
369
  end
352
- expected_errors_object =
353
- { :Errors =>
354
- [
355
- {
356
- :Source => { :Pointer => '/data/attributes/PublishedAt' },
357
- :Detail => 'must be in the future'
358
- },
359
- {
360
- :Source => { :Pointer => '/data/attributes/Title' },
361
- :Detail => 'must be longer'
362
- }
363
- ]
364
- }
370
+ expected_errors_object = {
371
+ Errors: [
372
+ {
373
+ Source: { Pointer: '/data/attributes/PublishedAt' },
374
+ Detail: 'must be in the future'
375
+ },
376
+ {
377
+ Source: { Pointer: '/data/attributes/Title' },
378
+ Detail: 'must be longer'
379
+ }
380
+ ]
381
+ }
365
382
  assert_equal expected_errors_object, result
366
383
  end
367
384
 
@@ -375,19 +392,18 @@ module ActiveModelSerializers
375
392
  adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
376
393
  adapter.serializable_hash
377
394
  end
378
- expected_errors_object =
379
- { :Errors =>
380
- [
381
- {
382
- :Source => { :Pointer => '/data/attributes/PublishedAt' },
383
- :Detail => 'must be in the future'
384
- },
385
- {
386
- :Source => { :Pointer => '/data/attributes/Title' },
387
- :Detail => 'must be longer'
388
- }
389
- ]
390
- }
395
+ expected_errors_object = {
396
+ Errors: [
397
+ {
398
+ Source: { Pointer: '/data/attributes/PublishedAt' },
399
+ Detail: 'must be in the future'
400
+ },
401
+ {
402
+ Source: { Pointer: '/data/attributes/Title' },
403
+ Detail: 'must be longer'
404
+ }
405
+ ]
406
+ }
391
407
  assert_equal expected_errors_object, result
392
408
  end
393
409
 
@@ -402,18 +418,17 @@ module ActiveModelSerializers
402
418
  adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
403
419
  result = adapter.serializable_hash
404
420
 
405
- expected_errors_object =
406
- { :errors =>
407
- [
408
- {
409
- :source => { :pointer => '/data/attributes/published-at' },
410
- :detail => 'must be in the future'
411
- },
412
- {
413
- :source => { :pointer => '/data/attributes/title' },
414
- :detail => 'must be longer'
415
- }
416
- ]
421
+ expected_errors_object = {
422
+ errors: [
423
+ {
424
+ source: { pointer: '/data/attributes/published-at' },
425
+ detail: 'must be in the future'
426
+ },
427
+ {
428
+ source: { pointer: '/data/attributes/title' },
429
+ detail: 'must be longer'
430
+ }
431
+ ]
417
432
  }
418
433
  assert_equal expected_errors_object, result
419
434
  end
@@ -429,12 +444,11 @@ module ActiveModelSerializers
429
444
  adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
430
445
  result = adapter.serializable_hash
431
446
 
432
- expected_errors_object =
433
- { :errors =>
434
- [
435
- { :source => { :pointer => '/data/attributes/published_at' }, :detail => 'must be in the future' },
436
- { :source => { :pointer => '/data/attributes/title' }, :detail => 'must be longer' }
437
- ]
447
+ expected_errors_object = {
448
+ errors: [
449
+ { source: { pointer: '/data/attributes/published_at' }, detail: 'must be in the future' },
450
+ { source: { pointer: '/data/attributes/title' }, detail: 'must be longer' }
451
+ ]
438
452
  }
439
453
  assert_equal expected_errors_object, result
440
454
  end
@@ -466,12 +480,11 @@ module ActiveModelSerializers
466
480
  adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
467
481
  result = adapter.serializable_hash
468
482
 
469
- expected_errors_object =
470
- { :Errors =>
471
- [
472
- { :Source => { :Pointer => '/data/attributes/PublishedAt' }, :Detail => 'must be in the future' },
473
- { :Source => { :Pointer => '/data/attributes/Title' }, :Detail => 'must be longer' }
474
- ]
483
+ expected_errors_object = {
484
+ Errors: [
485
+ { Source: { Pointer: '/data/attributes/PublishedAt' }, Detail: 'must be in the future' },
486
+ { Source: { Pointer: '/data/attributes/Title' }, Detail: 'must be longer' }
487
+ ]
475
488
  }
476
489
  assert_equal expected_errors_object, result
477
490
  end
@@ -487,12 +500,11 @@ module ActiveModelSerializers
487
500
  adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
488
501
  result = adapter.serializable_hash
489
502
 
490
- expected_errors_object =
491
- { :errors =>
492
- [
493
- { :source => { :pointer => '/data/attributes/publishedAt' }, :detail => 'must be in the future' },
494
- { :source => { :pointer => '/data/attributes/title' }, :detail => 'must be longer' }
495
- ]
503
+ expected_errors_object = {
504
+ errors: [
505
+ { source: { pointer: '/data/attributes/publishedAt' }, detail: 'must be in the future' },
506
+ { source: { pointer: '/data/attributes/title' }, detail: 'must be longer' }
507
+ ]
496
508
  }
497
509
  assert_equal expected_errors_object, result
498
510
  end