-
-
Save adamrobbie/9001355 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SoftDeletable | |
extend ActiveSupport::Concern | |
def soft_delete! | |
find_each do |record| | |
record.soft_delete! | |
end | |
end | |
included do | |
scope :deleted, where("#{table_name}.deleted_at IS NOT NULL") | |
define_model_callbacks :soft_delete | |
def soft_delete! | |
run_callbacks :soft_delete do | |
self.deleted_at = Time.now | |
save!(validate: false) | |
end | |
end | |
def restore! | |
self.deleted_at = nil | |
save!(validate: false) | |
end | |
def deleted? | |
deleted_at.present? | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment