Soft delete implementation for Rails app.
Install gem:
gem 'soft_deletable', git: "https://github.com/prosas/soft_deletable"
class MyModel < ApplicationRecord
extend SoftDeletable
soft_destroy :removed # Boolean column that defines whether the record was removed or not.
end
@model = MyModel.find(id)
@model.destroy
MyModel.find(@model.id)
# >> ActiveRecord::RecordNotFound
Rails callbacks will trigger normally.
class MyModel < ApplicationRecord
extend SoftDeletable
before_destroy do
puts "...will be destroyed"
end
after_destroy do
puts "destroyed!"
end
soft_destroy :removed
end
MyModel.first.destroy
# >> ...will be destroyed
# >> destroyed!
You can define a validation for the soft_destroy method.
#...
soft_destroy :removed, if: ->(instance){ instance.can_remove? }, message: 'Don`t do this.'
#...
Allow you to define your own implementation.
#...
soft_destroy :removed do |instance|
# additional things
instance.update_column(removed: true, updated_at: Time.now)
end
#...