Skip to content

Instantly share code, notes, and snippets.

@adamrobbie
Forked from basgys/soft_deletable.rb
Created February 14, 2014 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamrobbie/9001355 to your computer and use it in GitHub Desktop.
Save adamrobbie/9001355 to your computer and use it in GitHub Desktop.
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