bmabey/database_cleaner — pull request #212 by jfelchner (2013-05-27 22:06:49+00:00)
Title: Request for #autoloadable?

@bmabey I'm not sure if you'd be open to this, so I'm asking here first before I submit a PR.

I have a gem which works with RSpec to try and handle a bunch of setup/use cases for common gems which work with RSpec. The main goal for [my gem](/jfelchner/rspectacular) is to be a drop-in replacement for a spec_helper but which is faster and more portable for everyone (eg no more copying and pasting the same code for the same gems in all your projects) as well as easier for newbies to get up and running without having to know the details.

For the [database_cleaner portion](https://github.com/jfelchner/rspectacular/blob/70e289f82b75d00b366d762b06adf0bad85031d2/lib/rspectacular/plugins/database_cleaner.rb) I'm utilizing the best practices outlined in the wiki for enabling the fastest tests possible for various testing scenarios (eg javascript vs unit tests vs no DB tests).

The problem I'm running in to is that, when database_cleaner is included in a project, it's attempting to load it for tests which require database access _and also_ for those which don't.

Example:

```
rspec database_needed_spec.rb
# Finishes Successfully
```

```
rspec spec
# Finishes Successfully
```

```
rspec active_record_not_loaded_spec.rb
# Fails Because Database Cleaner Can't Find Any ORMs
```

This hurts because it means that I _have_ to include an ORM even on those specs which don't include it. My workaround is ([full file with hacky workaround](https://github.com/jfelchner/rspectacular/blob/e18df5801df354288cabee64d2cc62c4447d0e55/lib/rspectacular/plugins/database_cleaner.rb)):

```ruby
begin
autodetected = DatabaseCleaner::Base.new.send(:autodetect)
rescue DatabaseCleaner::NoORMDetected
autodetected = false
end

if autodetected
# Load DatabaseCleaner hooks
end
```

Not only is it ugly, not only is it using an exception to drive flow, but it's using one of database_cleaner's private API calls.

What I'd _like_ to have is something like:

```ruby
if DatabaseCleaner.can_detect_orm?
# Load DatabaseCleaner hooks
end
```

So my questions are: Can you think of a better way to do this? and Are you opposed to the idea of adding a method like this to the public API?