File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -314,6 +314,38 @@ So that when we render the `#edit` action, we'll get
314
314
315
315
Where ` can_edit ` is ` view_context.current_user.admin? ` (true).
316
316
317
+ You can also tell what to set as ` serialization_scope ` for specific actions.
318
+
319
+ For example, use ` admin_user ` only for ` Admin::PostSerializer ` and ` current_user ` for rest.
320
+
321
+ ``` ruby
322
+ class PostsController < ActionController ::Base
323
+
324
+ before_action only: :edit do
325
+ self .class .serialization_scope :admin_user
326
+ end
327
+
328
+ def show
329
+ render json: @post , serializer: PostSerializer
330
+ end
331
+
332
+ def edit
333
+ @post .save
334
+ render json: @post , serializer: Admin ::PostSerializer
335
+ end
336
+
337
+ private
338
+
339
+ def admin_user
340
+ User .new (id: 2 , name: ' Bob' , admin: true )
341
+ end
342
+
343
+ def current_user
344
+ User .new (id: 2 , name: ' Bob' , admin: false )
345
+ end
346
+ end
347
+ ```
348
+
317
349
#### #read_attribute_for_serialization(key)
318
350
319
351
The serialized value for a given key. e.g. ` read_attribute_for_serialization(:title) #=> 'Hello World' `
You can’t perform that action at this time.
0 commit comments