Skip to content

Commit 1e13c91

Browse files
committed
Adding documentation on conditional attributes
Adding documentation and short example ([from this pull request](rails-api#1403)) on conditional attributes.
1 parent ec15fa9 commit 1e13c91

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

docs/general/serializers.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ Serialization of the resource `title`
3434
| `attribute :title { 'A Different Title'}` | `{ title: 'A Different Title' } `
3535
| `attribute :title`<br>`def title 'A Different Title' end` | `{ title: 'A Different Title' }`
3636

37+
An `if` or `unless` parameter can make an attribute conditional. It take a symbol of a method name on the serializer.
38+
39+
e.g.
40+
41+
```ruby
42+
attribute :private_data, if: :is_current_user?
43+
44+
def is_current_user?
45+
object.id == current_user.id
46+
end
47+
```
48+
3749
[PR please for conditional attributes:)](https://github.com/rails-api/active_model_serializers/pull/1403)
3850

3951
### Associations

0 commit comments

Comments
 (0)