Module: MultiTenant

Defined in:
lib/activerecord-multi-tenant/version.rb,
lib/activerecord-multi-tenant/migrations.rb,
lib/activerecord-multi-tenant/multi_tenant.rb,
lib/activerecord-multi-tenant/copy_from_client.rb,
lib/activerecord-multi-tenant/model_extensions.rb,
lib/activerecord-multi-tenant/controller_extensions.rb

Defined Under Namespace

Modules: ControllerExtensions, CopyFromClient, MigrationExtensions, ModelExtensionsClassMethods Classes: CopyFromClientHelper, TenantIdWrapper, TenantIsImmutable

Constant Summary collapse

VERSION =
'0.3.4'
@@tenant_klass =
nil
@@enable_with_lock_workaround =

Workaroud to make “with_lock” work until github.com/citusdata/citus/issues/1236 is fixed

false

Class Method Summary collapse

Class Method Details

.current_tenantObject



27
28
29
# File 'lib/activerecord-multi-tenant/multi_tenant.rb', line 27

def self.current_tenant
  RequestStore.store[:current_tenant]
end

.current_tenant=(tenant) ⇒ Object



23
24
25
# File 'lib/activerecord-multi-tenant/multi_tenant.rb', line 23

def self.current_tenant=(tenant)
  RequestStore.store[:current_tenant] = tenant
end

.current_tenant_idObject



35
36
37
# File 'lib/activerecord-multi-tenant/multi_tenant.rb', line 35

def self.current_tenant_id
  current_tenant.try(:id)
end

.current_tenant_id=(tenant_id) ⇒ Object



31
32
33
# File 'lib/activerecord-multi-tenant/multi_tenant.rb', line 31

def self.current_tenant_id=(tenant_id)
  self.current_tenant = TenantIdWrapper.new(id: tenant_id)
end

.enable_with_lock_workaroundObject



20
# File 'lib/activerecord-multi-tenant/multi_tenant.rb', line 20

def self.enable_with_lock_workaround; @@enable_with_lock_workaround = true; end

.partition_keyObject



14
15
16
# File 'lib/activerecord-multi-tenant/multi_tenant.rb', line 14

def self.partition_key
  "#{@@tenant_klass.to_s}_id"
end

.set_tenant_klass(klass) ⇒ Object



6
7
8
# File 'lib/activerecord-multi-tenant/multi_tenant.rb', line 6

def self.set_tenant_klass(klass)
  @@tenant_klass = klass
end

.tenant_klassObject



10
11
12
# File 'lib/activerecord-multi-tenant/multi_tenant.rb', line 10

def self.tenant_klass
  @@tenant_klass
end

.with(tenant, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/activerecord-multi-tenant/multi_tenant.rb', line 39

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

.with_id(tenant_id, &block) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/activerecord-multi-tenant/multi_tenant.rb', line 49

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

.with_lock_workaround_enabled?Boolean

Returns:

  • (Boolean)


21
# File 'lib/activerecord-multi-tenant/multi_tenant.rb', line 21

def self.with_lock_workaround_enabled?; @@enable_with_lock_workaround; end