Access doorkeeper_token (or current_user) in Active_model_serializers

I am working on a Rails API created using RocketPants . I use active_model_serializers to serialize JSON , and Doorkeeper for OAuth .

The problem is accessing current_userhelper method c class UserSerializer < ActiveModel::Serializer. Mistake:

NameError (undefined local variable or method `request' for #<UserSerializer:0xb5892118>)

current_user helper uses this snippet:

User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token

and doorkeeper_token:

def doorkeeper_token
    methods = Doorkeeper.configuration.access_token_methods
    @token ||= OAuth::Token.authenticate request, *methods
end

So, as I found out, there is no object requestavailable in Serializer. How can I make it available? Or should there be another way to implement current_user?

Thank you in advance

+4
source share
2

, - scope, active-model-serializers. : scope Serializer - it nil. serialization_scope :current_user no method error, ... :

class Api::V1::BaseController < RocketPants::Base
    include ActionController::Serialization
    ............

: can't use 'merge' on Nil. ,

def default_serializer_options
  {
    :url_options => url_options,
    :root        => false
  }
end

ActionController::Serialization , , nil.

, super - , scope nil. , :scope => serialization_scope default_serializer_options. scope .

- , , - .

, - , =)

+2

, , , , ActiveModel::Serializer current_user , scope , , , current_user , , , :

if scope.is_admin?
  #your code here
end
0

All Articles