Skip to content

Rodrigo Urubatan – About Code

Helping ruby developers to use the best tools for each job so they can solve hard problems, with less bugs and have more free time.

Menu
  • Home
  • My last presentations
  • About
  • Privacy Policy
Menu

How to create a request scope for your rails application

Posted on 2019-06-06 by Rodrigo Urubatan

Some time ago I missed one of the cool features from the time I worked with JavaEE, that was a request scope for some variables.

I know the instance variables on controllers do a great job simulating it, but I needed to access context in a completely different part of the application, that wasn’t always called from controllers, variables that should be scoped to that request.

So I thought, I could create something like that for my rails app.

To start, I created a file called ‘request_scope.rb’ in the lib folder, with something similar to this (I’ll not post the actual file here because it is from a client’s project)

class RequestScope
  def self.start_scope  
        raise "request_context already set" if Thread.current[:request_context]
    Thread.current[:request_context] = {}
  end
 
  def self.end_scope
    raise "request_context already nil" unless Thread.current[:request_context]
    Thread.current[:request_context] = nil
  end 
 
  def self.scoped_hash
    raise "request_context not initialized" unless Thread.current[:request_context]
    Thread.current[:request_context]
  end
 
  def self.[](name)
    self.scoped_hash[name]
  end
 
  def self.[]=(name,val)
    self.scoped_hash[name] = val
  end
end

That will allow me to do things like:

RequestScope['alpha'] = 1
RequestScope['alpha']

And for that to work, as a real request scope, I just needed to add one bit of code to the application_controller.rb of my application:

class ApplicationController < ActionController::Base
  around_action :define_request_scope
 
  def define_request_scope
    EnvConfig.start_scope
    begin
      yield
    ensure
      EnvConfig.end_scope
    end
  end
end

There is one trick thou, if you need to read or write to the request context in any other filters, remember that they need to be defined after the “define_context_scope”, because the filters are executed in the order they are defined.

So, what do you think about this approach to create a request scope? would you do different? do you see any problems in this implementation?

2 thoughts on “How to create a request scope for your rails application”

  1. Brandon Zylstra says:
    2019-11-07 at 06:04

    Can you explain the problem this solves? What does this allow you to do now?

    Reply
    1. urubatan says:
      2019-12-13 at 19:44

      The problem I was solving with this, was to access in an easy way, information related to the current user request in “commands” implemented under the lib directory and in models, the code didn’t belong in controllers…

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent posts

  • I see Dead Jobs everywhere (sidekiq DeadSet)
  • Quick tips that help: rails notes
  • Ruby 3.2.0 released with WASI, YJIT, RegExp improvements, …
  • Rubyconf Thailand quick summary and slides
  • Testing download contents with Cucumber+Capybara and chromedriver

Comments

  1. When Kubernetes is not the right choice? | Rodrigo Urubatan - About Code on Rails from “zero” to kubernetes – first pod
  2. When Kubernetes is not the right choice? | Rodrigo Urubatan - About Code on How to use docker to have an uniform development environment for your rails project
  3. Rails from "zero" to kubernetes – ingress reverse proxy | Rodrigo Urubatan - About Code on Rails from “zero” to kubernetes – a service to access your pod
  4. Rails from "zero" to kubernetes – horizontal autoscaling | Rodrigo Urubatan - About Code on Rails from “zero” to kubernetes – a service to access your pod
  5. Jeronimo on 6 Lessons From CrossFit That will help your developer career (Or any other career in the matter of fact)

Arquives

  • February 2023
  • January 2023
  • December 2022
  • June 2021
  • March 2020
  • January 2020
  • July 2019
  • June 2019
  • May 2019
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • February 2018
  • January 2018
  • November 2017
  • August 2015
  • August 2014
  • July 2014
  • August 2007

Categories

  • articles
  • cfp
  • firebase
  • gems
  • git
  • opinion
  • presentations
  • projects
  • rails6
  • ruby
  • Sem categoria
  • server-api
  • Uncategorized
© 2023 Rodrigo Urubatan – About Code | Powered by Minimalist Blog WordPress Theme