Sha256: 3c01bf033cb9775e3aed5490c14263756e8b7660195a8f4e3ba1af9df0282404

Contents?: true

Size: 1.83 KB

Versions: 13

Compression:

Stored size: 1.83 KB

Contents

# 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)
          lambda do |corrector|
            corrector.replace(node.loc.selector, 'validates')
            correct_validate_type(corrector, node)
          end
        end

        def correct_validate_type(corrector, node)
          _receiver, method_name, *args = *node
          options = args.find { |arg| !arg.sym_type? }
          validate_type = method_name.to_s.split('_')[1]

          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

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/rails/validation.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/rails/validation.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/rails/validation.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/rails/validation.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/rails/validation.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/rails/validation.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/rails/validation.rb
rubocop-0.47.1 lib/rubocop/cop/rails/validation.rb
rubocop-0.47.0 lib/rubocop/cop/rails/validation.rb
rubocop-0.46.0 lib/rubocop/cop/rails/validation.rb
rubocop-0.45.0 lib/rubocop/cop/rails/validation.rb
rubocop-0.44.1 lib/rubocop/cop/rails/validation.rb
rubocop-0.44.0 lib/rubocop/cop/rails/validation.rb