Module: ConcernsOnRails::Models::Anonymizable::ClassMethods

Includes:
Support::ColumnGuard
Defined in:
lib/concerns_on_rails/models/anonymizable.rb

Instance Method Summary collapse

Methods included from Support::ColumnGuard

#column_migration_hint, #ensure_columns!, #ensure_columns_on!, #schema_reachable?

Instance Method Details

#anonymizable(*fields, with:, stamp: UNSET, clear_audit_trail: UNSET, prefix: nil, suffix: nil) ⇒ Object

Declare fields and their erasure strategy. Repeatable — field rules merge across calls; stamp:/clear_audit_trail:/prefix:/suffix: apply only when explicitly passed (last explicit value wins).

Raises:

  • (ArgumentError)


91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/concerns_on_rails/models/anonymizable.rb', line 91

def anonymizable(*fields, with:, stamp: UNSET, clear_audit_trail: UNSET, prefix: nil, suffix: nil)
  raise ArgumentError, "#{LABEL}: at least one field is required" if fields.empty?

  strategy = anonymizable_resolve_strategy(with)
  anonymizable_apply_options(stamp, clear_audit_trail)

  ensure_columns!(LABEL, fields)
  ensure_columns!(LABEL, anonymizable_stamp) if anonymizable_stamp
  self.anonymizable_rules = anonymizable_rules.merge(fields.to_h { |f| [f.to_sym, strategy] })

  anonymizable_define_scopes(prefix, suffix)
end

#anonymize_all!Object

Anonymize every matching record that isn't already stamped, in one transaction. Returns the Integer count of records anonymized (the 1.22 batch contract). Without a stamp column every record matches. Streams in PK batches (find_each) rather than loading the relation, filters stamped rows DB-side, and skips the per-record reload — the batch discards its instances, so reloading each one would cost a wasted SELECT per row.



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/concerns_on_rails/models/anonymizable.rb', line 111

def anonymize_all!
  relation = anonymizable_stamp ? all.where(anonymizable_stamp => nil) : all
  transaction do
    count = 0
    relation.find_each do |record|
      next if record.anonymized?

      record.send(:anonymize_record!)
      count += 1
    end
    count
  end
end