docs/general/serializers.md in active_model_serializers-0.10.6 vs docs/general/serializers.md in active_model_serializers-0.10.7

- old
+ new

@@ -292,10 +292,12 @@ ``` Whether you write the method as above or as `object.comments.where(created_by: scope)` is a matter of preference (assuming `scope_name` has been set). +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. + ##### Controller Authorization Context In the controller, the scope/scope_name options are equal to the [`serialization_scope`method](https://github.com/rails-api/active_model_serializers/blob/d02cd30fe55a3ea85e1d351b6e039620903c1871/lib/action_controller/serialization.rb#L13-L20), which is `:current_user`, by default. @@ -309,11 +311,11 @@ **IMPORTANT**: Since the scope is set at render, you may want to customize it so that `current_user` isn't called on every request. This was [also a problem](https://github.com/rails-api/active_model_serializers/pull/1252#issuecomment-159810477) in [`0.9`](https://github.com/rails-api/active_model_serializers/tree/0-9-stable#customizing-scope). -We can change the scope from `current_user` to `view_context`. +We can change the scope from `current_user` to `view_context`, which is included in subclasses of `ActionController::Base`. ```diff class SomeController < ActionController::Base + serialization_scope :view_context @@ -377,9 +379,10 @@ def current_user User.new(id: 2, name: 'Bob', admin: false) end end ``` +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`. #### #read_attribute_for_serialization(key) The serialized value for a given key. e.g. `read_attribute_for_serialization(:title) #=> 'Hello World'`