Skip to content

Commit efdee60

Browse files
committed
Adding documentation on conditional attributes
Adding documentation and short example ([from this pull request](rails-api#1403)) on conditional attributes. Adding lambda literal notation and example. Adding lambda literal notation and example, and fixing typo. Removing PR reminder Adding Changelog entry Moving CHANGELOG entry under master (unreleased) Use option instead of parameter
1 parent b5e2b41 commit efdee60

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)