Module: Apartment

Extended by:
Forwardable
Defined in:
lib/apartment/tasks/enhancements.rb,
lib/apartment.rb,
lib/apartment/tenant.rb,
lib/apartment/version.rb,
lib/apartment/railtie.rb,
lib/apartment/reloader.rb,
lib/apartment/migrator.rb,
lib/apartment/deprecation.rb,
lib/apartment/elevators/domain.rb,
lib/apartment/elevators/generic.rb,
lib/apartment/elevators/subdomain.rb,
lib/apartment/elevators/host_hash.rb,
lib/apartment/adapters/mysql2_adapter.rb,
lib/apartment/adapters/sqlite3_adapter.rb,
lib/apartment/adapters/postgis_adapter.rb,
lib/apartment/adapters/abstract_adapter.rb,
lib/apartment/elevators/first_subdomain.rb,
lib/apartment/adapters/postgresql_adapter.rb,
lib/apartment/adapters/jdbc_mysql_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]
WRITER_METHODS =
[:tenant_names, :database_schema_file, :excluded_models, :default_schema, :persistent_schemas, :connection_class, :tld_length, :db_migrate_tenants]
ApartmentError =

Exceptions

Class.new(StandardError)
AdapterNotFound =

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

Class.new(ApartmentError)
TenantNotFound =

Tenant specified is unknown

Class.new(ApartmentError)
TenantExists =

The Tenant attempting to be created already exists

Class.new(ApartmentError)
VERSION =
"1.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


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

def configure
  yield self if block_given?
end

.connection_classObject


55
56
57
# File 'lib/apartment.rb', line 55

def connection_class
  @connection_class || ActiveRecord::Base
end

.const_missing(const_name) ⇒ Object


68
69
70
71
72
73
74
75
# File 'lib/apartment/tenant.rb', line 68

def self.const_missing(const_name)
  if const_name == :Database
    Apartment::Deprecation.warn "`Apartment::Database` has been deprecated. Use `Apartment::Tenant` instead."
    Tenant
  else
    super
  end
end

.database_namesObject


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

def database_names
  Apartment::Deprecation.warn "[Deprecation Warning] `database_names` is now deprecated, please use `tenant_names`"
  tenant_names
end

.database_names=(names) ⇒ Object


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

def database_names=(names)
  Apartment::Deprecation.warn "[Deprecation Warning] `database_names=` is now deprecated, please use `tenant_names=`"
  self.tenant_names=(names)
end

.database_schema_fileObject


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

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

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

.db_migrate_tenantsObject

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


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

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


45
46
47
# File 'lib/apartment.rb', line 45

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

.excluded_modelsObject

Default to empty array


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

def excluded_models
  @excluded_models || []
end

.persistent_schemasObject


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

def persistent_schemas
  @persistent_schemas || []
end

.resetObject

Reset all the config for Apartment


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

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

.tenant_namesObject

Be careful not to use `return` here so both Proc and lambda can be used without breaking


28
29
30
# File 'lib/apartment.rb', line 28

def tenant_names
  @tenant_names.respond_to?(:call) ? @tenant_names.call : @tenant_names
end

.tld_lengthObject


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

def tld_length
  @tld_length || 1
end

.use_postgres_schemasObject


84
85
86
87
# File 'lib/apartment.rb', line 84

def use_postgres_schemas
  Apartment::Deprecation.warn "[Deprecation Warning] `use_postgresql_schemas` is now deprecated, please use `use_schemas`"
  use_schemas
end

.use_postgres_schemas=(to_use_or_not_to_use) ⇒ Object


89
90
91
92
# File 'lib/apartment.rb', line 89

def use_postgres_schemas=(to_use_or_not_to_use)
  Apartment::Deprecation.warn "[Deprecation Warning] `use_postgresql_schemas=` is now deprecated, please use `use_schemas=`"
  self.use_schemas = to_use_or_not_to_use
end