active_model_serializers 0.8.3 → 0.10.0.rc4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (184) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +6 -0
  3. data/.rubocop.yml +86 -0
  4. data/.rubocop_todo.yml +240 -0
  5. data/.simplecov +111 -0
  6. data/.travis.yml +33 -22
  7. data/CHANGELOG.md +358 -6
  8. data/CONTRIBUTING.md +220 -0
  9. data/Gemfile +46 -1
  10. data/{MIT-LICENSE.txt → LICENSE.txt} +3 -2
  11. data/README.md +81 -591
  12. data/Rakefile +68 -11
  13. data/active_model_serializers.gemspec +57 -23
  14. data/appveyor.yml +27 -0
  15. data/docs/ARCHITECTURE.md +120 -0
  16. data/docs/DESIGN.textile +8 -0
  17. data/docs/README.md +35 -0
  18. data/docs/general/adapters.md +162 -0
  19. data/docs/general/caching.md +52 -0
  20. data/docs/general/configuration_options.md +27 -0
  21. data/docs/general/getting_started.md +98 -0
  22. data/docs/general/instrumentation.md +40 -0
  23. data/docs/general/logging.md +14 -0
  24. data/docs/general/rendering.md +153 -0
  25. data/docs/general/serializers.md +207 -0
  26. data/docs/how-open-source-maintained.jpg +0 -0
  27. data/docs/howto/add_pagination_links.md +121 -0
  28. data/docs/howto/add_root_key.md +51 -0
  29. data/docs/howto/outside_controller_use.md +58 -0
  30. data/docs/howto/test.md +152 -0
  31. data/docs/integrations/ember-and-json-api.md +112 -0
  32. data/docs/integrations/grape.md +19 -0
  33. data/docs/jsonapi/schema/schema.json +366 -0
  34. data/docs/jsonapi/schema.md +140 -0
  35. data/lib/action_controller/serialization.rb +41 -37
  36. data/lib/active_model/serializable_resource.rb +72 -0
  37. data/lib/active_model/serializer/adapter/attributes.rb +66 -0
  38. data/lib/active_model/serializer/adapter/base.rb +58 -0
  39. data/lib/active_model/serializer/adapter/cached_serializer.rb +45 -0
  40. data/lib/active_model/serializer/adapter/fragment_cache.rb +111 -0
  41. data/lib/active_model/serializer/adapter/json/fragment_cache.rb +13 -0
  42. data/lib/active_model/serializer/adapter/json.rb +21 -0
  43. data/lib/active_model/serializer/adapter/json_api/deserialization.rb +207 -0
  44. data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +21 -0
  45. data/lib/active_model/serializer/adapter/json_api/link.rb +44 -0
  46. data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +58 -0
  47. data/lib/active_model/serializer/adapter/json_api.rb +223 -0
  48. data/lib/active_model/serializer/adapter/null.rb +11 -0
  49. data/lib/active_model/serializer/adapter.rb +91 -0
  50. data/lib/active_model/serializer/array_serializer.rb +9 -0
  51. data/lib/active_model/serializer/association.rb +20 -0
  52. data/lib/active_model/serializer/associations.rb +87 -220
  53. data/lib/active_model/serializer/attribute.rb +25 -0
  54. data/lib/active_model/serializer/attributes.rb +82 -0
  55. data/lib/active_model/serializer/belongs_to_reflection.rb +10 -0
  56. data/lib/active_model/serializer/caching.rb +100 -0
  57. data/lib/active_model/serializer/collection_reflection.rb +7 -0
  58. data/lib/active_model/serializer/collection_serializer.rb +47 -0
  59. data/lib/active_model/serializer/configuration.rb +28 -0
  60. data/lib/active_model/serializer/field.rb +56 -0
  61. data/lib/active_model/serializer/fieldset.rb +31 -0
  62. data/lib/active_model/serializer/has_many_reflection.rb +10 -0
  63. data/lib/active_model/serializer/has_one_reflection.rb +10 -0
  64. data/lib/active_model/serializer/include_tree.rb +111 -0
  65. data/lib/active_model/serializer/links.rb +33 -0
  66. data/lib/active_model/serializer/lint.rb +142 -0
  67. data/lib/active_model/serializer/reflection.rb +91 -0
  68. data/lib/active_model/serializer/singular_reflection.rb +7 -0
  69. data/lib/active_model/serializer/type.rb +25 -0
  70. data/lib/active_model/{serializers → serializer}/version.rb +1 -1
  71. data/lib/active_model/serializer.rb +99 -479
  72. data/lib/active_model_serializers/callbacks.rb +55 -0
  73. data/lib/active_model_serializers/deserialization.rb +13 -0
  74. data/lib/active_model_serializers/logging.rb +119 -0
  75. data/lib/active_model_serializers/model.rb +39 -0
  76. data/lib/active_model_serializers/railtie.rb +38 -0
  77. data/lib/active_model_serializers/serialization_context.rb +10 -0
  78. data/lib/active_model_serializers/test/schema.rb +103 -0
  79. data/lib/active_model_serializers/test/serializer.rb +125 -0
  80. data/lib/active_model_serializers/test.rb +7 -0
  81. data/lib/active_model_serializers.rb +20 -92
  82. data/lib/generators/rails/USAGE +6 -0
  83. data/lib/generators/rails/resource_override.rb +10 -0
  84. data/lib/generators/rails/serializer_generator.rb +36 -0
  85. data/lib/generators/rails/templates/serializer.rb.erb +8 -0
  86. data/lib/grape/active_model_serializers.rb +14 -0
  87. data/lib/grape/formatters/active_model_serializers.rb +15 -0
  88. data/lib/grape/helpers/active_model_serializers.rb +16 -0
  89. data/test/action_controller/adapter_selector_test.rb +53 -0
  90. data/test/action_controller/explicit_serializer_test.rb +134 -0
  91. data/test/action_controller/json/include_test.rb +167 -0
  92. data/test/action_controller/json_api/deserialization_test.rb +59 -0
  93. data/test/action_controller/json_api/linked_test.rb +196 -0
  94. data/test/action_controller/json_api/pagination_test.rb +116 -0
  95. data/test/{serialization_scope_name_test.rb → action_controller/serialization_scope_name_test.rb} +11 -15
  96. data/test/action_controller/serialization_test.rb +435 -0
  97. data/test/active_model_serializers/logging_test.rb +77 -0
  98. data/test/active_model_serializers/model_test.rb +9 -0
  99. data/test/active_model_serializers/railtie_test_isolated.rb +57 -0
  100. data/test/active_model_serializers/serialization_context_test.rb +18 -0
  101. data/test/active_model_serializers/test/schema_test.rb +128 -0
  102. data/test/active_model_serializers/test/serializer_test.rb +63 -0
  103. data/test/active_record_test.rb +9 -0
  104. data/test/adapter/fragment_cache_test.rb +38 -0
  105. data/test/adapter/json/belongs_to_test.rb +47 -0
  106. data/test/adapter/json/collection_test.rb +92 -0
  107. data/test/adapter/json/has_many_test.rb +47 -0
  108. data/test/adapter/json_api/belongs_to_test.rb +157 -0
  109. data/test/adapter/json_api/collection_test.rb +97 -0
  110. data/test/adapter/json_api/fields_test.rb +89 -0
  111. data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
  112. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
  113. data/test/adapter/json_api/has_many_test.rb +145 -0
  114. data/test/adapter/json_api/has_one_test.rb +81 -0
  115. data/test/adapter/json_api/json_api_test.rb +37 -0
  116. data/test/adapter/json_api/linked_test.rb +394 -0
  117. data/test/adapter/json_api/links_test.rb +68 -0
  118. data/test/adapter/json_api/pagination_links_test.rb +115 -0
  119. data/test/adapter/json_api/parse_test.rb +139 -0
  120. data/test/adapter/json_api/resource_type_config_test.rb +71 -0
  121. data/test/adapter/json_api/toplevel_jsonapi_test.rb +84 -0
  122. data/test/adapter/json_test.rb +47 -0
  123. data/test/adapter/null_test.rb +25 -0
  124. data/test/adapter_test.rb +42 -0
  125. data/test/array_serializer_test.rb +36 -73
  126. data/test/collection_serializer_test.rb +100 -0
  127. data/test/fixtures/active_record.rb +56 -0
  128. data/test/fixtures/poro.rb +229 -0
  129. data/test/generators/scaffold_controller_generator_test.rb +24 -0
  130. data/test/generators/serializer_generator_test.rb +57 -0
  131. data/test/grape_test.rb +82 -0
  132. data/test/include_tree/from_include_args_test.rb +26 -0
  133. data/test/include_tree/from_string_test.rb +94 -0
  134. data/test/include_tree/include_args_to_hash_test.rb +64 -0
  135. data/test/lint_test.rb +40 -0
  136. data/test/logger_test.rb +18 -0
  137. data/test/poro_test.rb +9 -0
  138. data/test/serializable_resource_test.rb +27 -0
  139. data/test/serializers/adapter_for_test.rb +166 -0
  140. data/test/serializers/association_macros_test.rb +36 -0
  141. data/test/serializers/associations_test.rb +267 -0
  142. data/test/serializers/attribute_test.rb +123 -0
  143. data/test/serializers/attributes_test.rb +52 -0
  144. data/test/serializers/cache_test.rb +209 -0
  145. data/test/serializers/configuration_test.rb +32 -0
  146. data/test/serializers/fieldset_test.rb +14 -0
  147. data/test/serializers/meta_test.rb +130 -0
  148. data/test/serializers/options_test.rb +21 -0
  149. data/test/serializers/root_test.rb +21 -0
  150. data/test/serializers/serializer_for_test.rb +134 -0
  151. data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
  152. data/test/support/isolated_unit.rb +77 -0
  153. data/test/support/rails5_shims.rb +29 -0
  154. data/test/support/rails_app.rb +25 -0
  155. data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
  156. data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +6 -0
  157. data/test/support/schemas/custom/show.json +7 -0
  158. data/test/support/schemas/hyper_schema.json +93 -0
  159. data/test/support/schemas/render_using_json_api.json +43 -0
  160. data/test/support/schemas/simple_json_pointers.json +10 -0
  161. data/test/support/serialization_testing.rb +53 -0
  162. data/test/support/simplecov.rb +6 -0
  163. data/test/support/stream_capture.rb +50 -0
  164. data/test/support/test_case.rb +19 -0
  165. data/test/test_helper.rb +55 -24
  166. metadata +358 -42
  167. data/DESIGN.textile +0 -586
  168. data/Gemfile.edge +0 -9
  169. data/bench/perf.rb +0 -43
  170. data/cruft.md +0 -19
  171. data/lib/active_model/array_serializer.rb +0 -104
  172. data/lib/active_record/serializer_override.rb +0 -16
  173. data/lib/generators/resource_override.rb +0 -13
  174. data/lib/generators/serializer/USAGE +0 -9
  175. data/lib/generators/serializer/serializer_generator.rb +0 -42
  176. data/lib/generators/serializer/templates/serializer.rb +0 -19
  177. data/test/association_test.rb +0 -592
  178. data/test/caching_test.rb +0 -96
  179. data/test/generators_test.rb +0 -85
  180. data/test/no_serialization_scope_test.rb +0 -34
  181. data/test/serialization_test.rb +0 -392
  182. data/test/serializer_support_test.rb +0 -51
  183. data/test/serializer_test.rb +0 -1465
  184. data/test/test_fakes.rb +0 -217
