Module: DeleteSoftly::InstanceMethods
- Defined in:
- lib/instance_methods.rb
Instance Method Summary collapse
-
#destroy ⇒ Object
Custom destroy method for models using delete_softly.
-
#revive ⇒ Object
(also: #undelete, #undestroy)
Revive a destroyed item.
Instance Method Details
#destroy ⇒ Object
Custom destroy method for models using delete_softly
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/instance_methods.rb', line 5 def destroy if persisted? with_transaction_returning_status do _run_destroy_callbacks do update_attributes(:deleted_at => Time.now.utc) end end end @destroyed = true freeze end |
#revive ⇒ Object Also known as: undelete, undestroy
Revive a destroyed item. For a model like:
class Post < ActiveRecord::Base
delete_softly
end
Then the following can be done:
p = Post.find(1)
p.destroy
p = Post.find(1) # raise error
p = Post.with_deleted.find(1) # Original object but with deleted_at attribute set
p.revive #=> deleted_at => nil
p = Post.find(1) # business as usual
If papertrail is used for this model it will not store a copy
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/instance_methods.rb', line 30 def revive # Disable paper_trail when it is present and active if self.class.respond_to?(:paper_trail_active) && self.class.paper_trail_active self.class.paper_trail_off update_attribute :deleted_at, nil self.class.paper_trail_on else update_attribute :deleted_at, nil end end |