Skip to content

Commit 0bbeeb3

Browse files
committed
Update serializers.md
* Add note that any controller reference is acceptable for `serialization_scope` * Add note about `ActionController::API` and `view_context` set to `nil`, with respect to accessing helpers in a Serializer * refs #2144 * Update CHANGELOG.md
1 parent 4076a48 commit 0bbeeb3

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Features:
1212
- [#2189](https://github.com/rails-api/active_model_serializers/pull/2189)
1313
Update version constraint for jsonapi-renderer to `['>= 0.1.1.beta1', '< 0.3']`
1414
(@tagliala)
15+
- [#2215](https://github.com/rails-api/active_model_serializers/pull/2215) Update `serializers.md` documentation to denote alternate use cases for `scope`. (@stratigos)
1516

1617
Fixes:
1718

docs/general/serializers.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ end
294294
Whether you write the method as above or as `object.comments.where(created_by: scope)`
295295
is a matter of preference (assuming `scope_name` has been set).
296296

297+
Keep in mind that the scope can be set to any available controller reference. This can be utilized to provide access to any other data scopes or presentation helpers.
298+
297299
##### Controller Authorization Context
298300

299301
In the controller, the scope/scope_name options are equal to
@@ -311,7 +313,7 @@ current authorization scope when you call `render :json`.
311313
called on every request. This was [also a problem](https://github.com/rails-api/active_model_serializers/pull/1252#issuecomment-159810477)
312314
in [`0.9`](https://github.com/rails-api/active_model_serializers/tree/0-9-stable#customizing-scope).
313315

314-
We can change the scope from `current_user` to `view_context`.
316+
We can change the scope from `current_user` to `view_context`, which is included in subclasses of `ActionController::Base`.
315317

316318
```diff
317319
class SomeController < ActionController::Base
@@ -379,6 +381,7 @@ class PostsController < ActionController::Base
379381
end
380382
end
381383
```
384+
Note that any controller reference which provides the desired scope is acceptable, such as another controller method for loading a different resource or reference to helpers. For example, `ActionController::API` does not include `ActionView::ViewContext`, and would need a different reference for passing any helpers into a serializer via `serialization_scope`.
382385

383386
#### #read_attribute_for_serialization(key)
384387

0 commit comments

Comments
 (0)