Class: RuboCop::Cop::Lint::DefineDeletionStrategy

Inherits:
RuboCop::Cop
  • Object
show all
Defined in:
lib/anony/cops/define_deletion_strategy.rb

Overview

This cop checks whether an ActiveRecord model implements the ‘.anonymise` preference (using the Anony gem).

Examples:

Good

class User < ApplicationRecord
  anonymise do
    overwrite do
      email :email
      hex :given_name
    end
  end
end

Bad

class MyNewThing < ApplicationRecord; end

Constant Summary collapse

MSG =
"Define .anonymise for %<model>s, see https://github.com/gocardless/" \
"anony/blob/#{Anony::VERSION}/README.md for details".freeze

Instance Method Summary collapse

Instance Method Details

#class_name(node) ⇒ Object



43
44
45
# File 'lib/anony/cops/define_deletion_strategy.rb', line 43

def class_name(node)
  node.children[0].const_name
end

#model?(node) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/anony/cops/define_deletion_strategy.rb', line 38

def model?(node)
  superclass = node.children[1]
  model_superclass_name.include? superclass&.const_name
end

#model_superclass_nameObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/anony/cops/define_deletion_strategy.rb', line 47

def model_superclass_name
  unless cop_config["ModelSuperclass"]
    return ["ApplicationRecord"]
  end

  if cop_config["ModelSuperclass"].is_a?(Array)
    return cop_config["ModelSuperclass"]
  end

  [cop_config["ModelSuperclass"]]
end

#on_class(node) ⇒ Object



31
32
33
34
35
36
# File 'lib/anony/cops/define_deletion_strategy.rb', line 31

def on_class(node)
  return unless model?(node)
  return if uses_anonymise?(node)

  add_offense(node, message: sprintf(MSG, model: class_name(node)))
end