Allows one to annotate an ActiveRecord module as being soft_destroyable.
This gem was inspired by acts_as_paranoid and
rails3_acts_as_paranoid, both of which are great gems.
I found that rails3_acts_as_paranoid did not handle associations the way I wanted (at least at the time I wrote this),
so I wrote this implementation instead.
This changes the behavior of the method to become a soft-destroy, which
will set the attribute to Time.now, and the attribute to true
It exposes the method to reverse the effects of (for :dependent => :destroy associations only).
It also exposes the method which can be used to really destroy an object and it’s associations.
will not revive child associations which have been destroyed by actions other a destroy of the parent.
This requires the column attribute .
Standard ActiveRecord destroy callbacks are not called, however you can override , ,
and on your soft_destroyable models.
Standard ActiveRecord dependent options :destroy, :restrict, :nullify, :delete_all, and :delete are supported and will
behave as expected. will not undo the effects of , , and .
is effected by the state. In other words, deleted child models will not raise a restriction when
destroying the parent.
The operation is not modified by this module.
The operations: , , and are automatically delegated to the dependent association records.
in a single transaction.
Scopes are provided for and . The standard scope is not polluted, so will still
return all records, deleted or not.
Author: Michael Kintzer
- Migrate your model to add and attributes. There are helpers provided for this:
create_table :mytable do |t| t.soft_destroyable end
change_table :mytable do |t| t.soft_destroyable end
- Annotate your model to be soft_destroyable
class Parent has_many :children, :dependent => :restrict has_many :animals, :dependent => :nullify soft_destroyable ...
- That’s it!
- make sure you have rails and sqlite gems installed.
- rake test
- Fork the project.
- Make your feature addition or bug fix.
- Add tests for it. This is important so I don’t break it in a
future version unintentionally.
- Commit, do not mess with rakefile, version, or history.
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
- Send me a pull request
Copyright © 2010-11 Michael Kintzer, released under the MIT license