Sha256: 60a9a9e2f6b46fc24afc1a36a422dccfd4eb00fb68beac9b0b1f99dbe5c16fa5

Contents?: true

Size: 1.15 KB

Versions: 22

Compression:

Stored size: 1.15 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 < Cop
        MSG = 'Use `%<prefer>s` instead of `%<current>s`.'

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

        def on_send(node)
          ALIASES.each do |bad, good|
            next unless node.method?(bad)

            add_offense(node,
                        message: format(MSG, prefer: good, current: bad),
                        location: :selector,
                        severity: :warning)
            break
          end
        end

        alias on_csend on_send

        def autocorrect(node)
          lambda do |corrector|
            corrector.replace(
              node.loc.selector,
              ALIASES[node.method_name].to_s
            )
          end
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
rubocop-rails-2.8.1 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.8.0 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.7.1 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.7.0 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.6.0 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.5.2 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.5.1 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.5.0 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.4.2 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.4.1 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.4.0 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.3.2 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.3.1 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.3.0 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.2.1 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.2.0 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.1.0 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.0.1 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-0.71.0 lib/rubocop/cop/rails/active_record_aliases.rb
rubocop-rails-2.0.0 lib/rubocop/cop/rails/active_record_aliases.rb