Method: Multitenancy.with_tenant

Defined in:
lib/multitenancy.rb

.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