Skip to content

gsamokovarov/trashy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

3 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ—‘ 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'!

About

๐Ÿ—‘ Trashy let's you soft-delete Active Record models with ease.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published