jsonapi-rb / jsonapi-rails

Rails gem for fast jsonapi-compliant APIs.
http://jsonapi-rb.org
MIT License
319 stars 63 forks source link

passing current_user to Serializable class #89

Closed csaurav closed 6 years ago

csaurav commented 6 years ago

Hi,

I have to show some attribute, based on the user role and for this matter, I have to pass current_user object to the SerializableMyClass.

How to do it ?

Thanks

beauby commented 6 years ago

In your render call, you can specify an expose parameter, which allows you to expose arbitrary variables to serializable classes.

Example:

roles = { lucas: :admin }
render jsonapi: users, expose: { roles: roles }

Then you can refer to exposed parameters as instance variables in the serializable classes: e.g. @roles.