Giter Club home page Giter Club logo

soft_deletion's Introduction

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 soft-deletable dependencies that are marked as :destroy, :delete_all, :nullify
user = User.first
user.products.count == 10
user.soft_delete!(validate: false)
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

# Do NOT use on assocations: Account.first.users.with_deleted {

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

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

TODO

  • has_many :through should delete join associations on soft_delete
  • cascading soft_deletes should use the same timestamp for easy reverts

Authors

Contributors

Zendesk
[email protected]
License: MIT
CI

soft_deletion's People

Contributors

bsboris avatar gabetax avatar grosser avatar kpandya3 avatar markusharmsen avatar olivernn avatar petergoldstein avatar pikachuexe avatar pinkvelociraptor avatar pschambacher avatar ragurney avatar roman-melnyk avatar rtatsumi avatar steved avatar vanhalt avatar viralpraxis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

soft_deletion's Issues

v1.6.0+ breaks dependent nullify if it is not a has_many association

Hi,

In versions previous to v1.6.0, a belongs_to or has_one association may have dependent: :nullify and everything works fine. After v1.6.0 this no longer works, because it was changed to an update_all which doesn't work on singular dependencies because they don't have a scope.

The error looks like this:

NoMethodError: undefined method `update_all' for #<Foo:0x000055670f32a7c8>

See a2302b4

Thanks!

Don't really want to nullify upon soft deletion

Recently I've been adding some functionality to our app to hard-delete certain records in order to comply with European regulations. As a result of this, I've been adding dependent: :nullify to a lot of our associations.

The comment I want to make is that I think soft_deletion should basically disregard the "nullify" option. It only really has any relevance in the case of hard deletion, where we nonetheless are willing to keep certain orphaned records around. We do still use soft_deletion in most cases, and I don't want to set these records as :destroy just to avoid soft_delete nullifying them.

I figured this is worth discussing so I decided to make an issue about it. Thanks for the gem.

Rails 7

Hey there,

I tried updating an existing application to Rails 7, and the version for this gem dropped from 1.6.0 to 0.5.6 - I guess because that was the latest version that didn't have any active record dependencies declared in the gemspec.

Is this gem still maintained and are there any plans to support Rails/ActiveRecord 7?

Request: Keep a change log

Hi,

First of all, thank you for writing this library ๐Ÿ‘

Would you consider keeping a simple change log between releases? I tend to check change logs before updating libraries my apps depends on, but I don't see one for this library. It's much easier to check a change log to see what's backwards incompatibile and what's fixed than to dive into commit logs.

Thanks!

Uniqueness validations with scope include soft deleted records

When you add a uniqueness validation with a particular scope to a soft-deletable model Dog, e.g.:

validates :dog_id , uniqueness: { scope: :owner_id }

The scope you pass overrides the soft deletion default scope, so soft-deleted Dogs will make this validation fail.
As a workaround, I had to do something like this:

validates :dog_id, uniqueness: { scope: :owner_id , conditions: -> { where(deleted_at: nil) } }

It works fine, but I reckon ActiveRecord should just treat soft-deleted records as if they don't exist, regardless of what scope you pass, unless you explicitly call with_deleted.

There should be a scope for finding deleted objects

Instead of (or in addition to) Object.with_deleted { current_user.objects.find(...) }

We should have:
current_user.objects.with_deleted.find(...)

I was looking in https://github.com/sricc/soft_deletion/blob/master/lib/soft_deletion/core.rb

and it looks like you could just add this:

scope :with_deleted, -> {
    unscoped {}
}

Note: I'm using Ruby 2.0, and Rails 4

with_deleted not working properly

Just cases like User.with_deleted { User.first } no longer work. It complains that core.rb line 23 is expecting an ActiveRecord::Relation. We've had to roll back to 0.7.0 for now.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.