Skip to content

Commit

Permalink
Adding documentation on conditional attributes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lambda2 committed May 19, 2016
1 parent b5e2b41 commit efdee60
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
13 changes: 12 additions & 1 deletion docs/general/serializers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit efdee60

Please sign in to comment.