Module: Apartment

Extended by:
Forwardable
Defined in:
lib/apartment/tasks/enhancements.rb,
lib/apartment.rb,
lib/apartment/tenant.rb,
lib/apartment/railtie.rb,
lib/apartment/version.rb,
lib/apartment/migrator.rb,
lib/apartment/reloader.rb,
lib/apartment/deprecation.rb,
lib/apartment/elevators/domain.rb,
lib/apartment/elevators/generic.rb,
lib/apartment/elevators/host_hash.rb,
lib/apartment/elevators/subdomain.rb,
lib/apartment/adapters/mysql2_adapter.rb,
lib/apartment/adapters/postgis_adapter.rb,
lib/apartment/adapters/sqlite3_adapter.rb,
lib/apartment/adapters/abstract_adapter.rb,
lib/apartment/elevators/first_subdomain.rb,
lib/apartment/adapters/jdbc_mysql_adapter.rb,
lib/apartment/adapters/postgresql_adapter.rb,
lib/apartment/adapters/abstract_jdbc_adapter.rb,
lib/apartment/adapters/jdbc_postgresql_adapter.rb,
lib/generators/apartment/install/install_generator.rb

Overview

Require this file to append Apartment rake tasks to ActiveRecord db rake tasks Enabled by default in the initializer

Defined Under Namespace

Modules: Adapters, Deprecation, Elevators, Migrator, Tenant Classes: InstallGenerator, Railtie, RakeTaskEnhancer, Reloader

Constant Summary collapse

ACCESSOR_METHODS =
[:use_schemas, :use_sql, :seed_after_create, :prepend_environment, :append_environment, :with_multi_server_setup ]
WRITER_METHODS =
[:tenant_names, :database_schema_file, :excluded_models, :default_schema, :persistent_schemas, :connection_class, :tld_length, :db_migrate_tenants, :seed_data_file]
ApartmentError =

Exceptions

Class.new(StandardError)
AdapterNotFound =

Raised when apartment cannot find the adapter specified in config/database.yml

Class.new(ApartmentError)
FileNotFound =

Raised when apartment cannot find the file to be loaded

Class.new(ApartmentError)
TenantNotFound =

Tenant specified is unknown

Class.new(ApartmentError)
TenantExists =

The Tenant attempting to be created already exists

Class.new(ApartmentError)
VERSION =
"2.0.0"

Class Method Summary collapse

Class Method Details

.configure {|_self| ... } ⇒ Object

configure apartment with available options

Yields:

  • (_self)

Yield Parameters:

  • _self (Apartment)

    the object that the method was called on



22
23
24
# File 'lib/apartment.rb', line 22

def configure
  yield self if block_given?
end

.connection_classObject



61
62
63
# File 'lib/apartment.rb', line 61

def connection_class
  @connection_class || ActiveRecord::Base
end

.database_schema_fileObject



65
66
67
68
69
# File 'lib/apartment.rb', line 65

def database_schema_file
  return @database_schema_file if defined?(@database_schema_file)

  @database_schema_file = Rails.root.join('db', 'schema.rb')
end

.db_config_for(tenant) ⇒ Object



34
35
36
# File 'lib/apartment.rb', line 34

def db_config_for(tenant)
  (tenants_with_config[tenant] || connection_config).with_indifferent_access
end

.db_migrate_tenantsObject

Whether or not db:migrate should also migrate tenants defaults to true



40
41
42
43
44
# File 'lib/apartment.rb', line 40

def db_migrate_tenants
  return @db_migrate_tenants if defined?(@db_migrate_tenants)

  @db_migrate_tenants = true
end

.default_schemaObject Also known as: default_tenant



51
52
53
# File 'lib/apartment.rb', line 51

def default_schema
  @default_schema || "public" # TODO 'public' is postgres specific
end

.excluded_modelsObject

Default to empty array



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

def excluded_models
  @excluded_models || []
end

.extract_tenant_configObject



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/apartment.rb', line 82

def extract_tenant_config
  return {} unless @tenant_names
  values = @tenant_names.respond_to?(:call) ? @tenant_names.call : @tenant_names
  unless values.is_a? Hash
    values = values.each_with_object({}) do |tenant, hash|
      hash[tenant] = connection_config
    end
  end
  values.with_indifferent_access
rescue ActiveRecord::StatementInvalid
  {}
end

.persistent_schemasObject



57
58
59
# File 'lib/apartment.rb', line 57

def persistent_schemas
  @persistent_schemas || []
end

.resetObject

Reset all the config for Apartment



78
79
80
# File 'lib/apartment.rb', line 78

def reset
  (ACCESSOR_METHODS + WRITER_METHODS).each{|method| remove_instance_variable(:"@#{method}") if instance_variable_defined?(:"@#{method}") }
end

.seed_data_fileObject



71
72
73
74
75
# File 'lib/apartment.rb', line 71

def seed_data_file
  return @seed_data_file if defined?(@seed_data_file)

  @seed_data_file = "#{Rails.root}/db/seeds.rb"
end

.tenant_namesObject



26
27
28
# File 'lib/apartment.rb', line 26

def tenant_names
  extract_tenant_config.keys.map(&:to_s)
end

.tenants_with_configObject



30
31
32
# File 'lib/apartment.rb', line 30

def tenants_with_config
  extract_tenant_config
end