From 17531cd60cb9442ad692965399be5aad54a02907 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Aubin?= Date: Wed, 18 May 2016 13:01:56 +0200 Subject: [PATCH] Adding documentation on conditional attributes Adding documentation and short example ([from this pull request](https://github.com/rails-api/active_model_serializers/pull/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) --- CHANGELOG.md | 1 + docs/general/serializers.md | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0684c14a5..d49d07ade 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/docs/general/serializers.md b/docs/general/serializers.md index b0d99ad1d..c8e4e830b 100644 --- a/docs/general/serializers.md +++ b/docs/general/serializers.md @@ -34,7 +34,18 @@ Serialization of the resource `title` | `attribute :title { 'A Different Title'}` | `{ title: 'A Different Title' } ` | `attribute :title`
`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` parameter 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