DEV Community

loading...

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

Seiei Miyagi
こんにちは
・1 min read
# 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

Discussion (0)