Updated how activerecord-import loads so it is done after a connection is established in ActiveRecord. This supports loading import for multiple adapters automatically w/o having to have a person require those specifically.

This commit is contained in:
Zach Dennis 2010-09-26 20:47:27 -04:00
parent 6c6a0d22b9
commit cb542a86f4
3 changed files with 27 additions and 8 deletions

View file

@ -1,2 +1,16 @@
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)
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)
require File.join File.dirname(__FILE__), "activerecord-import/base" require File.join File.dirname(__FILE__), "activerecord-import/base"
ActiveRecord::Import.load end
ActiveRecord::Import.load_from_connection connection
end

View file

@ -11,12 +11,16 @@ module ActiveRecord::Import
require File.join(AdapterPath,"/#{adapter}_adapter") require File.join(AdapterPath,"/#{adapter}_adapter")
end end
# Loads the import functionality for the current ActiveRecord::Base.connection # Loads the import functionality for the passed in ActiveRecord connection
def self.load def self.load_from_connection(connection)
config = ActiveRecord::Base.connection.instance_variable_get :@config 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] require_adapter config[:adapter]
end end
end end
end
this_dir = Pathname.new File.dirname(__FILE__) this_dir = Pathname.new File.dirname(__FILE__)
require this_dir.join("import") require this_dir.join("import")

View file

@ -28,10 +28,11 @@ FileUtils.mkdir_p 'log'
ActiveRecord::Base.logger = Logger.new("log/test.log") ActiveRecord::Base.logger = Logger.new("log/test.log")
ActiveRecord::Base.logger.level = Logger::DEBUG ActiveRecord::Base.logger.level = Logger::DEBUG
ActiveRecord::Base.configurations["test"] = YAML.load(test_dir.join("database.yml").open)[adapter] ActiveRecord::Base.configurations["test"] = YAML.load(test_dir.join("database.yml").open)[adapter]
require "activerecord-import"
ActiveRecord::Base.establish_connection "test" ActiveRecord::Base.establish_connection "test"
# load the library
require "activerecord-import"
ActiveSupport::Notifications.subscribe(/active_record.sql/) do |event, _, _, _, hsh| ActiveSupport::Notifications.subscribe(/active_record.sql/) do |event, _, _, _, hsh|
ActiveRecord::Base.logger.info hsh[:sql] ActiveRecord::Base.logger.info hsh[:sql]