docs/general/serializers.md in active_model_serializers-0.10.1 vs docs/general/serializers.md in active_model_serializers-0.10.2
- old
+ new
@@ -312,9 +312,41 @@
{"data":{"id":"1","type":"users","attributes":{"name":"Pete","can_edit":true}}}
```
Where `can_edit` is `view_context.current_user.admin?` (true).
+You can also tell what to set as `serialization_scope` for specific actions.
+
+For example, use `admin_user` only for `Admin::PostSerializer` and `current_user` for rest.
+
+```ruby
+class PostsController < ActionController::Base
+
+ before_action only: :edit do
+ self.class.serialization_scope :admin_user
+ end
+
+ def show
+ render json: @post, serializer: PostSerializer
+ end
+
+ def edit
+ @post.save
+ render json: @post, serializer: Admin::PostSerializer
+ end
+
+ private
+
+ def admin_user
+ User.new(id: 2, name: 'Bob', admin: true)
+ end
+
+ def current_user
+ User.new(id: 2, name: 'Bob', admin: false)
+ end
+end
+```
+
#### #read_attribute_for_serialization(key)
The serialized value for a given key. e.g. `read_attribute_for_serialization(:title) #=> 'Hello World'`
#### #links