mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge pull request #1734 from lambda2/patch-1
Adding documentation on conditional attributes
This commit is contained in:
commit
835aad3c74
@ -9,6 +9,7 @@ Features:
|
||||
Fixes:
|
||||
|
||||
Misc:
|
||||
- [#1734](https://github.com/rails-api/active_model_serializers/pull/1734) Adds documentation for conditional attribute (@lambda2)
|
||||
|
||||
### [v0.10.0 (2016-05-17)](https://github.com/rails-api/active_model_serializers/compare/4a2d9853ba7...v0.10.0)
|
||||
|
||||
|
||||
@ -34,7 +34,18 @@ Serialization of the resource `title`
|
||||
| `attribute :title { 'A Different Title'}` | `{ title: 'A Different Title' } `
|
||||
| `attribute :title`<br>`def title 'A Different Title' end` | `{ title: 'A Different Title' }`
|
||||
|
||||
[PR please for conditional attributes:)](https://github.com/rails-api/active_model_serializers/pull/1403)
|
||||
An `if` or `unless` option can make an attribute conditional. It takes a symbol of a method name on the serializer, or a lambda literal.
|
||||
|
||||
e.g.
|
||||
|
||||
```ruby
|
||||
attribute :private_data, if: :is_current_user?
|
||||
attribute :another_private_data, if: -> { scope.admin? }
|
||||
|
||||
def is_current_user?
|
||||
object.id == current_user.id
|
||||
end
|
||||
```
|
||||
|
||||
### Associations
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user