Active_model_serializers: How i can pass current_user now to serializer?

Created on 1 Sep 2014  路  15Comments  路  Source: rails-api/active_model_serializers

What happens with scope?

undefined local variable or methodcurrent_user' for`

0.9.x

Most helpful comment

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

All 15 comments

same question.

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

Which version are you all using?

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

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

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.

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

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

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

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)

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

@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

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

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.

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

Was this page helpful?
0 / 5 - 0 ratings