Add doc for setting conditional serialization_scope

This commit is contained in:
Yogesh Khater 2016-06-30 17:00:19 +05:30
parent c029c00758
commit 156dc232ce

View File

@ -314,6 +314,38 @@ So that when we render the `#edit` action, we'll get
Where `can_edit` is `view_context.current_user.admin?` (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) #### #read_attribute_for_serialization(key)
The serialized value for a given key. e.g. `read_attribute_for_serialization(:title) #=> 'Hello World'` The serialized value for a given key. e.g. `read_attribute_for_serialization(:title) #=> 'Hello World'`