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

Scope is not passed through the custom attributes #937

Closed
sungwoncho opened this issue Jun 3, 2015 · 5 comments
Closed

Scope is not passed through the custom attributes #937

sungwoncho opened this issue Jun 3, 2015 · 5 comments
Assignees

Comments

@sungwoncho
Copy link

Scope is nil when serializer is called within a custom attribute of another serializer.

Contrived example:

class ApplicationController < ActionController::Base
  serialization_scope :view_context
end
class UserSerializer < ActiveModel::Serializer
  attributes :can_vote

  def can_vote
    VotePolicy.allowed?(user) if object == scope.current_user
  end
end

Above works fine. However, scope is nil when I use UserSerializer in a custom attribute of another serializer:

class CourseSerializer < ActiveModel::Serializer
  attributes :student

  def student
    UserSerializer(object.enrollment.student)
  end
end

Update

After reading the code, I realize that I can pass in scope when initializing the Serializer.

What are your thoughts on passing the scope by default, if one is defined?

@joaomdmoura
Copy link
Member

@sungwoncho just checkin, are you using 0.10? from master or rc1?

@bf4
Copy link
Member

bf4 commented Jun 11, 2015

Also, is there any reason you can't use belongs_to :student or some such so that it will just work? I know has_one was removed.

@joaomdmoura joaomdmoura self-assigned this Jun 12, 2015
@bf4
Copy link
Member

bf4 commented Jun 21, 2015

Propose closing since OP hasn't responded with version. (Though I correct myself, has_one was removed, but has since been re-added)

@sungwoncho
Copy link
Author

@bf4 I am using 0.9.3.I am no longer working on this project so I am not sure if upgrading the gem would have fixed the issue.

@joaomdmoura
Copy link
Member

belongs_to: student as @bf4 pointed out seems the way to go for me.
IMHO passing an scope by default is unnecessary. 😄
It indeed won't work, but it seems just an edge-case for me that could be handled with the belongs_to, so I'm closing this for now.

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

No branches or pull requests

3 participants