westonganger / active_snapshot

Simplified snapshots and restoration for ActiveRecord models and associations with a transparent white-box implementation
MIT License
103 stars 16 forks source link

Add dependent: :destroy on snapshots #40

Open Judobob opened 1 year ago

Judobob commented 1 year ago

Is it possible to add in the future the ability to have dependent delete on the snapshots? If a user removes himself from our app we would like to have all his snapshots removed also without having to do it through a method manually.

westonganger commented 1 year ago

There is this comment in the readme

Destroy snapshot and all its child snapshots must be performed manually, snapshots and snapshot items are NEVER destroyed automatically

My initial reaction is that I want to keep that approach.

However as I think about it, if you are deleting the record than its probable that you would want the snapshots deleted too. If you really wanted to keep the snapshots I suspect you would actually mark your record as inactive/archived.

Maybe an option could be helpful.

As it stands however I think its easy enough to just modify your destroy method, even if it does seem a little non-intuitive.

class User < ApplicationRecord
  def destroy
    destroyed = super
    if destroyed
      Snapshot.where(item_type: self.class.name, item_id: id).destroy_all
    end
    return destroyed
  end
end