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/postgis_adapter.rb,
lib/apartment/adapters/sqlite3_adapter.rb,
lib/apartment/adapters/trilogy_adapter.rb,
lib/apartment/adapters/jdbc_mysql_adapter.rb,
lib/apartment/adapters/postgresql_adapter.rb,
lib/apartment/adapters/jdbc_postgresql_adapter.rb

Overview

JDBC helper to decide wether to use JDBC Postgresql Adapter or JDBC Postgresql Adapter with Schemas

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.



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

def config=(value)
  @config = value
end

Class Method Details

.jdbc_mysql_adapter(config) ⇒ Object



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

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

.jdbc_postgresql_adapter(config) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/apartment/adapters/jdbc_postgresql_adapter.rb', line 8

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

.mysql2_adapter(config) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/apartment/adapters/mysql2_adapter.rb', line 8

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

.postgis_adapter(config) ⇒ Object



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

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

.postgresql_adapter(config) ⇒ Object



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

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



7
8
9
# File 'lib/apartment/adapters/sqlite3_adapter.rb', line 7

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

.trilogy_adapter(config) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/apartment/adapters/trilogy_adapter.rb', line 8

def self.trilogy_adapter(config)
  if Apartment.use_schemas
    Adapters::TrilogySchemaAdapter.new(config)
  else
    Adapters::TrilogyAdapter.new(config)
  end
end

Instance Method Details

#adapterObject

Fetch the proper multi-tenant adapter based on Rails config

@return {subclass of Apartment::AbstractAdapter}


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/apartment/tenant.rb', line 21

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

    if defined?(JRUBY_VERSION)
      case config[:adapter]
      when /mysql/
        adapter_method = 'jdbc_mysql_adapter'
      when /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

#reload!(config = nil) ⇒ Object

Reset config and adapter so they are regenerated



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

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