Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Explicit soft deletion for ActiveRecord via deleted_at and default scope
Ruby

v0.5.6

latest commit 9d0597c50c
@grosser authored
Failed to load latest commit information.
gemfiles
lib
spec
.gitignore Pointless to keep the Appraisals gemfile lock
.travis.yml remove table exist check on rails 3, it is lazy anyway, also fixes we…
Appraisals remove table exist check on rails 3, it is lazy anyway, also fixes we…
Gemfile
Gemfile.lock v0.5.6
MIT-LICENSE add a MIT-LICENSE file, in case you do not know what it means or you …
Rakefile bump
Readme.md efficient soft_delete
gem-public_cert.pem
soft_deletion.gemspec

Readme.md

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

Install

gem install soft_deletion

Usage

require 'soft_deletion'

class User < ActiveRecord::Base
  has_soft_deletion :default_scope => true

  before_soft_delete :validate_deletability # soft_delete stops if this returns false
  after_soft_delete :send_deletion_emails

  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

# soft delete many
User.soft_delete_all!(1,2,3,4)

TODO

  • nullify should use update_all on has_many on soft_delete (performance)
  • has_many :through should delete join associations on soft_delete
  • cascading soft_deletes should use the same timestamp for easy reverts

Authors

Contributors

Zendesk
michael@grosser.it
License: MIT
Build Status

Something went wrong with that request. Please try again.