active_model_serializers 0.8.3 → 0.10.0.rc5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (235) 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 +7 -0
  5. data/.rubocop.yml +104 -0
  6. data/.rubocop_todo.yml +167 -0
  7. data/.simplecov +110 -0
  8. data/.travis.yml +46 -23
  9. data/CHANGELOG.md +442 -6
  10. data/CONTRIBUTING.md +95 -0
  11. data/Gemfile +51 -1
  12. data/{MIT-LICENSE.txt → MIT-LICENSE} +3 -2
  13. data/README.md +102 -590
  14. data/Rakefile +93 -8
  15. data/active_model_serializers.gemspec +65 -23
  16. data/appveyor.yml +28 -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 +126 -0
  21. data/docs/README.md +39 -0
  22. data/docs/STYLE.md +58 -0
  23. data/docs/general/adapters.md +245 -0
  24. data/docs/general/caching.md +52 -0
  25. data/docs/general/configuration_options.md +100 -0
  26. data/docs/general/deserialization.md +100 -0
  27. data/docs/general/getting_started.md +133 -0
  28. data/docs/general/instrumentation.md +40 -0
  29. data/docs/general/key_transforms.md +40 -0
  30. data/docs/general/logging.md +14 -0
  31. data/docs/general/rendering.md +255 -0
  32. data/docs/general/serializers.md +339 -0
  33. data/docs/how-open-source-maintained.jpg +0 -0
  34. data/docs/howto/add_pagination_links.md +139 -0
  35. data/docs/howto/add_root_key.md +51 -0
  36. data/docs/howto/outside_controller_use.md +58 -0
  37. data/docs/howto/passing_arbitrary_options.md +27 -0
  38. data/docs/howto/test.md +152 -0
  39. data/docs/integrations/ember-and-json-api.md +112 -0
  40. data/docs/integrations/grape.md +19 -0
  41. data/docs/jsonapi/errors.md +56 -0
  42. data/docs/jsonapi/schema/schema.json +366 -0
  43. data/docs/jsonapi/schema.md +151 -0
  44. data/docs/rfcs/0000-namespace.md +106 -0
  45. data/docs/rfcs/template.md +15 -0
  46. data/lib/action_controller/serialization.rb +31 -36
  47. data/lib/active_model/serializable_resource.rb +11 -0
  48. data/lib/active_model/serializer/adapter/attributes.rb +15 -0
  49. data/lib/active_model/serializer/adapter/base.rb +16 -0
  50. data/lib/active_model/serializer/adapter/json.rb +15 -0
  51. data/lib/active_model/serializer/adapter/json_api.rb +15 -0
  52. data/lib/active_model/serializer/adapter/null.rb +15 -0
  53. data/lib/active_model/serializer/adapter.rb +24 -0
  54. data/lib/active_model/serializer/array_serializer.rb +9 -0
  55. data/lib/active_model/serializer/association.rb +19 -0
  56. data/lib/active_model/serializer/associations.rb +87 -220
  57. data/lib/active_model/serializer/attribute.rb +25 -0
  58. data/lib/active_model/serializer/attributes.rb +82 -0
  59. data/lib/active_model/serializer/belongs_to_reflection.rb +10 -0
  60. data/lib/active_model/serializer/caching.rb +151 -0
  61. data/lib/active_model/serializer/collection_reflection.rb +7 -0
  62. data/lib/active_model/serializer/collection_serializer.rb +64 -0
  63. data/lib/active_model/serializer/configuration.rb +35 -0
  64. data/lib/active_model/serializer/error_serializer.rb +10 -0
  65. data/lib/active_model/serializer/errors_serializer.rb +27 -0
  66. data/lib/active_model/serializer/field.rb +56 -0
  67. data/lib/active_model/serializer/fieldset.rb +31 -0
  68. data/lib/active_model/serializer/has_many_reflection.rb +10 -0
  69. data/lib/active_model/serializer/has_one_reflection.rb +10 -0
  70. data/lib/active_model/serializer/include_tree.rb +111 -0
  71. data/lib/active_model/serializer/links.rb +35 -0
  72. data/lib/active_model/serializer/lint.rb +156 -0
  73. data/lib/active_model/serializer/meta.rb +29 -0
  74. data/lib/active_model/serializer/null.rb +17 -0
  75. data/lib/active_model/serializer/reflection.rb +147 -0
  76. data/lib/active_model/serializer/singular_reflection.rb +7 -0
  77. data/lib/active_model/serializer/type.rb +25 -0
  78. data/lib/active_model/{serializers → serializer}/version.rb +1 -1
  79. data/lib/active_model/serializer.rb +156 -481
  80. data/lib/active_model_serializers/adapter/attributes.rb +94 -0
  81. data/lib/active_model_serializers/adapter/base.rb +90 -0
  82. data/lib/active_model_serializers/adapter/json.rb +11 -0
  83. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +213 -0
  84. data/lib/active_model_serializers/adapter/json_api/error.rb +96 -0
  85. data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +49 -0
  86. data/lib/active_model_serializers/adapter/json_api/link.rb +83 -0
  87. data/lib/active_model_serializers/adapter/json_api/meta.rb +37 -0
  88. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +57 -0
  89. data/lib/active_model_serializers/adapter/json_api/relationship.rb +52 -0
  90. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +37 -0
  91. data/lib/active_model_serializers/adapter/json_api.rb +513 -0
  92. data/lib/active_model_serializers/adapter/null.rb +10 -0
  93. data/lib/active_model_serializers/adapter.rb +92 -0
  94. data/lib/active_model_serializers/cached_serializer.rb +87 -0
  95. data/lib/active_model_serializers/callbacks.rb +55 -0
  96. data/lib/active_model_serializers/deprecate.rb +55 -0
  97. data/lib/active_model_serializers/deserialization.rb +13 -0
  98. data/lib/active_model_serializers/fragment_cache.rb +118 -0
  99. data/lib/active_model_serializers/json_pointer.rb +14 -0
  100. data/lib/active_model_serializers/key_transform.rb +70 -0
  101. data/lib/active_model_serializers/logging.rb +122 -0
  102. data/lib/active_model_serializers/model.rb +49 -0
  103. data/lib/active_model_serializers/railtie.rb +46 -0
  104. data/lib/active_model_serializers/register_jsonapi_renderer.rb +64 -0
  105. data/lib/active_model_serializers/serializable_resource.rb +81 -0
  106. data/lib/active_model_serializers/serialization_context.rb +32 -0
  107. data/lib/active_model_serializers/test/schema.rb +103 -0
  108. data/lib/active_model_serializers/test/serializer.rb +125 -0
  109. data/lib/active_model_serializers/test.rb +7 -0
  110. data/lib/active_model_serializers.rb +34 -89
  111. data/lib/generators/rails/USAGE +6 -0
  112. data/lib/generators/rails/resource_override.rb +10 -0
  113. data/lib/generators/rails/serializer_generator.rb +36 -0
  114. data/lib/generators/rails/templates/serializer.rb.erb +8 -0
  115. data/lib/grape/active_model_serializers.rb +14 -0
  116. data/lib/grape/formatters/active_model_serializers.rb +15 -0
  117. data/lib/grape/helpers/active_model_serializers.rb +16 -0
  118. data/test/action_controller/adapter_selector_test.rb +53 -0
  119. data/test/action_controller/explicit_serializer_test.rb +134 -0
  120. data/test/action_controller/json/include_test.rb +167 -0
  121. data/test/action_controller/json_api/deserialization_test.rb +112 -0
  122. data/test/action_controller/json_api/errors_test.rb +41 -0
  123. data/test/action_controller/json_api/linked_test.rb +197 -0
  124. data/test/action_controller/json_api/pagination_test.rb +116 -0
  125. data/test/action_controller/json_api/transform_test.rb +180 -0
  126. data/test/action_controller/serialization_scope_name_test.rb +229 -0
  127. data/test/action_controller/serialization_test.rb +467 -0
  128. data/test/active_model_serializers/adapter_for_test.rb +208 -0
  129. data/test/active_model_serializers/cached_serializer_test.rb +80 -0
  130. data/test/active_model_serializers/fragment_cache_test.rb +34 -0
  131. data/test/active_model_serializers/json_pointer_test.rb +20 -0
  132. data/test/active_model_serializers/key_transform_test.rb +263 -0
  133. data/test/active_model_serializers/logging_test.rb +77 -0
  134. data/test/active_model_serializers/model_test.rb +9 -0
  135. data/test/active_model_serializers/railtie_test_isolated.rb +63 -0
  136. data/test/active_model_serializers/serialization_context_test_isolated.rb +58 -0
  137. data/test/active_model_serializers/test/schema_test.rb +128 -0
  138. data/test/active_model_serializers/test/serializer_test.rb +63 -0
  139. data/test/active_record_test.rb +9 -0
  140. data/test/adapter/deprecation_test.rb +100 -0
  141. data/test/adapter/json/belongs_to_test.rb +45 -0
  142. data/test/adapter/json/collection_test.rb +90 -0
  143. data/test/adapter/json/has_many_test.rb +45 -0
  144. data/test/adapter/json/transform_test.rb +93 -0
  145. data/test/adapter/json_api/belongs_to_test.rb +155 -0
  146. data/test/adapter/json_api/collection_test.rb +95 -0
  147. data/test/adapter/json_api/errors_test.rb +78 -0
  148. data/test/adapter/json_api/fields_test.rb +87 -0
  149. data/test/adapter/json_api/has_many_embed_ids_test.rb +43 -0
  150. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +96 -0
  151. data/test/adapter/json_api/has_many_test.rb +143 -0
  152. data/test/adapter/json_api/has_one_test.rb +79 -0
  153. data/test/adapter/json_api/json_api_test.rb +35 -0
  154. data/test/adapter/json_api/linked_test.rb +392 -0
  155. data/test/adapter/json_api/links_test.rb +93 -0
  156. data/test/adapter/json_api/pagination_links_test.rb +148 -0
  157. data/test/adapter/json_api/parse_test.rb +137 -0
  158. data/test/adapter/json_api/relationship_test.rb +161 -0
  159. data/test/adapter/json_api/relationships_test.rb +199 -0
  160. data/test/adapter/json_api/resource_identifier_test.rb +85 -0
  161. data/test/adapter/json_api/resource_meta_test.rb +100 -0
  162. data/test/adapter/json_api/toplevel_jsonapi_test.rb +82 -0
  163. data/test/adapter/json_api/transform_test.rb +500 -0
  164. data/test/adapter/json_api/type_test.rb +61 -0
  165. data/test/adapter/json_test.rb +45 -0
  166. data/test/adapter/null_test.rb +23 -0
  167. data/test/adapter/polymorphic_test.rb +72 -0
  168. data/test/adapter_test.rb +40 -0
  169. data/test/array_serializer_test.rb +35 -73
  170. data/test/benchmark/app.rb +65 -0
  171. data/test/benchmark/benchmarking_support.rb +67 -0
  172. data/test/benchmark/bm_caching.rb +117 -0
  173. data/test/benchmark/bm_transform.rb +34 -0
  174. data/test/benchmark/config.ru +3 -0
  175. data/test/benchmark/controllers.rb +77 -0
  176. data/test/benchmark/fixtures.rb +167 -0
  177. data/test/cache_test.rb +388 -0
  178. data/test/collection_serializer_test.rb +110 -0
  179. data/test/fixtures/active_record.rb +68 -0
  180. data/test/fixtures/poro.rb +254 -0
  181. data/test/generators/scaffold_controller_generator_test.rb +24 -0
  182. data/test/generators/serializer_generator_test.rb +57 -0
  183. data/test/grape_test.rb +82 -0
  184. data/test/include_tree/from_include_args_test.rb +26 -0
  185. data/test/include_tree/from_string_test.rb +94 -0
  186. data/test/include_tree/include_args_to_hash_test.rb +64 -0
  187. data/test/lint_test.rb +49 -0
  188. data/test/logger_test.rb +18 -0
  189. data/test/poro_test.rb +9 -0
  190. data/test/serializable_resource_test.rb +83 -0
  191. data/test/serializers/association_macros_test.rb +36 -0
  192. data/test/serializers/associations_test.rb +267 -0
  193. data/test/serializers/attribute_test.rb +123 -0
  194. data/test/serializers/attributes_test.rb +52 -0
  195. data/test/serializers/caching_configuration_test_isolated.rb +170 -0
  196. data/test/serializers/configuration_test.rb +32 -0
  197. data/test/serializers/fieldset_test.rb +14 -0
  198. data/test/serializers/meta_test.rb +198 -0
  199. data/test/serializers/options_test.rb +21 -0
  200. data/test/serializers/read_attribute_for_serialization_test.rb +79 -0
  201. data/test/serializers/root_test.rb +21 -0
  202. data/test/serializers/serialization_test.rb +55 -0
  203. data/test/serializers/serializer_for_test.rb +134 -0
  204. data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
  205. data/test/support/isolated_unit.rb +80 -0
  206. data/test/support/rails5_shims.rb +47 -0
  207. data/test/support/rails_app.rb +45 -0
  208. data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
  209. data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +6 -0
  210. data/test/support/schemas/custom/show.json +7 -0
  211. data/test/support/schemas/hyper_schema.json +93 -0
  212. data/test/support/schemas/render_using_json_api.json +43 -0
  213. data/test/support/schemas/simple_json_pointers.json +10 -0
  214. data/test/support/serialization_testing.rb +53 -0
  215. data/test/test_helper.rb +51 -24
  216. metadata +456 -45
  217. data/DESIGN.textile +0 -586
  218. data/Gemfile.edge +0 -9
  219. data/bench/perf.rb +0 -43
  220. data/cruft.md +0 -19
  221. data/lib/active_model/array_serializer.rb +0 -104
  222. data/lib/active_record/serializer_override.rb +0 -16
  223. data/lib/generators/resource_override.rb +0 -13
  224. data/lib/generators/serializer/USAGE +0 -9
  225. data/lib/generators/serializer/serializer_generator.rb +0 -42
  226. data/lib/generators/serializer/templates/serializer.rb +0 -19
  227. data/test/association_test.rb +0 -592
  228. data/test/caching_test.rb +0 -96
  229. data/test/generators_test.rb +0 -85
  230. data/test/no_serialization_scope_test.rb +0 -34
  231. data/test/serialization_scope_name_test.rb +0 -67
  232. data/test/serialization_test.rb +0 -392
  233. data/test/serializer_support_test.rb +0 -51
  234. data/test/serializer_test.rb +0 -1465
  235. data/test/test_fakes.rb +0 -217
