I do not have much idea about Raven
, but below is a way, using which, we access the current user in a request, all over our application.
We have created a class, which acts as a cache, and inserts/retrieves data from the current thread
class CustomCache
def self.namespace
"my_application"
end
def self.get(res)
Thread.current[self.namespace] ||= {}
val = Thread.current[self.namespace][res]
if val.nil? and block_given?
val = yield
self.set(res, val) unless val.nil?
end
return val
end
def self.set(key, value)
Thread.current[self.namespace][key] = value
end
def self.reset
Thread.current[self.namespace] = {}
end
end
And then, when the request is received, a check for the current session is performed, and then the user's model is inserted in the cache as below
def current_user
if defined?(@current_user)
return @current_user
end
@current_user = current_user_session && current_user_session.record
CustomCache.set(:current_user, @current_user)
return @current_user
end
Now, you can retrieve the current user from anywhere in your application, using the code below,
CustomCache.get(:current_user)
We also make sure to reset the cache before and after the request has been served, so we do this,
CustomCache.reset
Hope this helps.