You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can use `is_collection` option to have better control over collection serialization.
267
+
268
+
If this option is not provided or `nil` autedetect logic is used to try understand
269
+
if provided resource is a single object or collection.
270
+
271
+
Autodetect logic is compatible with most DB toolkits (ActiveRecord, Sequel, etc.) but
272
+
**cannot** guarantee that single vs collection will be always detected properly.
273
+
274
+
```ruby
275
+
options[:is_collection]
276
+
```
277
+
278
+
was introduced to be able to have precise control this behavior
279
+
280
+
-`nil` or not provided: will try to autodetect single vs collection (please, see notes above)
281
+
-`true` will always treat input resource as *collection*
282
+
-`false` will always treat input resource as *single object*
283
+
262
284
### Caching
263
285
Requires a `cache_key` method be defined on model:
264
286
@@ -307,6 +329,53 @@ serializer.serializable_hash
307
329
Custom attributes and relationships that only receive the resource are still possible by defining
308
330
the block to only receive one argument.
309
331
332
+
### Conditional Attributes
333
+
334
+
Conditional attributes can be defined by passing a Proc to the `if` key on the `attribute` method. Return `true` if the attribute should be serialized, and `false` if not. The record and any params passed to the serializer are available inside the Proc as the first and second parameters, respectively.
335
+
336
+
```ruby
337
+
classMovieSerializer
338
+
includeFastJsonapi::ObjectSerializer
339
+
340
+
attributes :name, :year
341
+
attribute :release_year, if:Proc.newdo |record|
342
+
# Release year will only be serialized if it's greater than 1990
Conditional relationships can be defined by passing a Proc to the `if` key. Return `true` if the relationship should be serialized, and `false` if not. The record and any params passed to the serializer are available inside the Proc as the first and second parameters, respectively.
361
+
362
+
```ruby
363
+
classMovieSerializer
364
+
includeFastJsonapi::ObjectSerializer
365
+
366
+
# Actors will only be serialized if the record has any associated actors
0 commit comments