Managing current user in Rails, our way - part 2

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyrinNew
    Senior Member
    • Feb 2024
    • 5168

    #1

    Managing current user in Rails, our way - part 2

    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/...ttributes.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, in many cases, it is equivalent to Current.user, which holds the same value


    This was easy and short. Not so much for our next part!




    More...
Working...