For the EU's GDPR compliance (user privacy), we need to redact personally identifiable information form the versions of our records. I've come up with something that seems to work, but figure I should ask if there's an established way to do this.
class User < ActiveRecord::Base
has_paper_trail
end
user = User.create! name: 'Josh'
user.update_attributes name: 'Josh2'
user.update_attributes name: 'Josh3'
user.destroy!
def self.get_data
PaperTrail::Version.order(:id).where(item_id: 1).map { |ver| [ver.event, ver.object, ver.object_changes] }
end
# ===== BEFORE =====
get_data
# => [["create", nil, {"id"=>[nil, 1], "name"=>[nil, "Josh"]}],
# ["update", {"id"=>1, "name"=>"Josh"}, {"name"=>["Josh", "Josh2"]}],
# ["update", {"id"=>1, "name"=>"Josh2"}, {"name"=>["Josh2", "Josh3"]}],
# ["destroy", {"id"=>1, "name"=>"Josh3"}, nil]]
PaperTrail::Version.where_object_changes(name: 'Josh').each do |ver|
ver.object['name'] = 'REDACTED' if ver.object && ver.object['name'] == 'Josh'
if oc = ver.object_changes
oc['name'] = oc['name'].map { |name| name == 'Josh' ? 'REDACTED' : name }
ver.object_changes = oc
end
ver.save!
end
# ===== AFTER =====
get_data
# => [["create", nil, {"id"=>[nil, 1], "name"=>[nil, "REDACTED"]}],
# ["update",
# {"id"=>1, "name"=>"REDACTED"},
# {"name"=>["REDACTED", "Josh2"]}],
# ["update", {"id"=>1, "name"=>"Josh2"}, {"name"=>["Josh2", "Josh3"]}],
# ["destroy", {"id"=>1, "name"=>"Josh3"}, nil]]
UPDATE: Actually, I'm going to need to scope the record by an association, as well, so my example isn't sufficient.