Sha256: b91301043752f12d856e426164cdc2c8ad81c3bd4a769a326b94b30aeb487e48

Contents?: true

Size: 1.83 KB

Versions: 3

Compression:

Stored size: 1.83 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # This cop checks for the use of old-style attribute validation macros.
      class Validation < Cop
        MSG = 'Prefer the new style validations `%s` over `%s`.'.freeze

        TYPES = %w(
          acceptance
          confirmation
          exclusion
          format
          inclusion
          length
          numericality
          presence
          size
          uniqueness
        ).freeze

        BLACKLIST = TYPES.map { |p| "validates_#{p}_of".to_sym }.freeze

        WHITELIST = TYPES.map { |p| "validates :column, #{p}: value" }.freeze

        def on_send(node)
          receiver, method_name, *_args = *node
          return unless receiver.nil? && BLACKLIST.include?(method_name)

          add_offense(node,
                      :selector,
                      format(MSG,
                             preferred_method(method_name),
                             method_name))
        end

        private

        def preferred_method(method)
          WHITELIST[BLACKLIST.index(method.to_sym)]
        end

        def autocorrect(node)
          _receiver, method_name, *args = *node
          options = args.find { |arg| arg.type != :sym }
          lambda do |corrector|
            validate_type = method_name.to_s.split('_')[1]
            corrector.replace(node.loc.selector, 'validates')
            cop_config['AllowUnusedKeywordArguments']
            if options
              corrector.replace(
                options.loc.expression,
                "#{validate_type}: { #{options.source} }"
              )
            else
              corrector.insert_after(
                node.loc.expression,
                ", #{validate_type}: true"
              )
            end
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-0.41.2 lib/rubocop/cop/rails/validation.rb
rubocop-0.41.1 lib/rubocop/cop/rails/validation.rb
rubocop-0.41.0 lib/rubocop/cop/rails/validation.rb