DEV: Allow the default connection handler to be changed.

DEV: Allow the default connection handler to be changed.

Remove the need to establish connection for the default handler because ActiveRecord will always do so in an initializer.

diff --git a/lib/rails_multisite/connection_management.rb b/lib/rails_multisite/connection_management.rb
index 7b03be5..95c8135 100644
--- a/lib/rails_multisite/connection_management.rb
+++ b/lib/rails_multisite/connection_management.rb
@@ -137,7 +137,18 @@ module RailsMultisite
       :"#{spec.name}#{@handler_key_suffix}"
     end
 
+    def self.default_connection_handler=(connection_handler)
+      if @instance
+        unless connection_handler.is_a?(ActiveRecord::ConnectionAdapters::ConnectionHandler)
+          raise ArgumentError.new("Invalid connection handler")
+        end
+
+        @instance.default_connection_handler = connection_handler
+      end
+    end
+
     attr_reader :config_filename, :db_spec_cache, :connection_handlers
+    attr_writer :default_connection_handler
 
     def initialize(config_filename)
       @config_filename = config_filename
@@ -153,7 +164,6 @@ module RailsMultisite
       @db_spec_cache = {}
       @default_spec = SPEC_KLASS::Resolver.new(ActiveRecord::Base.configurations).spec(Rails.env.to_sym)
       @default_connection_handler = ActiveRecord::Base.connection_handler
-      @established_default = false
 
       @reload_mutex = Mutex.new
 
@@ -246,10 +256,6 @@ module RailsMultisite
         end
       else
         handler = @default_connection_handler
-        if !@established_default
-          handler_establish_connection(handler, spec)
-          @established_default = true
-        end
       end
 
       ActiveRecord::Base.connection_handler = handler
diff --git a/spec/connection_management_spec.rb b/spec/connection_management_spec.rb
index 02c38e2..c491b1b 100644
--- a/spec/connection_management_spec.rb
+++ b/spec/connection_management_spec.rb
@@ -240,4 +240,31 @@ describe RailsMultisite::ConnectionManagement do
     end
   end
 
+  describe '.default_connection_handler=' do
+    before do
+      conn.config_filename = fixture_path("two_dbs.yml")
+    end
+
+    it 'should raise the right error if attempting to assign an invalid argument' do
+      expect do
+        conn.default_connection_handler = 'test'
+      end.to raise_error(ArgumentError)
+    end
+
+    it 'should allow the default connection handler to be assigned' do
+      default_handler = ActiveRecord::Base.connection_handler
+      new_handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new
+
+      conn.default_connection_handler = new_handler
+      conn.establish_connection(db: described_class::DEFAULT)
+
+      expect(ActiveRecord::Base.connection_handler).to eq(new_handler)
+
+      conn.default_connection_handler = default_handler
+      conn.establish_connection(db: described_class::DEFAULT)
+
+      expect(ActiveRecord::Base.connection_handler).to eq(default_handler)
+    end
+  end
+
 end

GitHub sha: 174ed8bc

This commit appears in #16 which was approved by eviltrout. It was merged by tgxworld.