Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes it possible to use current_user within serializers #24

Merged
merged 1 commit into from
Oct 6, 2014

Conversation

sonxurxo
Copy link
Contributor

Setting the scope to the endpoint makes it possible to use current_user within serializers.

Serializers must delegate :current_user to the scope, like this:

class BaseSerializer < ActiveModel::Serializer
  delegate :current_user, to: :scope
end

I personally use this BaseSerializer and then extend all my serializers from this one.

@jrhe
Copy link
Member

jrhe commented Sep 24, 2014

Looks good but the tests need fixing and this needs its own tests. Can you write some then I will merge it. Cheers.

jrhe added a commit that referenced this pull request Oct 6, 2014
Makes it possible to use current_user within serializers
@jrhe jrhe merged commit 85b92a4 into ruby-grape:master Oct 6, 2014
@anujmiddha
Copy link

On trying this, but I am getting an error

UserSerializer#current_user delegated to scope.current_user, but scope is nil

I am setting the scope to the endpoint as

get "", scope: @current_user do
...
end

Did you face anything similar?

Thanks

@railslauncher
Copy link

@anujmiddha you need to change your scope declaration as like below

get "/", scope: :current_user do
...
end

And you should have current_user method declared in your helpers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants