Sha256: 60eaf139ab6654b00f811dc6094dfff90604f916bd7656287207dd7cce80a26e

Contents?: true

Size: 1.07 KB

Versions: 12

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # Checks that ActiveRecord aliases are not used. The direct method names
      # are more clear and easier to read.
      #
      # @example
      #   #bad
      #   Book.update_attributes!(author: 'Alice')
      #
      #   #good
      #   Book.update!(author: 'Alice')
      class ActiveRecordAliases < Base
        extend AutoCorrector

        MSG = 'Use `%<prefer>s` instead of `%<current>s`.'

        ALIASES = {
          update_attributes: :update,
          update_attributes!: :update!
        }.freeze

        RESTRICT_ON_SEND = ALIASES.keys.freeze

        def on_send(node)
          method_name = node.method_name
          alias_method = ALIASES[method_name]

          add_offense(
            node.loc.selector,
            message: format(MSG, prefer: alias_method, current: method_name),
            severity: :warning
          ) do |corrector|
            corrector.replace(node.loc.selector, alias_method)
          end
        end

        alias on_csend on_send
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rubocop-rails-2.12.3 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.12.2 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.12.1 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.12.0 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.11.3 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.11.2 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.11.1 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.11.0 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.10.1 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.10.0 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.9.1 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.9.0 lib/rubocop/cop/rails/active_record_aliases.rb