use ActiveRecord::Base configuration when different with config file

This commit is contained in:
wendy0402 2014-12-13 23:48:07 +07:00
parent d42481813c
commit c428633514
2 changed files with 38 additions and 1 deletions

View file

@ -34,10 +34,19 @@ module DatabaseCleaner
def load_config
if self.db != :default && self.db.is_a?(Symbol) && File.file?(ActiveRecord.config_file_location)
connection_details = YAML::load(ERB.new(IO.read(ActiveRecord.config_file_location)).result)
@connection_hash = connection_details[self.db.to_s]
@connection_hash = valid_config(connection_details)[self.db.to_s]
end
end
def valid_config(connection_file)
if !::ActiveRecord::Base.configurations.nil? && !::ActiveRecord::Base.configurations.empty?
if connection_file != ::ActiveRecord::Base.configurations
return ::ActiveRecord::Base.configurations
end
end
connection_file
end
def connection_class
@connection_class ||= if db && !db.is_a?(Symbol)
db

View file

@ -84,6 +84,34 @@ my_db:
subject.load_config
end
context 'use ActiveRecord::Base.configuration' do
it 'when config file different with it' do
::ActiveRecord::Base.stub(:configurations).and_return({ "my_db" =>{ "database" => "two"} })
subject.load_config
subject.connection_hash.should eq({ "database" => "two"})
end
end
context 'use config file' do
it 'when config file same with it' do
::ActiveRecord::Base.stub(:configurations).and_return({ "my_db" =>{ "database" => "one"} })
subject.load_config
subject.connection_hash.should eq({ "database" => "one"})
end
it 'when ::ActiveRecord::Base.configurations nil' do
::ActiveRecord::Base.stub(:configurations).and_return(nil)
subject.load_config
subject.connection_hash.should eq({ "database" => "one"})
end
it 'when ::ActiveRecord::Base.configurations empty' do
::ActiveRecord::Base.stub(:configurations).and_return({})
subject.load_config
subject.connection_hash.should eq({ "database" => "one"})
end
end
it "should store the relevant config in connection_hash" do
subject.load_config
subject.connection_hash.should eq( "database" => "one" )