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:
parent
6c6a0d22b9
commit
cb542a86f4
|
@ -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"
|
||||
ActiveRecord::Import.load
|
||||
end
|
||||
ActiveRecord::Import.load_from_connection connection
|
||||
end
|
||||
|
|
|
@ -11,12 +11,16 @@ module ActiveRecord::Import
|
|||
require File.join(AdapterPath,"/#{adapter}_adapter")
|
||||
end
|
||||
|
||||
# Loads the import functionality for the current ActiveRecord::Base.connection
|
||||
def self.load
|
||||
config = ActiveRecord::Base.connection.instance_variable_get :@config
|
||||
# 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
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
this_dir = Pathname.new File.dirname(__FILE__)
|
||||
require this_dir.join("import")
|
||||
|
|
|
@ -28,10 +28,11 @@ FileUtils.mkdir_p 'log'
|
|||
ActiveRecord::Base.logger = Logger.new("log/test.log")
|
||||
ActiveRecord::Base.logger.level = Logger::DEBUG
|
||||
ActiveRecord::Base.configurations["test"] = YAML.load(test_dir.join("database.yml").open)[adapter]
|
||||
|
||||
|
||||
require "activerecord-import"
|
||||
ActiveRecord::Base.establish_connection "test"
|
||||
|
||||
# load the library
|
||||
require "activerecord-import"
|
||||
|
||||
ActiveSupport::Notifications.subscribe(/active_record.sql/) do |event, _, _, _, hsh|
|
||||
ActiveRecord::Base.logger.info hsh[:sql]
|
||||
|
|
Loading…
Reference in a new issue