Module: Anony::Anonymisable::ClassMethods

Defined in:
lib/anony/anonymisable.rb

Overview

Mixin containing methods that will be exposed on the ActiveRecord class after including the Anonymisable module.

The primary method, .anonymise, is used to configure the strategies to apply. This configuration is lazily executed when trying to actually anonymise an instance: this is because the database or other lazily-loaded properties are not necessarily available when the class is configured.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#anonymise_configObject (readonly)

Returns the value of attribute anonymise_config.



88
89
90
# File 'lib/anony/anonymisable.rb', line 88

def anonymise_config
  @anonymise_config
end

Instance Method Details

#anonymise { ... } ⇒ Object

Define a set of anonymisation configuration on the ActiveRecord class.

Examples:

class Manager < ApplicationRecord
  anonymise do
    overwrite do
      with_strategy(:first_name) { "ANONYMISED" }
    end
  end
end

Yields:

  • A configuration block

See Also:

  • Anony::Strategies::Overwrite - the methods available inside this block


42
43
44
# File 'lib/anony/anonymisable.rb', line 42

def anonymise(&block)
  @anonymise_config = ModelConfig.new(self, &block)
end

#anonymise_for!(subject, subject_id) ⇒ Object

Finds the records that relate to a particular subject and runs anonymise on each of them. If a selector is not defined it will raise an exception.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/anony/anonymisable.rb', line 60

def anonymise_for!(subject, subject_id)
  unless anonymise_config
    raise ArgumentError, "#{name} does not have an Anony configuration"
  end

  records = anonymise_config.
    select(subject, subject_id)
  records.map do |record|
    if !record.respond_to?(:anonymise!)
      raise NotAnonymisableException, record
    end

    record.anonymise!
  end
end

#selector_for?(subject) ⇒ Boolean

Checks if a selector has been defined for a given subject. This is useful for when writing tests to check all models have a valid selector for a given subject.

Examples:

Manager.selector_for?(:user_id)

Returns:

  • (Boolean)


82
83
84
85
86
# File 'lib/anony/anonymisable.rb', line 82

def selector_for?(subject)
  return false if anonymise_config.nil?

  anonymise_config.selector_for?(subject)
end

#valid_anonymisation?Boolean

Check whether the model has been configured correctly. Returns a simple ‘true`/`false`. If configuration has not yet been configured, it returns `false`.

Examples:

Manager.valid_anonymisation?

Returns:

  • (Boolean)


52
53
54
55
56
# File 'lib/anony/anonymisable.rb', line 52

def valid_anonymisation?
  return false unless @anonymise_config

  @anonymise_config.valid?
end