Project

soft_trash

0.0
The project is in a healthy, maintained state
A Ruby gem that provides soft delete and recovery capabilities using deleted_at timestamp
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

 Project Readme

SoftTrash

Soft deletes for ActiveRecord done right. SoftTrash provides a simple and effective way to implement soft deletion in your Rails applications.

Features

  • Simple integration with ActiveRecord models
  • Customizable column names for soft delete functionality
  • Scopes for including/excluding soft-deleted records
  • Callbacks for soft deletion events
  • Works with associations
  • Supports Rails 5.2+ and Ruby 2.6+

Installation

Add this line to your application's Gemfile:

gem 'soft_trash'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install soft_trash

Usage

Basic Setup

  1. Generate and run the migration for a model:
rails generate soft_trash:install Post
rails db:migrate
add t.soft_trash 
example 

class CreatePosts < ActiveRecord::Migration[8.0]
  def change
    create_table :posts do |t|
      t.string :name
      t.soft_trash
      t.timestamps
    end
  end
end
  1. Include the module in your model:
class Post < ApplicationRecord
  include SoftTrash::Model
end

Default Behavior

By default, SoftTrash will:

  • Add a deleted_at datetime column to track deletion
  • Add a default scope to exclude soft-deleted records
  • Override destroy to perform soft deletion
  • Provide really_destroy! for permanent deletion

Scopes

# Get all records (default scope)
Post.all

# Get only non-deleted records
Post.active

# Get only deleted records
Post.deleted

Instance Methods

post = Post.find(1)

# Soft delete a record
post.trash!  # Sets deleted_at to current time

# Check if a record is soft-deleted
post.trashed?  # => true

# Restore a soft-deleted record
post.restore!

# Check if a record is not soft-deleted
post.active?  # => true

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/mahendraxceed/soft_trash. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

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

Code of Conduct

Everyone interacting in the SoftTrash project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.