module DatabaseCleaner::ActiveRecord::Base

Attributes

connection_hash[RW]

Public Instance Methods

connection_class() click to toggle source
# File lib/database_cleaner/active_record/base.rb, line 41
def connection_class
  @connection_class ||= if @db && !@db.is_a?(Symbol)
                          @db
                        elsif connection_hash
                          lookup_from_connection_pool || establish_connection
                        else
                          ::ActiveRecord::Base
                        end
end
db() click to toggle source
Calls superclass method DatabaseCleaner::Generic::Base#db
# File lib/database_cleaner/active_record/base.rb, line 30
def db
  @db || super
end
db=(desired_db) click to toggle source
# File lib/database_cleaner/active_record/base.rb, line 25
def db=(desired_db)
  @db = desired_db
  load_config
end
load_config() click to toggle source
# File lib/database_cleaner/active_record/base.rb, line 34
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]
  end
end

Private Instance Methods

establish_connection() click to toggle source
# File lib/database_cleaner/active_record/base.rb, line 61
def establish_connection
  ::ActiveRecord::Base.establish_connection(connection_hash)
end
lookup_from_connection_pool() click to toggle source
# File lib/database_cleaner/active_record/base.rb, line 53
def lookup_from_connection_pool
  if ::ActiveRecord::Base.respond_to?(:descendants)
    database_name = connection_hash["database"] || connection_hash[:database]
    models        = ::ActiveRecord::Base.descendants
    models.detect { |m| m.connection_pool.spec.config[:database] == database_name }
  end
end