Sha256: 57a3d066b1096661c39b455989d7177a3b479e178d1227a6a2e08a6350bbcb1b
Contents?: true
Size: 1.05 KB
Versions: 2
Compression:
Stored size: 1.05 KB
Contents
require 'request_store' module MultiTenant @@tenant_klass = nil def self.set_tenant_klass(klass) @@tenant_klass = klass end def self.tenant_klass @@tenant_klass end def self.partition_key "#{@@tenant_klass.to_s}_id" end def self.current_tenant=(tenant) RequestStore.store[:current_tenant] = tenant end def self.current_tenant RequestStore.store[:current_tenant] end def self.current_tenant_id current_tenant.try(:id) end def self.with(tenant, &block) old_tenant = self.current_tenant self.current_tenant = tenant value = block.call return value ensure self.current_tenant = old_tenant end def self.with_id(tenant_id, &block) if MultiTenant.current_tenant_id == tenant_id block.call else MultiTenant.with(TenantIdWrapper.new(id: tenant_id), &block) end end class TenantIsImmutable < StandardError end class TenantIdWrapper attr_reader :id def initialize(id:) @id = id end def new_record?; true; end def touch; nil; end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
activerecord-multi-tenant-0.3.1 | lib/activerecord-multi-tenant/multi_tenant.rb |
activerecord-multi-tenant-0.3.0 | lib/activerecord-multi-tenant/multi_tenant.rb |