Module: Multitenancy

Defined in:
lib/multitenancy.rb,
lib/multitenancy/tenant.rb,
lib/multitenancy/version.rb,
lib/multitenancy/rack/filter.rb,
lib/multitenancy/model_extensions.rb

Defined Under Namespace

Modules: ModelExtensions Classes: Filter, Tenant

Constant Summary collapse

VERSION =
"0.0.5"
@@tenant_header =
'X_TENANT_ID'
@@sub_tenant_header =
'X_SUB_TENANT_ID'
@@append_headers_to_rest_calls =
true
@@logger =
(logger rescue nil) || Logger.new(STDOUT)
@@db_config_prefix =
''
@@db_config_suffix =
'_development'
@@db_type =

or :dedicated

:shared

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.sub_tenant_fieldObject

Returns the value of attribute sub_tenant_field.



4
5
6
# File 'lib/multitenancy/model_extensions.rb', line 4

def sub_tenant_field
  @sub_tenant_field
end

.tenant_fieldObject

Returns the value of attribute tenant_field.



4
5
6
# File 'lib/multitenancy/model_extensions.rb', line 4

def tenant_field
  @tenant_field
end

Class Method Details

.append_headers_to_rest_calls?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/multitenancy.rb', line 47

def append_headers_to_rest_calls?
  @@append_headers_to_rest_calls
end

.current_tenantObject



74
75
76
# File 'lib/multitenancy.rb', line 74

def current_tenant
  Thread.current[:tenant]
end

.current_tenant=(tenant) ⇒ Object



69
70
71
72
# File 'lib/multitenancy.rb', line 69

def current_tenant=(tenant)
  self.logger.debug "Setting the current tenant to - #{tenant}"
  Thread.current[:tenant] = tenant
end

.db_typeObject



31
32
33
# File 'lib/multitenancy.rb', line 31

def db_type
  @@db_type
end

.init(config) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/multitenancy.rb', line 21

def init(config)
  @@tenant_header = config[:tenant_header]
  @@sub_tenant_header = config[:sub_tenant_header]
  @@logger = config[:logger] if config[:logger]
  @@append_headers_to_rest_calls = config[:append_headers_to_rest_calls] unless config[:append_headers_to_rest_calls].nil?
  @@db_config_prefix = config[:db_config_prefix] unless config[:db_config_prefix].nil?
  @@db_config_suffix = config[:db_config_suffix] unless config[:db_config_suffix].nil?
  @@db_type = (config[:db_type].nil? || ![:shared, :dedicated].include?(config[:db_type])) ? :shared : config[:db_type] 
end

.loggerObject



35
36
37
# File 'lib/multitenancy.rb', line 35

def logger
  @@logger
end

.sub_tenant_headerObject



43
44
45
# File 'lib/multitenancy.rb', line 43

def sub_tenant_header
  @@sub_tenant_header
end

.tenant_headerObject



39
40
41
# File 'lib/multitenancy.rb', line 39

def tenant_header
  @@tenant_header
end

.with_tenant(tenant, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/multitenancy.rb', line 51

def with_tenant(tenant, &block)
  self.logger.debug "Executing the block with the tenant - #{tenant}"
  if block.nil?
    raise ArgumentError, "block required"
  end
  old_tenant = self.current_tenant
  self.current_tenant = tenant
  begin
    if db_type == :shared
      return block.call
    else
      return ActiveRecord::Base.switch_db("#{@@db_config_prefix}#{tenant.tenant_id}#{@@db_config_suffix}".to_sym, &block)
    end
  ensure
    self.current_tenant = old_tenant
  end
end