DEV Community

Seiei Miyagi
Seiei Miyagi

Posted on

How to define almost global variable and almost top level method in Rails

# app/models/application_container.rb
require 'singleton'

class ApplicationContainer
  include Singleton
end
# app/controllers/application_controler.rb
class ApplicationController < ActionController::Base
  delegate_missing_to :container

  cattr_accessor :container, default: ApplicationContainer.instance
end
# app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true

  delegate_missing_to :container

  cattr_accessor :container, default: ApplicationContainer.instance
end

Top comments (0)