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 45
def connection_class
  @connection_class ||= if @db == :default || (@db.nil? && connection_hash.nil?)
                          ::ActiveRecord::Base
                        elsif connection_hash
                          lookup_from_connection_pool || establish_connection
                        else
                          @db # allows for an actual class to be passed in
                        end
end
create_connection_class() click to toggle source
# File lib/database_cleaner/active_record/base.rb, line 41
def create_connection_class
  Class.new(::ActiveRecord::Base)
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 && 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 65
def establish_connection
  strategy_class = create_connection_class
  strategy_class.send :establish_connection, connection_hash
  strategy_class
end
lookup_from_connection_pool() click to toggle source
# File lib/database_cleaner/active_record/base.rb, line 57
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