0.0
No release in over a year
SoftDeleter.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 7.0.7
 Project Readme

SoftDeleter

soft delete Rails plugin.

Installation

Add this line to your application's Gemfile:

gem "soft_deleter"

And then execute:

$ bundle

Or install it yourself as:

$ gem install soft_deleter

Usage

Introduce

Soft delete model User.

bundle exec rails g soft_deleter user

It creates migration file to create user with attributes which is needed to introduce soft delete.
Add some attributes to migration file, like name, email, age, ...etc.
And excute bundle exec rails db:migrate. That's all.

Or if you already have User model, and you want introduce soft delete in it, create migration file and add lines like bellow

class AddSoftDeleterAttributesToUsers < ActiveRecord::Migration[7.0]
  def change
    add_column :users, :deleter_type, :string
    add_column :users, :deleter_id, :integer
    add_column :users, :deleted_at, :timestamp
  end
end

and excute bundle exec rails db:migrate
Here, deleter_type and deleter_id, these are the infomations who soft delete.
Like current_admin, when admin which is "Admin" class does soft delete user record, admin's class name and id can be recorded.

And add line to model

class User < ApplicationRecord
  include SoftDeleter
end

This line is added automatically if you use rails g soft_deleter user command to make user model.

scope

When you load users whitout soft deleted records, you need to scope like bellow.

users = User.enabled.all

If you don't use enabled scope, you will load users in all records including soft deleted.
Otherwise, if you need to load records with soft deleted, excute like bellow.

deleted_users = User.deleted.all

Soft delete

user = User.enabled.first
user.soft_delete                 # soft delete
user.soft_delete!                # soft delete or raise when fail occurs
user.restore                     # restore soft deleted user
user.restore!                    # restore soft deleted user or raise when fail occurs

If your app have some models other than user, like Admin model,
and you need to record to that Admin user did soft delete.
Then,

user = User.enabled.first

admin = Admin.enabled.first     # soft deleted by admin user
user.soft_delete(admin)         # soft delete and set admin to deleter
user.soft_delete!(admin)        # raise if fail to soft delete

user.deleter                    # => <Admin:0x00007f37f96a0c88
user.deleter_type               # => Admin(id: integer, ...
user.deleter_id                 # => "admin.id" if deleter is not set, "user.id"
user.soft_deleted?              # => true
user.alive?                     # => false

Associations

If associations some models, User, Book, Section.

# User model
class User < ApplicationRecord
  include SoftDeleter
  has_many :books, dependent: :destroy
end

# Book model
class Book < ApplicationRecord
  include SoftDeleter
  belongs_to :user
  has_many :sections, dependent: :destroy
end

# Section model
class Section < ApplicationRecord
  include SoftDeleter
  belongs_to :book
end

So, if you excute user.soft_delete, then associations books, and sections are soft deleted.
And excute user.restore, then associations books, and sections are restored.
It works with dependent destroy descriptions. If not, it doesn't work.

Contributing

Contribution directions go here.

License

The gem is available as open source under the terms of the MIT License.