Skip to content

Commit

Permalink
Add callbacks on #create and #switch!
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed May 21, 2016
1 parent b805197 commit ff9c9d0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
25 changes: 16 additions & 9 deletions lib/apartment/adapters/abstract_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
module Apartment
module Adapters
class AbstractAdapter
include ActiveSupport::Callbacks
define_callbacks :create, :switch

attr_writer :default_tenant

# @constructor
Expand All @@ -17,15 +20,17 @@ def initialize(config)
# @param {String} tenant Tenant name
#
def create(tenant)
create_tenant(tenant)
run_callbacks :create do
create_tenant(tenant)

switch(tenant) do
import_database_schema
switch(tenant) do
import_database_schema

# Seed data if appropriate
seed_data if Apartment.seed_after_create
# Seed data if appropriate
seed_data if Apartment.seed_after_create

yield if block_given?
yield if block_given?
end
end
end

Expand Down Expand Up @@ -80,10 +85,12 @@ def drop(tenant)
# @param {String} tenant name
#
def switch!(tenant = nil)
return reset if tenant.nil?
run_callbacks :switch do
return reset if tenant.nil?

connect_to_new(tenant).tap do
Apartment.connection.clear_query_cache
connect_to_new(tenant).tap do
Apartment.connection.clear_query_cache
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/apartment/tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Tenant
extend self
extend Forwardable

def_delegators :adapter, :create, :drop, :switch, :switch!, :current, :each, :reset, :seed, :current_tenant, :default_tenant
def_delegators :adapter, :create, :drop, :switch, :switch!, :current, :each, :reset, :set_callback, :seed, :current_tenant, :default_tenant

attr_writer :config

Expand Down

0 comments on commit ff9c9d0

Please sign in to comment.