@@ -0,0 +1,14 @@
1
+ [Back to Guides](../README.md)
2
+
3
+ # Logging
4
+
5
+ The default logger in a Rails application will be `Rails.logger`.
6
+
7
+ When there is no `Rails.logger`, the default logger is an instance of
8
+ `ActiveSupport::TaggedLogging` logging to STDOUT.
9
+
10
+ You may customize the logger in an initializer, for example:
11
+
12
+ ```ruby
13
+ ActiveModelSerializers.logger = Logger.new(STDOUT)
14
+ ```
@@ -0,0 +1,255 @@
1
+ [Back to Guides](../README.md)
2
+
3
+ # Rendering
4
+
5
+ ### Implicit Serializer
6
+
7
+ In your controllers, when you use `render :json`, Rails will now first search
8
+ for a serializer for the object and use it if available.
9
+
10
+ ```ruby
11
+ class PostsController < ApplicationController
12
+ def show
13
+ @post = Post.find(params[:id])
14
+
15
+ render json: @post
16
+ end
17
+ end
18
+ ```
19
+
20
+ In this case, Rails will look for a serializer named `PostSerializer`, and if
21
+ it exists, use it to serialize the `Post`.
22
+
23
+ ### Explicit Serializer
24
+
25
+ If you wish to use a serializer other than the default, you can explicitly pass it to the renderer.
26
+
27
+ #### 1. For a resource:
28
+
29
+ ```ruby
30
+ render json: @post, serializer: PostPreviewSerializer
31
+ ```
32
+
33
+ #### 2. For a resource collection:
34
+
35
+ Specify the serializer for each resource with `each_serializer`
36
+
37
+ ```ruby
38
+ render json: @posts, each_serializer: PostPreviewSerializer
39
+ ```
40
+
41
+ The default serializer for collections is `CollectionSerializer`.
42
+
43
+ Specify the collection serializer with the `serializer` option.
44
+
45
+ ```ruby
46
+ render json: @posts, serializer: CollectionSerializer, each_serializer: PostPreviewSerializer
47
+ ```
48
+
49
+ ## Serializing non-ActiveRecord objects
50
+
51
+ All serializable resources must pass the
52
+ [ActiveModel::Serializer::Lint::Tests](../../lib/active_model/serializer/lint.rb#L17).
53
+
54
+ See the ActiveModelSerializers::Model for a base class that implements the full
55
+ API for a plain-old Ruby object (PORO).
56
+
57
+ ## SerializableResource options
58
+
59
+ The `options` hash passed to `render` or `ActiveModelSerializers::SerializableResource.new(resource, options)`
60
+ are partitioned into `serializer_opts` and `adapter_opts`. `adapter_opts` are passed to new Adapters;
61
+ `serializer_opts` are passed to new Serializers.
62
+
63
+ The `adapter_opts` are specified in [ActiveModelSerializers::SerializableResource::ADAPTER_OPTIONS](../../lib/active_model_serializers/serializable_resource.rb#L5).
64
+ The `serializer_opts` are the remaining options.
65
+
66
+ (In Rails, the `options` are also passed to the `as_json(options)` or `to_json(options)`
67
+ methods on the resource serialization by the Rails JSON renderer. They are, therefore, important
68
+ to know about, but not part of ActiveModelSerializers.)
69
+
70
+ See [ARCHITECTURE](../ARCHITECTURE.md) for more information.
71
+
72
+ ### adapter_opts
73
+
74
+ #### fields
75
+
76
+ PR please :)
77
+
78
+ #### adapter
79
+
80
+ PR please :)
81
+
82
+ #### key_transform
83
+
84
+ ```render json: posts, each_serializer: PostSerializer, key_transform: :camel_lower```
85
+
86
+ See [Key Transforms](key_transforms.md) for more informaiton.
87
+
88
+ #### meta
89
+
90
+ A `meta` member can be used to include non-standard meta-information. `meta` can
91
+ be utilized in several levels in a response.
92
+
93
+ ##### Top-level
94
+
95
+ To set top-level `meta` in a response, specify it in the `render` call.
96
+
97
+ ```ruby
98
+ render json: @post, meta: { total: 10 }
99
+ ```
100
+
101
+ The key can be customized using `meta_key` option.
102
+
103
+ ```ruby
104
+ render json: @post, meta: { total: 10 }, meta_key: "custom_meta"
105
+ ```
106
+
107
+ `meta` will only be included in your response if you are using an Adapter that
108
+ supports `root`, e.g., `JsonApi` and `Json` adapters. The default adapter,
109
+ `Attributes` does not have `root`.
110
+
111
+
112
+ ##### Resource-level
113
+
114
+ To set resource-level `meta` in a response, define meta in a serializer with one
115
+ of the following methods:
116
+
117
+ As a single, static string.
118
+
119
+ ```ruby
120
+ meta stuff: 'value'
121
+ ```
122
+
123
+ As a block containing a Hash.
124
+
125
+ ```ruby
126
+ meta do
127
+ {
128
+ rating: 4,
129
+ comments_count: object.comments.count
130
+ }
131
+ end
132
+ ```
133
+
134
+
135
+ #### links
136
+
137
+ If you wish to use Rails url helpers for link generation, e.g., `link(:resources) { resources_url }`, ensure your application sets
138
+ `Rails.application.routes.default_url_options`.
139
+
140
+ ##### Top-level
141
+
142
+ JsonApi supports a [links object](http://jsonapi.org/format/#document-links) to be specified at top-level, that you can specify in the `render`:
143
+
144
+ ```ruby
145
+ links_object = {
146
+ href: "http://example.com/api/posts",
147
+ meta: {
148
+ count: 10
149
+ }
150
+ }
151
+ render json: @posts, links: links_object
152
+ ```
153
+
154
+ That's the result:
155
+
156
+ ```json
157
+ {
158
+ "data": [
159
+ {
160
+ "type": "posts",
161
+ "id": "1",
162
+ "attributes": {
163
+ "title": "JSON API is awesome!",
164
+ "body": "You should be using JSON API",
165
+ "created": "2015-05-22T14:56:29.000Z",
166
+ "updated": "2015-05-22T14:56:28.000Z"
167
+ }
168
+ }
169
+ ],
170
+ "links": {
171
+ "href": "http://example.com/api/posts",
172
+ "meta": {
173
+ "count": 10
174
+ }
175
+ }
176
+ }
177
+ ```
178
+
179
+ This feature is specific to JsonApi, so you have to use the use the [JsonApi Adapter](adapters.md#jsonapi)
180
+
181
+
182
+ ##### Resource-level
183
+
184
+ In your serializer, define each link in one of the following methods:
185
+
186
+ As a static string
187
+
188
+ ```ruby
189
+ link :link_name, 'https://example.com/resource'
190
+ ```
191
+
192
+ As a block to be evaluated. When using Rails, URL helpers are available.
193
+ Ensure your application sets `Rails.application.routes.default_url_options`.
194
+
195
+ ```ruby
196
+ link :link_name_ do
197
+ "https://example.com/resource/#{object.id}"
198
+ end
199
+
200
+ link(:link_name) { "https://example.com/resource/#{object.id}" }
201
+
202
+ link(:link_name) { resource_url(object) }
203
+
204
+ link(:link_name) { url_for(controller: 'controller_name', action: 'index', only_path: false) }
205
+
206
+ ```
207
+
208
+ ### serializer_opts
209
+
210
+ #### include
211
+
212
+ PR please :)
213
+
214
+ #### Overriding the root key
215
+
216
+ Overriding the resource root only applies when using the JSON adapter.
217
+
218
+ Normally, the resource root is derived from the class name of the resource being serialized.
219
+ e.g. `UserPostSerializer.new(UserPost.new)` will be serialized with the root `user_post` or `user_posts` according the adapter collection pluralization rules.
220
+
221
+ When using the JSON adapter in your initializer (ActiveModelSerializers.config.adapter = :json), or passing in the adapter in your render call, you can specify the root by passing it as an argument to `render`. For example:
222
+
223
+ ```ruby
224
+ render json: @user_post, root: "admin_post", adapter: :json
225
+ ```
226
+
227
+ This will be rendered as:
228
+ ```json
229
+ {
230
+ "admin_post": {
231
+ "title": "how to do open source"
232
+ }
233
+ }
234
+ ```
235
+ Note: the `Attributes` adapter (default) does not include a resource root. You also will not be able to create a single top-level root if you are using the :json_api adapter.
236
+
237
+ #### serializer
238
+
239
+ PR please :)
240
+
241
+ #### scope
242
+
243
+ PR please :)
244
+
245
+ #### scope_name
246
+
247
+ PR please :)
248
+
249
+ ## Using a serializer without `render`
250
+
251
+ See [Usage outside of a controller](../howto/outside_controller_use.md#serializing-before-controller-render).
252
+
253
+ ## Pagination
254
+
255
+ See [How to add pagination links](https://github.com/rails-api/active_model_serializers/blob/master/docs/howto/add_pagination_links.md).
@@ -0,0 +1,339 @@
1
+ [Back to Guides](../README.md)
2
+
3
+ # Serializers
4
+
5
+ Given a serializer class:
6
+
7
+ ```ruby
8
+ class SomeSerializer < ActiveModel::Serializer
9
+ end
10
+ ```
11
+
12
+ The following methods may be defined in it:
13
+
14
+ ### Attributes
15
+
16
+ #### ::attributes
17
+
18
+ Serialization of the resource `title` and `body`
19
+
20
+ | In Serializer | #attributes |
21
+ |---------------------------- |-------------|
22
+ | `attributes :title, :body` | `{ title: 'Some Title', body: 'Some Body' }`
23
+ | `attributes :title, :body`<br>`def body "Special #{object.body}" end` | `{ title: 'Some Title', body: 'Special Some Body' }`
24
+
25
+
26
+ #### ::attribute
27
+
28
+ Serialization of the resource `title`
29
+
30
+ | In Serializer | #attributes |
31
+ |---------------------------- |-------------|
32
+ | `attribute :title` | `{ title: 'Some Title' } `
33
+ | `attribute :title, key: :name` | `{ name: 'Some Title' } `
34
+ | `attribute :title { 'A Different Title'}` | `{ title: 'A Different Title' } `
35
+ | `attribute :title`<br>`def title 'A Different Title' end` | `{ title: 'A Different Title' }`
36
+
37
+ [PR please for conditional attributes:)](https://github.com/rails-api/active_model_serializers/pull/1403)
38
+
39
+ ### Associations
40
+
41
+ #### ::has_one
42
+
43
+ e.g.
44
+
45
+ ```ruby
46
+ has_one :bio
47
+ has_one :blog, key: :site
48
+ has_one :maker, virtual_value: { id: 1 }
49
+
50
+ has_one :blog do |serializer|
51
+ serializer.cached_blog
52
+ end
53
+
54
+ def cached_blog
55
+ cache_store.fetch("cached_blog:#{object.updated_at}") do
56
+ Blog.find(object.blog_id)
57
+ end
58
+ end
59
+ ```
60
+
61
+ #### ::has_many
62
+
63
+ e.g.
64
+
65
+ ```ruby
66
+ has_many :comments
67
+ has_many :comments, key: :reviews
68
+ has_many :comments, serializer: CommentPreviewSerializer
69
+ has_many :reviews, virtual_value: [{ id: 1 }, { id: 2 }]
70
+ has_many :comments, key: :last_comments do
71
+ last(1)
72
+ end
73
+ ```
74
+
75
+ #### ::belongs_to
76
+
77
+ e.g.
78
+
79
+ ```ruby
80
+ belongs_to :author, serializer: AuthorPreviewSerializer
81
+ belongs_to :author, key: :writer
82
+ belongs_to :post
83
+ belongs_to :blog
84
+ def blog
85
+ Blog.new(id: 999, name: 'Custom blog')
86
+ end
87
+ ```
88
+
89
+ ### Polymorphic Relationships
90
+
91
+ Polymorphic relationships are serialized by specifying the relationship, like any other association. For example:
92
+
93
+ ```ruby
94
+ class PictureSerializer < ActiveModel::Serializer
95
+ has_one :imageable
96
+ end
97
+ ```
98
+
99
+ For more context, see the [tests](../../test/adapter/polymorphic_test.rb) for each adapter.
100
+
101
+ ### Caching
102
+
103
+ #### ::cache
104
+
105
+ e.g.
106
+
107
+ ```ruby
108
+ cache key: 'post', expires_in: 0.1, skip_digest: true
109
+ cache expires_in: 1.day, skip_digest: true
110
+ cache key: 'writer', skip_digest: true
111
+ cache only: [:name], skip_digest: true
112
+ cache except: [:content], skip_digest: true
113
+ cache key: 'blog'
114
+ cache only: [:id]
115
+ ```
116
+
117
+ #### #cache_key
118
+
119
+ e.g.
120
+
121
+ ```ruby
122
+ # Uses a custom non-time-based cache key
123
+ def cache_key
124
+ "#{self.class.name.downcase}/#{self.id}"
125
+ end
126
+ ```
127
+
128
+ ### Other
129
+
130
+ #### ::type
131
+
132
+ The `::type` method defines the JSONAPI [type](http://jsonapi.org/format/#document-resource-object-identification) that will be rendered for this serializer.
133
+ It either takes a `String` or `Symbol` as parameter.
134
+
135
+ Note: This method is useful only when using the `:json_api` adapter.
136
+
137
+ Examples:
138
+ ```ruby
139
+ class UserProfileSerializer < ActiveModel::Serializer
140
+ type 'profile'
141
+ end
142
+ class AuthorProfileSerializer < ActiveModel::Serializer
143
+ type :profile
144
+ end
145
+ ```
146
+
147
+ With the `:json_api` adapter, the previous serializers would be rendered as:
148
+
149
+ ``` json
150
+ {
151
+ "data": {
152
+ "id": "1",
153
+ "type": "profile"
154
+ }
155
+ }
156
+ ```
157
+
158
+ #### ::link
159
+
160
+ ```ruby
161
+ link :self do
162
+ href "https://example.com/link_author/#{object.id}"
163
+ end
164
+ link :author { link_author_url(object) }
165
+ link :link_authors { link_authors_url }
166
+ link :other, 'https://example.com/resource'
167
+ link :posts { link_author_posts_url(object) }
168
+ ```
169
+
170
+ #### #object
171
+
172
+ The object being serialized.
173
+
174
+ #### #root
175
+
176
+ PR please :)
177
+
178
+ #### #scope
179
+
180
+ Allows you to include in the serializer access to an external method.
181
+
182
+ It's intended to provide an authorization context to the serializer, so that
183
+ you may e.g. show an admin all comments on a post, else only published comments.
184
+
185
+ - `scope` is a method on the serializer instance that comes from `options[:scope]`. It may be nil.
186
+ - `scope_name` is an option passed to the new serializer (`options[:scope_name]`). The serializer
187
+ defines a method with that name that calls the `scope`, e.g. `def current_user; scope; end`.
188
+ Note: it does not define the method if the serializer instance responds to it.
189
+
190
+ That's a lot of words, so here's some examples:
191
+
192
+ First, let's assume the serializer is instantiated in the controller, since that's the usual scenario.
193
+ We'll refer to the serialization context as `controller`.
194
+
195
+ | options | `Serializer#scope` | method definition |
196
+ |-------- | ------------------|--------------------|
197
+ | `scope: current_user, scope_name: :current_user` | `current_user` | `Serializer#current_user` calls `controller.current_user`
198
+ | `scope: view_context, scope_name: :view_context` | `view_context` | `Serializer#view_context` calls `controller.view_context`
199
+
200
+ We can take advantage of the scope to customize the objects returned based
201
+ on the current user (scope).
202
+
203
+ For example, we can limit the posts the current user sees to those they created:
204
+
205
+ ```ruby
206
+ class PostSerializer < ActiveModel::Serializer
207
+ attributes :id, :title, :body
208
+
209
+ # scope comments to those created_by the current user
210
+ has_many :comments do
211
+ object.comments.where(created_by: current_user)
212
+ end
213
+ end
214
+ ```
215
+
216
+ Whether you write the method as above or as `object.comments.where(created_by: scope)`
217
+ is a matter of preference (assuming `scope_name` has been set).
218
+
219
+ ##### Controller Authorization Context
220
+
221
+ In the controller, the scope/scope_name options are equal to
222
+ the [`serialization_scope`method](https://github.com/rails-api/active_model_serializers/blob/d02cd30fe55a3ea85e1d351b6e039620903c1871/lib/action_controller/serialization.rb#L13-L20),
223
+ which is `:current_user`, by default.
224
+
225
+ Specfically, the `scope_name` is defaulted to `:current_user`, and may be set as
226
+ `serialization_scope :view_context`. The `scope` is set to `send(scope_name)` when `scope_name` is
227
+ present and the controller responds to `scope_name`.
228
+
229
+ Thus, in a serializer, the controller provides `current_user` as the
230
+ current authorization scope when you call `render :json`.
231
+
232
+ **IMPORTANT**: Since the scope is set at render, you may want to customize it so that `current_user` isn't
233
+ called on every request. This was [also a problem](https://github.com/rails-api/active_model_serializers/pull/1252#issuecomment-159810477)
234
+ in [`0.9`](https://github.com/rails-api/active_model_serializers/tree/0-9-stable#customizing-scope).
235
+
236
+ We can change the scope from `current_user` to `view_context`.
237
+
238
+ ```diff
239
+ class SomeController < ActionController::Base
240
+ + serialization_scope :view_context
241
+
242
+ def current_user
243
+ User.new(id: 2, name: 'Bob', admin: true)
244
+ end
245
+
246
+ def edit
247
+ user = User.new(id: 1, name: 'Pete')
248
+ render json: user, serializer: AdminUserSerializer, adapter: :json_api
249
+ end
250
+ end
251
+ ```
252
+
253
+ We could then use the controller method `view_context` in our serializer, like so:
254
+
255
+ ```diff
256
+ class AdminUserSerializer < ActiveModel::Serializer
257
+ attributes :id, :name, :can_edit
258
+
259
+ def can_edit?
260
+ + view_context.current_user.admin?
261
+ end
262
+ end
263
+ ```
264
+
265
+ So that when we render the `#edit` action, we'll get
266
+
267
+ ```json
268
+ {"data":{"id":"1","type":"users","attributes":{"name":"Pete","can_edit":true}}}
269
+ ```
270
+
271
+ Where `can_edit` is `view_context.current_user.admin?` (true).
272
+
273
+ #### #read_attribute_for_serialization(key)
274
+
275
+ The serialized value for a given key. e.g. `read_attribute_for_serialization(:title) #=> 'Hello World'`
276
+
277
+ #### #links
278
+
279
+ PR please :)
280
+
281
+ #### #json_key
282
+
283
+ PR please :)
284
+
285
+ ## Examples
286
+
287
+ Given two models, a `Post(title: string, body: text)` and a
288
+ `Comment(name: string, body: text, post_id: integer)`, you will have two
289
+ serializers:
290
+
291
+ ```ruby
292
+ class PostSerializer < ActiveModel::Serializer
293
+ cache key: 'posts', expires_in: 3.hours
294
+ attributes :title, :body
295
+
296
+ has_many :comments
297
+ end
298
+ ```
299
+
300
+ and
301
+
302
+ ```ruby
303
+ class CommentSerializer < ActiveModel::Serializer
304
+ attributes :name, :body
305
+
306
+ belongs_to :post
307
+ end
308
+ ```
309
+
310
+ Generally speaking, you, as a user of ActiveModelSerializers, will write (or generate) these
311
+ serializer classes.
312
+
313
+ ## More Info
314
+
315
+ For more information, see [the Serializer class on GitHub](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializer.rb)
316
+
317
+ ## Overriding association methods
318
+
319
+ To override an association, call `has_many`, `has_one` or `belongs_to` with a block:
320
+
321
+ ```ruby
322
+ class PostSerializer < ActiveModel::Serializer
323
+ has_many :comments do
324
+ object.comments.active
325
+ end
326
+ end
327
+ ```
328
+
329
+ ## Overriding attribute methods
330
+
331
+ To override an attribute, call `attribute` with a block:
332
+
333
+ ```ruby
334
+ class PostSerializer < ActiveModel::Serializer
335
+ attribute :body do
336
+ object.body.downcase
337
+ end
338
+ end
339
+ ```
Binary file