Sha256: 4b3dc2a9b8f250af47f9bf4a19a31ac506a6c9f84c464af5859443004c185a8a

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

require "rubocop"

require_relative "../version"

module RuboCop
  module Cop
    module Lint
      # This cop checks whether an ActiveRecord model implements the `.anonymise`
      # preference (using the Anony gem).
      #
      # @example Good
      #   class User < ApplicationRecord
      #     anonymise do
      #       overwrite do
      #         email :email
      #         hex :given_name
      #       end
      #     end
      #   end
      #
      # @example Bad
      #   class MyNewThing < ApplicationRecord; end
      class DefineDeletionStrategy < Cop
        MSG = "Define .anonymise for %<model>s, see https://github.com/gocardless/" \
              "anony/blob/#{Anony::VERSION}/README.md for details"

        def_node_search :uses_anonymise?, "(send nil? :anonymise)"

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

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

        def model?(node)
          return unless (superclass = node.children[1])

          superclass.const_name == model_superclass_name
        end

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

        def model_superclass_name
          cop_config["ModelSuperclass"] || "ApplicationRecord"
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
anony-1.0.2 lib/anony/cops/define_deletion_strategy.rb