@@ -0,0 +1,19 @@
1
+ # Integration with Grape
2
+
3
+ [Grape](https://github.com/ruby-grape/grape) is an opinionated micro-framework for creating REST-like APIs in ruby.
4
+
5
+ ActiveModelSerializers currently supports Grape >= 0.13, < 1.0
6
+
7
+ To add [Grape](https://github.com/ruby-grape/grape) support, enable the formatter and helper functions by including `Grape::ActiveModelSerializers` in your base endpoint. For example:
8
+
9
+ ```ruby
10
+ module Example
11
+ class Dummy < Grape::API
12
+ require 'grape/active_model_serializers'
13
+ include Grape::ActiveModelSerializers
14
+ mount Example::V1::Base
15
+ end
16
+ end
17
+ ```
18
+
19
+ Aside from this, [configuration](../general/configuration_options.md) of ActiveModelSerializers is exactly the same.
@@ -0,0 +1,366 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-04/schema#",
3
+ "title": "JSON API Schema",
4
+ "description": "This is a schema for responses in the JSON API format. For more, see http://jsonapi.org",
5
+ "oneOf": [
6
+ {
7
+ "$ref": "#/definitions/success"
8
+ },
9
+ {
10
+ "$ref": "#/definitions/failure"
11
+ },
12
+ {
13
+ "$ref": "#/definitions/info"
14
+ }
15
+ ],
16
+
17
+ "definitions": {
18
+ "success": {
19
+ "type": "object",
20
+ "required": [
21
+ "data"
22
+ ],
23
+ "properties": {
24
+ "data": {
25
+ "$ref": "#/definitions/data"
26
+ },
27
+ "included": {
28
+ "description": "To reduce the number of HTTP requests, servers **MAY** allow responses that include related resources along with the requested primary resources. Such responses are called \"compound documents\".",
29
+ "type": "array",
30
+ "items": {
31
+ "$ref": "#/definitions/resource"
32
+ },
33
+ "uniqueItems": true
34
+ },
35
+ "meta": {
36
+ "$ref": "#/definitions/meta"
37
+ },
38
+ "links": {
39
+ "description": "Link members related to the primary data.",
40
+ "allOf": [
41
+ {
42
+ "$ref": "#/definitions/links"
43
+ },
44
+ {
45
+ "$ref": "#/definitions/pagination"
46
+ }
47
+ ]
48
+ },
49
+ "jsonapi": {
50
+ "$ref": "#/definitions/jsonapi"
51
+ }
52
+ },
53
+ "additionalProperties": false
54
+ },
55
+ "failure": {
56
+ "type": "object",
57
+ "required": [
58
+ "errors"
59
+ ],
60
+ "properties": {
61
+ "errors": {
62
+ "type": "array",
63
+ "items": {
64
+ "$ref": "#/definitions/error"
65
+ },
66
+ "uniqueItems": true
67
+ },
68
+ "meta": {
69
+ "$ref": "#/definitions/meta"
70
+ },
71
+ "jsonapi": {
72
+ "$ref": "#/definitions/jsonapi"
73
+ }
74
+ },
75
+ "additionalProperties": false
76
+ },
77
+ "info": {
78
+ "type": "object",
79
+ "required": [
80
+ "meta"
81
+ ],
82
+ "properties": {
83
+ "meta": {
84
+ "$ref": "#/definitions/meta"
85
+ },
86
+ "links": {
87
+ "$ref": "#/definitions/links"
88
+ },
89
+ "jsonapi": {
90
+ "$ref": "#/definitions/jsonapi"
91
+ }
92
+ },
93
+ "additionalProperties": false
94
+ },
95
+
96
+ "meta": {
97
+ "description": "Non-standard meta-information that can not be represented as an attribute or relationship.",
98
+ "type": "object",
99
+ "additionalProperties": true
100
+ },
101
+ "data": {
102
+ "description": "The document's \"primary data\" is a representation of the resource or collection of resources targeted by a request.",
103
+ "oneOf": [
104
+ {
105
+ "$ref": "#/definitions/resource"
106
+ },
107
+ {
108
+ "description": "An array of resource objects, an array of resource identifier objects, or an empty array ([]), for requests that target resource collections.",
109
+ "type": "array",
110
+ "items": {
111
+ "$ref": "#/definitions/resource"
112
+ },
113
+ "uniqueItems": true
114
+ }
115
+ ]
116
+ },
117
+ "resource": {
118
+ "description": "\"Resource objects\" appear in a JSON API document to represent resources.",
119
+ "type": "object",
120
+ "required": [
121
+ "type",
122
+ "id"
123
+ ],
124
+ "properties": {
125
+ "type": {
126
+ "type": "string"
127
+ },
128
+ "id": {
129
+ "type": "string"
130
+ },
131
+ "attributes": {
132
+ "$ref": "#/definitions/attributes"
133
+ },
134
+ "relationships": {
135
+ "$ref": "#/definitions/relationships"
136
+ },
137
+ "links": {
138
+ "$ref": "#/definitions/links"
139
+ },
140
+ "meta": {
141
+ "$ref": "#/definitions/meta"
142
+ }
143
+ },
144
+ "additionalProperties": false
145
+ },
146
+
147
+ "links": {
148
+ "description": "A resource object **MAY** contain references to other resource objects (\"relationships\"). Relationships may be to-one or to-many. Relationships can be specified by including a member in a resource's links object.",
149
+ "type": "object",
150
+ "properties": {
151
+ "self": {
152
+ "description": "A `self` member, whose value is a URL for the relationship itself (a \"relationship URL\"). This URL allows the client to directly manipulate the relationship. For example, it would allow a client to remove an `author` from an `article` without deleting the people resource itself.",
153
+ "type": "string",
154
+ "format": "uri"
155
+ },
156
+ "related": {
157
+ "$ref": "#/definitions/link"
158
+ }
159
+ },
160
+ "additionalProperties": true
161
+ },
162
+ "link": {
163
+ "description": "A link **MUST** be represented as either: a string containing the link's URL or a link object.",
164
+ "oneOf": [
165
+ {
166
+ "description": "A string containing the link's URL.",
167
+ "type": "string",
168
+ "format": "uri"
169
+ },
170
+ {
171
+ "type": "object",
172
+ "required": [
173
+ "href"
174
+ ],
175
+ "properties": {
176
+ "href": {
177
+ "description": "A string containing the link's URL.",
178
+ "type": "string",
179
+ "format": "uri"
180
+ },
181
+ "meta": {
182
+ "$ref": "#/definitions/meta"
183
+ }
184
+ }
185
+ }
186
+ ]
187
+ },
188
+
189
+ "attributes": {
190
+ "description": "Members of the attributes object (\"attributes\") represent information about the resource object in which it's defined.",
191
+ "type": "object",
192
+ "patternProperties": {
193
+ "^(?!relationships$|links$)\\w[-\\w_]*$": {
194
+ "description": "Attributes may contain any valid JSON value."
195
+ }
196
+ },
197
+ "additionalProperties": false
198
+ },
199
+
200
+ "relationships": {
201
+ "description": "Members of the relationships object (\"relationships\") represent references from the resource object in which it's defined to other resource objects.",
202
+ "type": "object",
203
+ "patternProperties": {
204
+ "^\\w[-\\w_]*$": {
205
+ "properties": {
206
+ "links": {
207
+ "$ref": "#/definitions/links"
208
+ },
209
+ "data": {
210
+ "description": "Member, whose value represents \"resource linkage\".",
211
+ "oneOf": [
212
+ {
213
+ "$ref": "#/definitions/relationshipToOne"
214
+ },
215
+ {
216
+ "$ref": "#/definitions/relationshipToMany"
217
+ }
218
+ ]
219
+ },
220
+ "meta": {
221
+ "$ref": "#/definitions/meta"
222
+ }
223
+ },
224
+ "additionalProperties": false
225
+ }
226
+ },
227
+ "additionalProperties": false
228
+ },
229
+ "relationshipToOne": {
230
+ "description": "References to other resource objects in a to-one (\"relationship\"). Relationships can be specified by including a member in a resource's links object.",
231
+ "anyOf": [
232
+ {
233
+ "$ref": "#/definitions/empty"
234
+ },
235
+ {
236
+ "$ref": "#/definitions/linkage"
237
+ }
238
+ ]
239
+ },
240
+ "relationshipToMany": {
241
+ "description": "An array of objects each containing \"type\" and \"id\" members for to-many relationships.",
242
+ "type": "array",
243
+ "items": {
244
+ "$ref": "#/definitions/linkage"
245
+ },
246
+ "uniqueItems": true
247
+ },
248
+ "empty": {
249
+ "description": "Describes an empty to-one relationship.",
250
+ "type": "null"
251
+ },
252
+ "linkage": {
253
+ "description": "The \"type\" and \"id\" to non-empty members.",
254
+ "type": "object",
255
+ "required": [
256
+ "type",
257
+ "id"
258
+ ],
259
+ "properties": {
260
+ "type": {
261
+ "type": "string"
262
+ },
263
+ "id": {
264
+ "type": "string"
265
+ },
266
+ "meta": {
267
+ "$ref": "#/definitions/meta"
268
+ }
269
+ },
270
+ "additionalProperties": false
271
+ },
272
+ "pagination": {
273
+ "type": "object",
274
+ "properties": {
275
+ "first": {
276
+ "description": "The first page of data",
277
+ "oneOf": [
278
+ { "type": "string", "format": "uri" },
279
+ { "type": "null" }
280
+ ]
281
+ },
282
+ "last": {
283
+ "description": "The last page of data",
284
+ "oneOf": [
285
+ { "type": "string", "format": "uri" },
286
+ { "type": "null" }
287
+ ]
288
+ },
289
+ "prev": {
290
+ "description": "The previous page of data",
291
+ "oneOf": [
292
+ { "type": "string", "format": "uri" },
293
+ { "type": "null" }
294
+ ]
295
+ },
296
+ "next": {
297
+ "description": "The next page of data",
298
+ "oneOf": [
299
+ { "type": "string", "format": "uri" },
300
+ { "type": "null" }
301
+ ]
302
+ }
303
+ }
304
+ },
305
+
306
+ "jsonapi": {
307
+ "description": "An object describing the server's implementation",
308
+ "type": "object",
309
+ "properties": {
310
+ "version": {
311
+ "type": "string"
312
+ },
313
+ "meta": {
314
+ "$ref": "#/definitions/meta"
315
+ }
316
+ },
317
+ "additionalProperties": false
318
+ },
319
+
320
+ "error": {
321
+ "type": "object",
322
+ "properties": {
323
+ "id": {
324
+ "description": "A unique identifier for this particular occurrence of the problem.",
325
+ "type": "string"
326
+ },
327
+ "links": {
328
+ "$ref": "#/definitions/links"
329
+ },
330
+ "status": {
331
+ "description": "The HTTP status code applicable to this problem, expressed as a string value.",
332
+ "type": "string"
333
+ },
334
+ "code": {
335
+ "description": "An application-specific error code, expressed as a string value.",
336
+ "type": "string"
337
+ },
338
+ "title": {
339
+ "description": "A short, human-readable summary of the problem. It **SHOULD NOT** change from occurrence to occurrence of the problem, except for purposes of localization.",
340
+ "type": "string"
341
+ },
342
+ "detail": {
343
+ "description": "A human-readable explanation specific to this occurrence of the problem.",
344
+ "type": "string"
345
+ },
346
+ "source": {
347
+ "type": "object",
348
+ "properties": {
349
+ "pointer": {
350
+ "description": "A JSON Pointer [RFC6901] to the associated entity in the request document [e.g. \"/data\" for a primary data object, or \"/data/attributes/title\" for a specific attribute].",
351
+ "type": "string"
352
+ },
353
+ "parameter": {
354
+ "description": "A string indicating which query parameter caused the error.",
355
+ "type": "string"
356
+ }
357
+ }
358
+ },
359
+ "meta": {
360
+ "$ref": "#/definitions/meta"
361
+ }
362
+ },
363
+ "additionalProperties": false
364
+ }
365
+ }
366
+ }
@@ -0,0 +1,140 @@
1
+ [Back to Guides](../README.md)
2
+
3
+ [![JSON API 1.0](https://img.shields.io/badge/JSON%20API-1.0-lightgrey.svg)](http://jsonapi.org/)
4
+
5
+ ## JSON API Requests
6
+
7
+ - [Query Parameters Spec](http://jsonapi.org/format/#query-parameters)
8
+
9
+ Headers:
10
+
11
+ - Request: `Accept: application/vnd.api+json`
12
+ - Response: `Content-Type: application/vnd.api+json`
13
+
14
+ ### [Fetching Data](http://jsonapi.org/format/#fetching)
15
+
16
+ A server MUST support fetching resource data for every URL provided as:
17
+
18
+ - a `self` link as part of the top-level links object
19
+ - a `self` link as part of a resource-level links object
20
+ - a `related` link as part of a relationship-level links object
21
+
22
+ Example supported requests
23
+
24
+ - Individual resource or collection
25
+ - GET /articles
26
+ - GET /articles/1
27
+ - GET /articles/1/author
28
+ - Relationships
29
+ - GET /articles/1/relationships/comments
30
+ - GET /articles/1/relationships/author
31
+ - Optional: [Inclusion of related resources](http://jsonapi.org/format/#fetching-includes) `ActiveModel::Serializer::IncludeTree`
32
+ - GET /articles/1?`include`=comments
33
+ - GET /articles/1?`include`=comments.author
34
+ - GET /articles/1?`include`=author,comments.author
35
+ - GET /articles/1/relationships/comments?`include`=comments.author
36
+ - Optional: [Sparse Fieldsets](http://jsonapi.org/format/#fetching-sparse-fieldsets) `ActiveModel::Serializer::Fieldset`
37
+ - GET /articles?`include`=author&`fields`[articles]=title,body&`fields`[people]=name
38
+ - Optional: [Sorting](http://jsonapi.org/format/#fetching-sorting)
39
+ - GET /people?`sort`=age
40
+ - GET /people?`sort`=age,author.name
41
+ - GET /articles?`sort`=-created,title
42
+ - Optional: [Pagination](http://jsonapi.org/format/#fetching-pagination)
43
+ - GET /articles?`page`[number]=3&`page`[size]=1
44
+ - Optional: [Filtering](http://jsonapi.org/format/#fetching-filtering)
45
+ - GET /comments?`filter`[post]=1
46
+ - GET /comments?`filter`[post]=1,2
47
+ - GET /comments?`filter`[post]=1,2
48
+
49
+ ### [CRUD Actions](http://jsonapi.org/format/#crud)
50
+
51
+ ### [Asynchronous Processing](http://jsonapi.org/recommendations/#asynchronous-processing)
52
+
53
+ ### [Bulk Operations Extension](http://jsonapi.org/extensions/bulk/)
54
+
55
+ ## JSON API Document Schema
56
+
57
+ | JSON API object | JSON API properties | Required | ActiveModelSerializers representation |
58
+ |-----------------------|----------------------------------------------------------------------------------------------------|----------|---------------------------------------|
59
+ | schema | oneOf (success, failure, info) | |
60
+ | success | data, included, meta, links, jsonapi | | AM::SerializableResource
61
+ | success.meta | meta | | AM::S::Adapter::Base#meta
62
+ | success.included | UniqueArray(resource) | | AM::S::Adapter::JsonApi#serializable_hash_for_collection
63
+ | success.data | data | |
64
+ | success.links | allOf (links, pagination) | | AM::S::Adapter::JsonApi#links_for
65
+ | success.jsonapi | jsonapi | |
66
+ | failure | errors, meta, jsonapi | errors |
67
+ | failure.errors | UniqueArray(error) | | #1004
68
+ | meta | Object | |
69
+ | data | oneOf (resource, UniqueArray(resource)) | | AM::S::Adapter::JsonApi#serializable_hash_for_collection,#serializable_hash_for_single_resource
70
+ | resource | String(type), String(id),<br>attributes, relationships,<br>links, meta | type, id | AM::S::Adapter::JsonApi#primary_data_for
71
+ | links | Uri(self), Link(related) | | #1028, #1246, #1282
72
+ | link | oneOf (linkString, linkObject) | |
73
+ | link.linkString | Uri | |
74
+ | link.linkObject | Uri(href), meta | href |
75
+ | attributes | patternProperties(<br>`"^(?!relationships$|links$)\\w[-\\w_]*$"`),<br>any valid JSON | | AM::Serializer#attributes, AM::S::Adapter::JsonApi#resource_object_for
76
+ | relationships | patternProperties(<br>`"^\\w[-\\w_]*$"`);<br>links, relationships.data, meta | | AM::S::Adapter::JsonApi#relationships_for
77
+ | relationships.data | oneOf (relationshipToOne, relationshipToMany) | | AM::S::Adapter::JsonApi#resource_identifier_for
78
+ | relationshipToOne | anyOf(empty, linkage) | |
79
+ | relationshipToMany | UniqueArray(linkage) | |
80
+ | empty | null | |
81
+ | linkage | String(type), String(id), meta | type, id | AM::S::Adapter::JsonApi#primary_data_for
82
+ | pagination | pageObject(first), pageObject(last),<br>pageObject(prev), pageObject(next) | | AM::S::Adapter::JsonApi::PaginationLinks#serializable_hash
83
+ | pagination.pageObject | oneOf(Uri, null) | |
84
+ | jsonapi | String(version), meta | | AM::S::Adapter::JsonApi::ApiObjects::JsonApi
85
+ | error | String(id), links, String(status),<br>String(code), String(title),<br>String(detail), error.source, meta | |
86
+ | error.source | String(pointer), String(parameter) | |
87
+ | pointer | [JSON Pointer RFC6901](https://tools.ietf.org/html/rfc6901) | |
88
+
89
+
90
+ The [http://jsonapi.org/schema](schema/schema.json) makes a nice roadmap.
91
+
92
+ ### Success Document
93
+ - [ ] success
94
+ - [ ] data: `"$ref": "#/definitions/data"`
95
+ - [ ] included: array of unique items of type `"$ref": "#/definitions/resource"`
96
+ - [ ] meta: `"$ref": "#/definitions/meta"`
97
+ - [ ] links:
98
+ - [ ] link: `"$ref": "#/definitions/links"`
99
+ - [ ] pagination: ` "$ref": "#/definitions/pagination"`
100
+ - [ ] jsonapi: ` "$ref": "#/definitions/jsonapi"`
101
+
102
+ ### Failure Document
103
+
104
+ - [ ] failure
105
+ - [ ] errors: array of unique items of type ` "$ref": "#/definitions/error"`
106
+ - [ ] meta: `"$ref": "#/definitions/meta"`
107
+ - [ ] jsonapi: `"$ref": "#/definitions/jsonapi"`
108
+
109
+ ### Info Document
110
+
111
+ - [ ] info
112
+ - [ ] meta: `"$ref": "#/definitions/meta"`
113
+ - [ ] links: `"$ref": "#/definitions/links"`
114
+ - [ ] jsonapi: ` "$ref": "#/definitions/jsonapi"`
115
+
116
+ ### Definitions
117
+
118
+ - [ ] definitions:
119
+ - [ ] meta
120
+ - [ ] data: oneOf (resource, array of unique resources)
121
+ - [ ] resource
122
+ - [ ] attributes
123
+ - [ ] relationships
124
+ - [ ] relationshipToOne
125
+ - [ ] empty
126
+ - [ ] linkage
127
+ - [ ] meta
128
+ - [ ] relationshipToMany
129
+ - [ ] linkage
130
+ - [ ] meta
131
+ - [ ] links
132
+ - [ ] meta
133
+ - [ ] links
134
+ - [ ] link
135
+ - [ ] uri
136
+ - [ ] href, meta
137
+ - [ ] pagination
138
+ - [ ] jsonapi
139
+ - [ ] meta
140
+ - [ ] error: id, links, status, code, title: detail: source [{pointer, type}, {parameter: {description, type}], meta
@@ -1,32 +1,21 @@
1
+ require 'active_support/core_ext/class/attribute'
2
+ require 'active_model_serializers/serialization_context'
3
+
1
4
  module ActionController
2
- # Action Controller Serialization
3
- #
4
- # Overrides render :json to check if the given object implements +active_model_serializer+
5
- # as a method. If so, use the returned serializer instead of calling +to_json+ on the object.
6
- #
7
- # This module also provides a serialization_scope method that allows you to configure the
8
- # +serialization_scope+ of the serializer. Most apps will likely set the +serialization_scope+
9
- # to the current user:
10
- #
11
- # class ApplicationController < ActionController::Base
12
- # serialization_scope :current_user
13
- # end
14
- #
15
- # If you need more complex scope rules, you can simply override the serialization_scope:
16
- #
17
- # class ApplicationController < ActionController::Base
18
- # private
19
- #
20
- # def serialization_scope
21
- # current_user
22
- # end
23
- # end
24
- #
25
5
  module Serialization
26
6
  extend ActiveSupport::Concern
27
7
 
28
8
  include ActionController::Renderers
29
9
 
10
+ # Deprecated
11
+ ADAPTER_OPTION_KEYS = ActiveModel::SerializableResource::ADAPTER_OPTION_KEYS
12
+
13
+ module ClassMethods
14
+ def serialization_scope(scope)
15
+ self._serialization_scope = scope
16
+ end
17
+ end
18
+
30
19
  included do
31
20
  class_attribute :_serialization_scope
32
21
  self._serialization_scope = :current_user
@@ -37,24 +26,39 @@ module ActionController
37
26
  respond_to?(_serialization_scope, true)
38
27
  end
39
28
 
40
- def default_serializer_options
41
- end
42
-
43
- [:_render_option_json, :_render_with_renderer_json].each do |renderer_method|
44
- define_method renderer_method do |resource, options|
45
- json = ActiveModel::Serializer.build_json(self, resource, options)
46
-
47
- if json
48
- super(json, options)
49
- else
50
- super(resource, options)
29
+ def get_serializer(resource, options = {})
30
+ if !use_adapter?
31
+ warn 'ActionController::Serialization#use_adapter? has been removed. '\
32
+ "Please pass 'adapter: false' or see ActiveSupport::SerializableResource.new"
33
+ options[:adapter] = false
34
+ end
35
+ serializable_resource = ActiveModel::SerializableResource.new(resource, options)
36
+ if serializable_resource.serializer?
37
+ serializable_resource.serialization_scope ||= serialization_scope
38
+ serializable_resource.serialization_scope_name = _serialization_scope
39
+ begin
40
+ # Necessary to ensure we have an adapter for the serializable resource
41
+ # after it has been figured.
42
+ # TODO: This logic should be less opaque and probably moved into the SerializableResource.
43
+ serializable_resource.tap(&:adapter)
44
+ rescue ActiveModel::Serializer::CollectionSerializer::NoSerializerError
45
+ resource
51
46
  end
47
+ else
48
+ resource
52
49
  end
53
50
  end
54
51
 
55
- module ClassMethods
56
- def serialization_scope(scope)
57
- self._serialization_scope = scope
52
+ # Deprecated
53
+ def use_adapter?
54
+ true
55
+ end
56
+
57
+ [:_render_option_json, :_render_with_renderer_json].each do |renderer_method|
58
+ define_method renderer_method do |resource, options|
59
+ options.fetch(:serialization_context) { options[:serialization_context] = ActiveModelSerializers::SerializationContext.new(request) }
60
+ serializable_resource = get_serializer(resource, options)
61
+ super(serializable_resource, options)
58
62
  end
59
63
  end
60
64
  end