let's be current with Current attributes
In the first post of this series, I tried to show state of our code, and why we weren't happy with it.
In this part, I will start with the easiest change - introduction CurrentAttributes to our app.
After getting utterly confused and lost in the code I tried to outline for you, the first, mostly simple part, was getting rid of Thread.current
and using modern Current
(attributes API)[https://api.rubyonrails.org/classes/ActiveSupport/CurrentAttributes.html].
If you have never heard of it, this explains it:
Current Attributes
Abstract super class that provides a thread-isolated attributes singleton, which resets automatically before and after each request. This allows you to keep all the per-request attributes easily available to the whole system.
So, I looked for our usage of Thread (and our CurrentThread "wrapper"), and mostly just replaced it with the same using Current
.
Easy.
Sidenote: I will use
current_user
in remaining code, as we do in our app now. However, if your app usesCurrent.user
instead (as pure Rails 8+ apps might), it is approximately the same (not really, in that Current is global).
This was easy and short. Not so much for our next part!
Top comments (0)