Skip to content

prosas/soft_deletable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Soft_Deletable

Soft delete implementation for Rails app.

Usage

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

Callbacks

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!

Conditional

You can define a validation for the soft_destroy method.

#...
 soft_destroy :removed, if: ->(instance){ instance.can_remove? }, message: 'Don`t do this.'
#...

Custom function

Allow you to define your own implementation.

#...
 soft_destroy :removed do |instance|
 	# additional things
 	instance.update_column(removed: true, updated_at: Time.now)
 end
#...