From 08c36d33e4bb40cc6a1572cc6849b7909c9d95d0 Mon Sep 17 00:00:00 2001 From: Nat Budin Date: Mon, 10 Oct 2011 13:07:10 -0400 Subject: [PATCH] Use connection pool instead of actual connection to avoid having to connect to the database until necessary --- lib/activerecord-import.rb | 8 ++++---- lib/activerecord-import/base.rb | 8 ++------ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/lib/activerecord-import.rb b/lib/activerecord-import.rb index be565c4..7f114d2 100644 --- a/lib/activerecord-import.rb +++ b/lib/activerecord-import.rb @@ -2,15 +2,15 @@ class ActiveRecord::Base class << self def establish_connection_with_activerecord_import(*args) establish_connection_without_activerecord_import(*args) - ActiveSupport.run_load_hooks(:active_record_connection_established, connection) + ActiveSupport.run_load_hooks(:active_record_connection_established, connection_pool) end alias_method_chain :establish_connection, :activerecord_import end end -ActiveSupport.on_load(:active_record_connection_established) do |connection| - if !ActiveRecord.const_defined?(:Import) || !ActiveRecord::Import.respond_to?(:load_from_connection) +ActiveSupport.on_load(:active_record_connection_established) do |connection_pool| + if !ActiveRecord.const_defined?(:Import) || !ActiveRecord::Import.respond_to?(:load_from_connection_pool) require File.join File.dirname(__FILE__), "activerecord-import/base" end - ActiveRecord::Import.load_from_connection connection + ActiveRecord::Import.load_from_connection_pool connection_pool end diff --git a/lib/activerecord-import/base.rb b/lib/activerecord-import/base.rb index 6c5e353..8aeb27c 100644 --- a/lib/activerecord-import/base.rb +++ b/lib/activerecord-import/base.rb @@ -12,12 +12,8 @@ module ActiveRecord::Import end # Loads the import functionality for the passed in ActiveRecord connection - def self.load_from_connection(connection) - import_adapter = "ActiveRecord::Import::#{connection.class.name.demodulize}::InstanceMethods" - unless connection.class.ancestors.map(&:name).include?(import_adapter) - config = connection.instance_variable_get :@config - require_adapter config[:adapter] - end + def self.load_from_connection_pool(connection_pool) + require_adapter connection_pool.spec.config[:adapter] end end