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
Attributes can also use a different name by passing the original method or accessor with a proc shortcut:
212
+
213
+
```ruby
214
+
classMovieSerializer
215
+
includeFastJsonapi::ObjectSerializer
216
+
217
+
attributes :name
218
+
219
+
attribute :released_in_year, &:year
220
+
end
221
+
```
222
+
208
223
### Links Per Object
209
224
Links are defined in FastJsonapi using the `link` method. By default, link are read directly from the model property of the same name.In this example, `public_url` is expected to be a property of the object being serialized.
You can use `is_collection` option to have better control over collection serialization.
280
+
281
+
If this option is not provided or `nil` autedetect logic is used to try understand
282
+
if provided resource is a single object or collection.
283
+
284
+
Autodetect logic is compatible with most DB toolkits (ActiveRecord, Sequel, etc.) but
285
+
**cannot** guarantee that single vs collection will be always detected properly.
286
+
287
+
```ruby
288
+
options[:is_collection]
289
+
```
290
+
291
+
was introduced to be able to have precise control this behavior
292
+
293
+
-`nil` or not provided: will try to autodetect single vs collection (please, see notes above)
294
+
-`true` will always treat input resource as *collection*
295
+
-`false` will always treat input resource as *single object*
296
+
262
297
### Caching
263
298
Requires a `cache_key` method be defined on model:
264
299
@@ -284,7 +319,6 @@ block you opt-in to using params by adding it as a block parameter.
284
319
285
320
```ruby
286
321
classMovieSerializer
287
-
classMovieSerializer
288
322
includeFastJsonapi::ObjectSerializer
289
323
290
324
attributes :name, :year
@@ -308,6 +342,68 @@ serializer.serializable_hash
308
342
Custom attributes and relationships that only receive the resource are still possible by defining
309
343
the block to only receive one argument.
310
344
345
+
### Conditional Attributes
346
+
347
+
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.
348
+
349
+
```ruby
350
+
classMovieSerializer
351
+
includeFastJsonapi::ObjectSerializer
352
+
353
+
attributes :name, :year
354
+
attribute :release_year, if:Proc.newdo |record|
355
+
# 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.
374
+
375
+
```ruby
376
+
classMovieSerializer
377
+
includeFastJsonapi::ObjectSerializer
378
+
379
+
# Actors will only be serialized if the record has any associated actors
0 commit comments