kakurenbo-puti provides an ActiveRecord-friendly soft-delete gem. This gem does not override methods of ActiveRecord.
I think that kakurenbo-puti is cho-iketeru! (very cool!)
Add this line to your application's Gemfile:
gem 'kakurenbo-puti'
And then execute:
$ bundle
Or install it yourself as:
$ gem install kakurenbo-puti
At first, add soft_destroyed_at
column to your model.
$ rails g migration AddSoftDestroyedAtToYourModel soft_destroyed_at:datetime:index
$ rake db:migrate
Next, call soft_deletable
in model.
class Member < ActiveRecord::Base
soft_deletable
end
# Get models without soft-deleted
Model.without_soft_destroyed
# Get models only soft-deleted
Model.only_soft_destroyed
model.soft_destroy
# or
model.soft_destroy!
# check soft_destroyed
model.soft_destroyed? # => true
model.restore
# or
model.restore!
Use dependent_associations option of soft-deletable
.
This option is useable only in belongs_to
.
class Parent < ActiveRecord::Base
soft_deletable
has_many :children
end
class Child < ActiveRecord::Base
soft_deletable dependent_associations: [:parent]
belongs_to :parent
end
# create instance
parent = Parent.create!
child = Child.create!
# add child
parent.children << child
child.soft_destroyed? # false
# soft-destroy parent
parent.soft_destroy
child.soft_destroyed? # true
class Member < ActiveRecord::Base
soft_deletable :column => :deleted_at
end
This gem is released under the MIT license.