From c428633514cbcef18a01815d1516cea3b6a90923 Mon Sep 17 00:00:00 2001 From: wendy0402 Date: Sat, 13 Dec 2014 23:48:07 +0700 Subject: [PATCH] use ActiveRecord::Base configuration when different with config file --- lib/database_cleaner/active_record/base.rb | 11 +++++++- .../active_record/base_spec.rb | 28 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/database_cleaner/active_record/base.rb b/lib/database_cleaner/active_record/base.rb index 6a3e3bb..85bf50b 100644 --- a/lib/database_cleaner/active_record/base.rb +++ b/lib/database_cleaner/active_record/base.rb @@ -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 diff --git a/spec/database_cleaner/active_record/base_spec.rb b/spec/database_cleaner/active_record/base_spec.rb index a312227..ff5be7f 100644 --- a/spec/database_cleaner/active_record/base_spec.rb +++ b/spec/database_cleaner/active_record/base_spec.rb @@ -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" )