Sha256: 0dc67046bea6aeba13a42fce1ee51475888179f3f1e3b039f85c8c60cdbb6a61

Contents?: true

Size: 1.28 KB

Versions: 13

Compression:

Stored size: 1.28 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.
      #
      # @safety
      #   This cop is unsafe because custom `update_attributes` method call was changed to
      #   `update` but the method name remained same in the method definition.
      #
      # @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)
          return if node.arguments.empty?

          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

13 entries across 13 versions & 4 rubygems

Version Path
rubocop-rails-2.27.0 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.26.2 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.26.1 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.26.0 lib/rubocop/cop/rails/active_record_aliases.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.25.1/lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.25.1 lib/rubocop/cop/rails/active_record_aliases.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.25.0/lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.24.1 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.24.0 lib/rubocop/cop/rails/active_record_aliases.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.23.1/lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.23.1 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.23.0 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.22.2 lib/rubocop/cop/rails/active_record_aliases.rb