Skip to content

Dagnan/soft_deletion

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Explicit soft deletion for ActiveRecord via deleted_at and default scope + callbacks.
Not overwriting destroy or delete.

Install

gem install soft_deletion

Or

rails plugin install git://github.com/grosser/soft_deletion.git

Usage

# mix into any model ...
class User < ActiveRecord::Base
  include SoftDeletion

  after_soft_delete :send_deletion_emails # Rails 2 + 3
  set_callback :soft_delete, :after, :prepare_emails # Rails 3

  has_many :products
end

# soft delete them including all dependencies that are marked as :destroy, :delete_all, :nullify
user = User.first
user.products.count == 10
user.soft_delete!
user.deleted? # true

# use special with_deleted scope to find them ...
user.reload # ActiveRecord::RecordNotFound
User.with_deleted do
  user.reload # there it is ...
  user.products.count == 0
end

# soft undelete them all
user.soft_undelete!
user.products.count == 10

TODO

Author

Zendesk
michael@grosser.it
License: MIT
Build Status

About

Explicit soft deletion for ActiveRecord via deleted_at and default scope

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%