undeletable
on the model class disables the ability to delete
/destroy
on instance, model class, and via relation, using the default ActiveRecord version of those, and supports destroy callbacks if raise not enabled. undeletable!
on the model class will raise an error when those are attempted. destroy!
in Rails 4 is supported.
Tested with ActiveRecord 3.1.x, 3.2.x, and 4.0.x via travis and appraisal.
Code originally based on Paranoia (by Ryan Bigg and others), but extremely modified to the point it really shares little in common now.
Put this in your Gemfile:
gem 'undeletable'
Then run:
bundle install
Updating is as simple as bundle update undeletable
.
Undeletable.configure do
# if true, debug log failed attempts to delete/destroy when not raising error
self.debug = false
end
...
To silently disallow attempts to delete/destroy:
class Client < ActiveRecord::Base
undeletable
...
end
To disallow attempts to delete/destroy, but raise ActiveRecord::ReadOnlyRecord on attempts:
class Client < ActiveRecord::Base
undeletable!
...
end
- v0.0.1 -> v1.0.x:
delete!
anddestroy!
no longer supported for now.destroy!
has a Rails 4 naming conflict with same named method, and attempting to provide support for ActiveRecord::Relation made it too complicated for now (problems with infinite recursion requiring more state to setup and check, etc.). If you need to delete or destroy an undeletable model, the workaround is to comment the undeletable method on the class.
This gem is released under the MIT license.