Class: ActiveRecord::Tenanted::DatabaseConfigurations::BaseConfig

Inherits:
DatabaseConfigurations::HashConfig
  • Object
show all
Defined in:
lib/active_record/tenanted/database_configurations/base_config.rb

Constant Summary collapse

DEFAULT_MAX_CONNECTION_POOLS =
50

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBaseConfig

Returns a new instance of BaseConfig.



11
12
13
14
15
# File 'lib/active_record/tenanted/database_configurations/base_config.rb', line 11

def initialize(...)
  super
  @test_worker_id = nil
  @config_adapter = nil
end

Instance Attribute Details

#test_worker_idObject

Returns the value of attribute test_worker_id.



9
10
11
# File 'lib/active_record/tenanted/database_configurations/base_config.rb', line 9

def test_worker_id
  @test_worker_id
end

Instance Method Details

#config_adapterObject



17
18
19
# File 'lib/active_record/tenanted/database_configurations/base_config.rb', line 17

def config_adapter
  @config_adapter ||= ActiveRecord::Tenanted::DatabaseAdapter.new(self)
end

#database_for(tenant_name) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/active_record/tenanted/database_configurations/base_config.rb', line 25

def database_for(tenant_name)
  tenant_name = tenant_name.to_s

  config_adapter.validate_tenant_name(tenant_name)

  db = sprintf(database, tenant: tenant_name)

  if test_worker_id
    db = config_adapter.test_workerize(db, test_worker_id)
  end

  db
end

#database_tasks?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/active_record/tenanted/database_configurations/base_config.rb', line 21

def database_tasks?
  false
end

#max_connection_poolsObject



60
61
62
# File 'lib/active_record/tenanted/database_configurations/base_config.rb', line 60

def max_connection_pools
  (configuration_hash[:max_connection_pools] || DEFAULT_MAX_CONNECTION_POOLS).to_i
end

#new_connectionObject

Raises:



53
54
55
56
57
58
# File 'lib/active_record/tenanted/database_configurations/base_config.rb', line 53

def new_connection
  raise NoTenantError, "Cannot use an untenanted ActiveRecord::Base connection. " \
                       "If you have a model that inherits directly from ActiveRecord::Base, " \
                       "make sure to use 'subtenant_of'. In development, you may see this error " \
                       "if constant reloading is not being done properly."
end

#new_tenant_config(tenant_name) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/active_record/tenanted/database_configurations/base_config.rb', line 43

def new_tenant_config(tenant_name)
  config_name = "#{name}_#{tenant_name}"
  config_hash = configuration_hash.dup.tap do |hash|
    hash[:tenant] = tenant_name
    hash[:database] = database_for(tenant_name)
    hash[:tenanted_config_name] = name
  end
  Tenanted::DatabaseConfigurations::TenantConfig.new(env_name, config_name, config_hash)
end

#tenantsObject



39
40
41
# File 'lib/active_record/tenanted/database_configurations/base_config.rb', line 39

def tenants
  config_adapter.tenant_databases
end