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

How i can pass current_user now to serializer? #624

Closed
OpakAlex opened this issue Sep 1, 2014 · 15 comments
Closed

How i can pass current_user now to serializer? #624

OpakAlex opened this issue Sep 1, 2014 · 15 comments
Labels

Comments

@OpakAlex
Copy link

OpakAlex commented Sep 1, 2014

What happens with scope?

undefined local variable or methodcurrent_user' for`

@baash05
Copy link

baash05 commented Sep 3, 2014

same question.

@jasdeepsingh
Copy link

same question here, looks like the gem was re-written or atleast some features were removed, any thoughts anyone?

@steveklabnik
Copy link
Contributor

Which version are you all using?

@jasdeepsingh
Copy link

@steveklabnik i was using 0.9.0 and just switched back to 0.8.2 where current_user is accessible.

@jasdeepsingh
Copy link

After looking through the code, It looks like the feature is there, however the API has changed.. here's how I got around this issue:

Would be happy to open a PR updating the documentation:

class ApplicationController < ActionController::Base
   serialization_scope :view_context
end

then in your serializers:

class PostSerializer < ActiveModel::Serializer

  attributes :id, :body, :can_edit

  def can_edit
    true if scope.current_user.can_edit?(object)
  end

end

@baash05
Copy link

baash05 commented Sep 10, 2014

Here's how I skirted it

 def slizer(user)
     serializer = AccountSerializer.new(user, {scope: current_user} )
     def serializer.current_user() scope end
     serializer
 end

this seemed a better idea because it doesn't alter the code being tested.

@OpakAlex
Copy link
Author

OpakAlex commented Oct 2, 2014

Thanks @jacortinas for example!
Thanks @steveklabnik for tag label!

@toobulkeh
Copy link

Might want to add serialization_scope to the README. very helpful.

@joaomdmoura
Copy link
Member

@toobulkeh just checked the README, it's already here.
I'm closing this one

@petermumford
Copy link

For me I had to include the view_context to the ArraySerializer please see my comment on this issue: #510 (sorry don't know how to link to a comment on the issue)

@linjunpop
Copy link

@joaomdmoura The README on master branch does not have this section. Maybe add one?

@joaomdmoura
Copy link
Member

@linjunpop Indeed, it would be great to add it, but maybe to our new docs instead. I'm sure if the behavior still exactly the same but serialization_scope_name is implemented on 0.10.x

@danielfahy
Copy link

I'm passing information to the front end using the gon gem, as we are slowly moving towards react.

In my application controller I call the UserSerializer manually if the user visits the /users endpoint

gon.users = current_account.users.map do |user|
UserSerializer.new(user, {scope: current_user}).attributes
end

@pdurkim
Copy link

pdurkim commented Apr 19, 2017

How would we mock this in an RSpec test for Serializers?

allow_any_instance_of(Serializer).to receive(:scope).and_return(BaseController.new)

this includes the scope, but RSpec would not recognize the current_user method on the scope.

@mapreal19
Copy link

@peyterkim you need to mock/set scope_name as :current_user

@rails-api rails-api locked as resolved and limited conversation to collaborators Jun 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests