Skip to content

Commit 835aad3

Browse files
committed
Merge pull request #1734 from lambda2/patch-1
Adding documentation on conditional attributes
2 parents b5e2b41 + efdee60 commit 835aad3

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Features:
99
Fixes:
1010

1111
Misc:
12+
- [#1734](https://github.com/rails-api/active_model_serializers/pull/1734) Adds documentation for conditional attribute (@lambda2)
1213

1314
### [v0.10.0 (2016-05-17)](https://github.com/rails-api/active_model_serializers/compare/4a2d9853ba7...v0.10.0)
1415

docs/general/serializers.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +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-
[PR please for conditional attributes:)](https://github.com/rails-api/active_model_serializers/pull/1403)
37+
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.
38+
39+
e.g.
40+
41+
```ruby
42+
attribute :private_data, if: :is_current_user?
43+
attribute :another_private_data, if: -> { scope.admin? }
44+
45+
def is_current_user?
46+
object.id == current_user.id
47+
end
48+
```
3849

3950
### Associations
4051

0 commit comments

Comments
 (0)