radar/paranoia — pull request #36 by arsduo (2012-11-22 10:58:27+00:00)
Title: Support for dependent: :destroy! and :delete!, take 2

This pull request reimplements #35 without mucking around in Rails internals.

There are some cases where you want to hard-destroy dependent resources that might otherwise be paranoid -- for instance, when a user destroys her account, all related resources should be wiped from the database rather than left there orphaned.

This pull request adds support for such dependent hard-deletion using two new class methods:

```ruby
class User
# these methods are available to all classes,
# since non-paranoid classes may have paranoid dependencies
hard_delete_dependencies :widgets, :account
hard_destroy_dependencies :schematics
end
```

When a user is destroyed, the associated resources will be destroyed or deleted (with or without callbacks, respectively) as expected.

A few points of note:

* Since this uses an after_destroy callback, it only works when the resource is destroyed, not deleted (unlike AR's dependent options). I think this is an acceptable cost, and is documented in the readme.
* For the same reason, you need to call hard_*_dependencies after any other after_destroy callbacks that depend on the children being present; this is also documented.
* This should work on all modern versions of Rails, since it only relies on after_destroy and find_in_batches.

The changes are tested.

Thanks!