-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
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 |
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
this seemed a better idea because it doesn't alter the code being tested. |
Thanks @jacortinas for example! |
Might want to add serialization_scope to the README. very helpful. |
@toobulkeh just checked the README, it's already here. |
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 |
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| |
How would we mock this in an RSpec test for Serializers?
this includes the scope, but RSpec would not recognize the |
@peyterkim you need to mock/set |
What happens with scope?
undefined local variable or method
current_user' for`The text was updated successfully, but these errors were encountered: