mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
parent
55baaf2005
commit
594eaa1576
10
README.md
10
README.md
@ -358,7 +358,7 @@ and use it to serialize the comment.
|
|||||||
By default, serializers simply look up the association on the original object.
|
By default, serializers simply look up the association on the original object.
|
||||||
You can customize this behavior by implementing a method with the name of the
|
You can customize this behavior by implementing a method with the name of the
|
||||||
association and returning a different Array. Often, you will do this to
|
association and returning a different Array. Often, you will do this to
|
||||||
customize the objects returned based on the current user.
|
customize the objects returned based on the current user (scope).
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class PostSerializer < ActiveModel::Serializer
|
class PostSerializer < ActiveModel::Serializer
|
||||||
@ -367,7 +367,7 @@ class PostSerializer < ActiveModel::Serializer
|
|||||||
|
|
||||||
# only let the user see comments he created.
|
# only let the user see comments he created.
|
||||||
def comments
|
def comments
|
||||||
object.comments.where(created_by: current_user)
|
object.comments.where(created_by: scope)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
@ -409,7 +409,7 @@ class PostSerializer < ActiveModel::Serializer
|
|||||||
has_many :comments
|
has_many :comments
|
||||||
|
|
||||||
def filter(keys)
|
def filter(keys)
|
||||||
keys.delete :author unless current_user.admin?
|
keys.delete :author unless scope.admin?
|
||||||
keys.delete :comments if object.comments_disabled?
|
keys.delete :comments if object.comments_disabled?
|
||||||
keys
|
keys
|
||||||
end
|
end
|
||||||
@ -649,7 +649,7 @@ class CitiesController < ApplicationController
|
|||||||
def show
|
def show
|
||||||
@city = City.find(params[:id])
|
@city = City.find(params[:id])
|
||||||
|
|
||||||
render json: @city, scope: current_admin, scope_name: :current_admin
|
render json: @city, scope: current_admin
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
@ -674,7 +674,7 @@ class PostSerializer < ActiveModel::Serializer
|
|||||||
attributes :title, :body
|
attributes :title, :body
|
||||||
|
|
||||||
def cache_key
|
def cache_key
|
||||||
[object, current_user]
|
[object, scope]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user