Module: Apartment::Tenant

Extended by:
Tenant, Forwardable
Included in:
Tenant
Defined in:
lib/apartment/tenant.rb,
lib/apartment/adapters/mysql2_adapter.rb,
lib/apartment/adapters/sqlite3_adapter.rb,
lib/apartment/adapters/postgis_adapter.rb,
lib/apartment/adapters/postgresql_adapter.rb,
lib/apartment/adapters/jdbc_mysql_adapter.rb,
lib/apartment/adapters/jdbc_postgresql_adapter.rb

Overview

The main entry point to Apartment functions

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#config=(value) ⇒ Object

Sets the attribute config

Parameters:

  • value

    the value to set the attribute config to.


14
15
16
# File 'lib/apartment/tenant.rb', line 14

def config=(value)
  @config = value
end

Class Method Details

.jdbc_mysql_adapter(config) ⇒ Object


6
7
8
# File 'lib/apartment/adapters/jdbc_mysql_adapter.rb', line 6

def self.jdbc_mysql_adapter(config)
  Adapters::JDBCMysqlAdapter.new config
end

.jdbc_postgresql_adapter(config) ⇒ Object


6
7
8
9
10
# File 'lib/apartment/adapters/jdbc_postgresql_adapter.rb', line 6

def self.jdbc_postgresql_adapter(config)
  Apartment.use_schemas ?
    Adapters::JDBCPostgresqlSchemaAdapter.new(config) :
    Adapters::JDBCPostgresqlAdapter.new(config)
end

.mysql2_adapter(config) ⇒ Object


6
7
8
9
10
# File 'lib/apartment/adapters/mysql2_adapter.rb', line 6

def self.mysql2_adapter(config)
  Apartment.use_schemas ?
    Adapters::Mysql2SchemaAdapter.new(config) :
    Adapters::Mysql2Adapter.new(config)
end

.postgis_adapter(config) ⇒ Object


8
9
10
# File 'lib/apartment/adapters/postgis_adapter.rb', line 8

def self.postgis_adapter(config)
  postgresql_adapter(config)
end

.postgresql_adapter(config) ⇒ Object


6
7
8
9
10
11
# File 'lib/apartment/adapters/postgresql_adapter.rb', line 6

def self.postgresql_adapter(config)
  adapter = Adapters::PostgresqlAdapter
  adapter = Adapters::PostgresqlSchemaAdapter if Apartment.use_schemas
  adapter = Adapters::PostgresqlSchemaFromSqlAdapter if Apartment.use_sql && Apartment.use_schemas
  adapter.new(config)
end

.sqlite3_adapter(config) ⇒ Object


5
6
7
# File 'lib/apartment/adapters/sqlite3_adapter.rb', line 5

def self.sqlite3_adapter(config)
  Adapters::Sqlite3Adapter.new(config)
end

Instance Method Details

#adapterObject

Fetch the proper multi-tenant adapter based on Rails config

@return {subclass of Apartment::AbstractAdapter}

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/apartment/tenant.rb', line 26

def adapter
  Thread.current[:apartment_adapter] ||= begin
    adapter_method = "#{config[:adapter]}_adapter"

    if defined?(JRUBY_VERSION)
      if config[:adapter] =~ /mysql/
        adapter_method = 'jdbc_mysql_adapter'
      elsif config[:adapter] =~ /postgresql/
        adapter_method = 'jdbc_postgresql_adapter'
      end
    end

    begin
      require "apartment/adapters/#{adapter_method}"
    rescue LoadError
      raise "The adapter `#{adapter_method}` is not yet supported"
    end

    unless respond_to?(adapter_method)
      raise AdapterNotFound, "database configuration specifies nonexistent #{config[:adapter]} adapter"
    end

    send(adapter_method, config)
  end
end

#initObject

Initialize Apartment config options such as excluded_models


18
19
20
# File 'lib/apartment/tenant.rb', line 18

def init
  process_excluded_models
end

#reload!(config = nil) ⇒ Object

Reset config and adapter so they are regenerated


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

def reload!(config = nil)
  Thread.current[:apartment_adapter] = nil
  @config = config
end