Project

trashy

0.0
No commit activity in last 3 years
No release in over 3 years
Trashy let's you soft-delete Active Record models with ease
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0

Runtime

 Project Readme

🗑 Trashy

If facebook ever taught us something, it was that, NO, I will not accept your friend request! Oh, and you know, never, ever, delete the data of your users. Make them call you or write to your support. Ask them for their identification documents so you can "verify them". Then, collect that ID data as well and still don't delete their accounts. 🤷‍♀️ Oh, voluntary social surveillance, how much joy bringest thou to me! 👀

Since facebook, Google, and everyone else does it, why don't you? I'm sure you have a "good" reason to preserve users' data, now let me show you how to do it. 😅

The idea is simple. Don't deleting the data, mark it as "no show" instead!

The first thing you wanna do is add the trashy gem into your Gemfile, like so:

gem "trashy"

Next, introduce a timestamp to the models, so we know when they were "deleted" or trashed as we'll call this operation from now on. The timestamp is our marker, if you will:

bundle exec rails g migration AddDeletedAtToUsers deleted_at:timestamp

The timestamp is called deleted_at and needs to be NULLable. You can get into the habit of introducing it to every model.

Afterwards, all you need to do include the Trashy module into your ApplicationRecord model like so:

class ApplicationRecord
  self.abstract_class = true

  include Trashy
end

Now, if you wanna delete a User, instead of doing User#destroy, do User#trash.

class User < ApplicationRecord  
end

user = User.find(420)
user.trash

The trashed models won't be included in queries generated by Active Record. If you need to get them, you can use User.trashed.

If you need to learn more or add a bit of configuration, check-out the source code.

Happy data and don't be creepin'!