Skip to content

Commit

Permalink
trys to make a little more sense out of this horrible design
Browse files Browse the repository at this point in the history
  • Loading branch information
bmabey committed Aug 25, 2012
1 parent 4cdaaa0 commit c06754a
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions lib/database_cleaner/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,34 @@ def create_connection_klass
Class.new(::ActiveRecord::Base)
end



def connection_klass
return ::ActiveRecord::Base unless connection_hash
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

private

def lookup_from_connection_pool
if ::ActiveRecord::Base.respond_to?(:descendants)
database_name = connection_hash["database"] || connection_hash[:database]
models = ::ActiveRecord::Base.descendants
klass = models.detect {|m| m.connection_pool.spec.config[:database] == database_name}
return klass if klass
models.detect {|m| m.connection_pool.spec.config[:database] == database_name}
end
end

def establish_connection
klass = create_connection_klass
klass.send :establish_connection, connection_hash
klass
end

end
end
end

0 comments on commit c06754a

Please sign in to comment.