Class: RuboCop::Cop::RailsDeprecation::WhereNot

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rails_deprecation/where_not.rb

Overview

This cop identifies passing Hash with multiple elements to ‘where.not`.

Examples:


# bad
where.not(key1: value1, key2: value2)

# good
where.not(key1: value1).where.not(key2: value2)

# good
where.not(key1: value1)

Constant Summary collapse

MSG =
'Use `where.not(key1: value1).where.not(key2: value2)` instead of `where.not(key1: value1, key2: value2)`.'

Constants inherited from Base

Base::DEFAULT_MAXIMUM_TARGET_RAILS_VERSION, Base::DEFAULT_MINIMUM_TARGET_RAILS_VERSION

Instance Method Summary collapse

Methods inherited from Base

support_target_rails_version?

Instance Method Details

#on_send(node) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rubocop/cop/rails_deprecation/where_not.rb', line 34

def on_send(node)
  return unless where_not_with_multiple_elements_hash?(node)

  add_offense(node) do |corrector|
    pairs = node.children[2].children
    last_end_pos = pairs[0].location.expression.end_pos
    pairs[1..].each do |pair|
      corrector.remove(pair.location.expression.with(begin_pos: last_end_pos))
      last_end_pos = pair.location.expression.end_pos
      corrector.insert_after(node.location.expression, ".where.not(#{pair.source})")
    end
  end
end