Module: GdprAdmin::Helpers::EraseHelper
- Includes:
- FieldAnonymizerHelper, PaperTrailHelper
- Included in:
- ApplicationDataPolicy
- Defined in:
- lib/gdpr_admin/helpers/erase_helper.rb
Instance Method Summary collapse
-
#erase_fields(record, fields, base_fields = {}) ⇒ Object
Erases the given fields on the given record using the method given.
Methods included from PaperTrailHelper
#anonymize_version_object, #anonymize_version_object_changes, #without_paper_trail
Methods included from FieldAnonymizerHelper
#anonymize_field, #nilify, #nullify, #with_seed
Methods included from Anonymizers::InternetAnonymizer
#anonymize_email, #anonymize_ip, #anonymize_password, #mask_ip
Methods included from Anonymizers::ContactAnonymizer
#anonymize_city, #anonymize_country, #anonymize_country_code2, #anonymize_country_code3, #anonymize_phone_number, #anonymize_state, #anonymize_street_address, #anonymize_zip
Methods included from Anonymizers::CompanyAnonymizer
Methods included from Anonymizers::NameAnonymizer
#anonymize_first_name, #anonymize_last_name, #anonymize_name
Instance Method Details
#erase_fields(record, fields, base_fields = {}) ⇒ Object
Erases the given fields on the given record using the method given. Fields should be an array of hashes formatted as follows:
“‘ruby
[
{ field: :first_name, method: :anonymize, seed: :id },
{ field: :last_name, method: :anonymize },
{ field: :job_title, method: -> { 'Anonymized Title' } },
{ field: :email, method: :anonymize_email },
{ field: :password_digest, method: :anonymize_password },
{ field: :phone_number, method: ->(record) { record.phone_number.split('').shuffle.join } },
{ field: :address, method: :nilify },
]
“‘
By default, the seed for the anonymize method is the value of the field being anonymized. If you want to use a different field as the seed, you can specify it using the ‘seed` key.
If you want to use a method that is not defined in the anonymizer, you can pass a lambda. The lambda will be called with the record as the first argument and the seed as the second.
The third argument of the ‘erase_fields` method is an optional base fields hash that will be used to update the record. This is useful if you want to `erase_fields` to update other fields that are not part of the erasure process. For example:
“‘ruby
erase_fields(record, fields, { anonymized_at: Time.zone.now })
“‘
This method uses the ‘update_columns` method to update the record, so it will skip validations.
47 48 49 50 51 52 53 54 |
# File 'lib/gdpr_admin/helpers/erase_helper.rb', line 47 def erase_fields(record, fields, base_fields = {}) new_data = fields.inject(base_fields) do |changes, curr| changes.merge(curr[:field] => anonymize_field(record, curr)) end without_paper_trail do record.update_columns(new_data) end end |