A monkey patch for ActiveRecord to prevent physical deletion.
Add this line to your Rails application's Gemfile:
gem 'guard_against_physical_delete'
And then execute:
bundle install
If there is a record with a column named deleted_at
,
an exception is automatically raised on the methods that are likely to perform physical deletion.
# This will raise `GuardAgainstPhysicalDelete::PhysicalDeleteError`.
user.delete
# To allow phsycal deletion, do it in `physical_delete { ... }` block.
user.class.physical_delete do
user.delete
end
If you want to use a column name other than deleted_at
,
you can change it as follows:
class User < ApplicationRecord
self.logical_delete_column = :removed_at
end
This gem is available as open source under the terms of the MIT License. See License.txt for more details.