Module: Apartment::Database

Extended by:
Database
Included in:
Database
Defined in:
lib/apartment/database.rb,
lib/apartment/adapters/mysql_adapter.rb,
lib/apartment/adapters/postgresql_adapter.rb

Overview

The main entry point to Apartment functions

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.mysql_adapter(config) ⇒ Object



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

def self.mysql_adapter(config)
  Adapters::MysqlAdapter.new config
end

.postgresql_adapter(config) ⇒ Object



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

def self.postgresql_adapter(config)
  Apartment.use_postgres_schemas ?
    Adapters::PostgresqlSchemaAdapter.new(config, :schema_search_path => ActiveRecord::Base.connection.schema_search_path) :
    Adapters::PostgresqlAdapter.new(config)
end

Instance Method Details

#adapterObject

Fetch the proper multi-tenant adapter based on Rails config

@return {subclass of Apartment::AbstractAdapter}


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/apartment/database.rb', line 22

def adapter
  @adapter ||= begin
    adapter_method = "#{config[:adapter]}_adapter"

    begin
      require "apartment/adapters/#{adapter_method}"
    rescue LoadError
      raise "The adapter `#{config[:adapter]}` 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



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

def init
  process_excluded_models
end

#reload!Object

Reset config and adapter so they are regenerated



42
43
44
45
# File 'lib/apartment/database.rb', line 42

def reload!
  @adapter = nil
  @config = nil
end