Sha256: b22d1b1298efa6835de505aa97cdd4a5ffe6a51c1d064ff2b674cfe57ee06d0a

Contents?: true

Size: 794 Bytes

Versions: 3

Compression:

Stored size: 794 Bytes

Contents

module Tenancy
  module Resource
    extend ActiveSupport::Concern

    module ClassMethods

      def current=(value)
        tenant = case value
        when self
          value
        when nil
          nil
        else
          find(value)
        end

        RequestStore.store[:"#{name}.current"] = tenant
      end

      def current
        RequestStore.store[:"#{name}.current"]
      end

      def current_id
        current.try(:id)
      end

      def with_tenant(tenant, &block)
        raise ArgumentError, "block required" if block.nil?

        begin
          old          = self.current
          self.current = tenant

          block.call
        ensure
          self.current = old
        end
      end
      alias_method :use_tenant, :with_tenant
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tenancy-1.0.2 lib/tenancy/resource.rb
tenancy-1.0.1 lib/tenancy/resource.rb
tenancy-1.0.0 lib/tenancy/resource.rb