mirror of https://github.com/rails/rails
Use mattr_accessor in TestsDatabase instead of setup method
This commit is contained in:
parent
37283a6aae
commit
4a582aa7ca
|
@ -6,25 +6,35 @@ require 'active_record/fixtures'
|
|||
|
||||
module ActiveModel
|
||||
module TestsDatabase
|
||||
mattr_accessor :connected
|
||||
|
||||
def self.included(base)
|
||||
ActiveRecord::Base.logger = Logger.new("debug.log")
|
||||
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
|
||||
unless self.connected
|
||||
setup_connection
|
||||
setup_schema
|
||||
end
|
||||
|
||||
base.send :include, ActiveRecord::TestFixtures
|
||||
base.setup :setup_database
|
||||
end
|
||||
|
||||
def setup_database
|
||||
unless $schema_file_loaded
|
||||
begin
|
||||
# TODO : May the better way be with you
|
||||
original, $stdout = $stdout, StringIO.new
|
||||
load(SCHEMA_FILE)
|
||||
ensure
|
||||
$stdout = original
|
||||
end
|
||||
def self.setup_schema
|
||||
original, $stdout = $stdout, StringIO.new
|
||||
load(SCHEMA_FILE)
|
||||
ensure
|
||||
$stdout = original
|
||||
self.connected = true
|
||||
end
|
||||
|
||||
$schema_file_loaded = true
|
||||
def self.setup_connection
|
||||
defaults = { :database => ':memory:' }
|
||||
begin
|
||||
adapter = defined?(JRUBY_VERSION) ? 'jdbcsqlite3' : 'sqlite3'
|
||||
options = defaults.merge :adapter => adapter, :timeout => 500
|
||||
ActiveRecord::Base.establish_connection(options)
|
||||
rescue Exception
|
||||
$stderr.puts 'SQLite 3 unavailable; trying SQLite 2.'
|
||||
options = defaults.merge :adapter => 'sqlite'
|
||||
ActiveRecord::Base.establish_connection(options)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue