Module: Foreman::ThreadSession::OrganizationModel

Included in:
Organization
Defined in:
lib/foreman/thread_session.rb

Overview

include this in the Organization model object

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/foreman/thread_session.rb', line 79

def self.included(base)
  base.class_eval do
    def self.current
      Thread.current[:organization]
    end

    def self.current=(organization)
      unless organization.nil? || organization.is_a?(self) || organization.is_a?(Array)
        raise(ArgumentError, "Unable to set current organization, expected class '#{self}', got #{organization.inspect}")
      end

      Rails.logger.debug "Setting current organization thread-local variable to #{organization || "none"}"
      Thread.current[:organization] = organization
    end

    # Executes given block in the scope of an org:
    #
    # Organization.as_org organization do
    #   ...
    # end
    #
    # @param [org]
    # @param [block] block to execute
    def self.as_org org
      old_org = current
      self.current = org
      yield if block_given?
    ensure
      self.current = old_org
    end
  end
end