I donβt have a big idea about Raven , but below is the way that we refer to the current user in a request throughout our application.
We created a class that acts like 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 is performed for the current session, and then the user model is inserted into the cache, as shown 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 get the current user from anywhere in your application using the following code:
CustomCache.get(:current_user)
We also check the reset cache before and after the request, so we do this,
CustomCache.reset
Hope this helps.
Rohit
source share