Resurrection
Christ died on a Friday, and rose again on Sunday. In honor of this feat, I present Resurrection. You can delete an object and bring it back to life. Simple as that.
Installation
Add this line to your gemfile. I'm not adding this to rubygems. theres a million of these suckers out there. This one is mine with my own little tweaks. Feel free to use it.
gem 'resurrection', github: 'nwwatson/resurrection'
Use
Okay, lets use this sucker. First we need to add a deleted_at field to our model.
bundle exec rails g migration add_column_deleted_at_to_jesuses deleted_at:datetime
All the work is done in a Concern. Yeah, I know, there are those that are against Concerns. I'm not. I don't like voodoo magic that appears when I don't expect it to (ie, adding it to ActiveRecord::Base). This simple include reminds me that I have the power to raise stuff from the dead
include Resurrection::Model
For those of you who are challenged, your model may look something like this....
class Jesus < ActiveRecord::Base
include Resurrection::Model
end
Jesus is my model. You can make your model whatever you want.
So lets delete something. I know what your thinking.... Why didn't you name it crucify? Well, I don't want to crucify things. Its a very painful death. I don't want to relive Passion of the Christ every time I delete an object. A simple delete will do for me.
my_model = Model.find(params[id])
my_model.delete
You can also use delete! if you are so include. If it makes you feel like your really deleting it with the "delete!", then use it. They both do the same thing.
Many gems add a default scope hiding deleted items from your view. I don't like this. Your model will have scopes in it to help you find records. The scopes are available and deleted. For example, if I want to view all non deleted files id do this:
Model.available
Likewise, I can find all deleted
Model.deleted
Okay, its time to resurrect something. I like that. Because Jesus made resurrection possible, I'm going to resurrect him.
jesus = Jesus.first
jesus.resurrect!
Noticed I used "ressurect!" I did this because its Jesus. If it wasn't him, i'm just call resurrect. They both do the same thing, but Jesus is special, and he deserves a bang at the end.
Callback awesomeness
So you may want callbacks, such as after_delete, before_delete, and after_resurrection. They are there for the taking.
Contributing
If you'd like to contribute a feature or bugfix: Thanks. To make sure your fix/feature has a high chance of being included, please read the following guidelines:
- Post a pull request.
- Make sure there are tests! It's a rare time when explicit tests aren't needed. If you have questions about writing tests for paperclip, please open a GitHub issue.
License
This rocks the MIT license. Live long an prosper.