diff --git a/CHANGELOG.md b/CHANGELOG.md index 914c6bc5bd..593166ddca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## master (unreleased) +### Changes + +* [#409](https://github.com/rubocop-hq/rubocop-rails/pull/409): Deconstruct "table.column" in `Rails/WhereNot`. ([@mobilutz][]) + ## 2.9.1 (2020-12-16) ### Bug fixes diff --git a/docs/modules/ROOT/pages/cops_rails.adoc b/docs/modules/ROOT/pages/cops_rails.adoc index 5426eb6d81..bed5d99415 100644 --- a/docs/modules/ROOT/pages/cops_rails.adoc +++ b/docs/modules/ROOT/pages/cops_rails.adoc @@ -4554,11 +4554,13 @@ User.where('name <> :name', name: 'Gabe') User.where('name IS NOT NULL') User.where('name NOT IN (?)', ['john', 'jane']) User.where('name NOT IN (:names)', names: ['john', 'jane']) +User.where('users.name != :name', name: 'Gabe') # good User.where.not(name: 'Gabe') User.where.not(name: nil) User.where.not(name: ['john', 'jane']) +User.where.not(users: { name: 'Gabe' }) ---- === References diff --git a/lib/rubocop/cop/rails/where_not.rb b/lib/rubocop/cop/rails/where_not.rb index 998618bed2..f50f4c01a4 100644 --- a/lib/rubocop/cop/rails/where_not.rb +++ b/lib/rubocop/cop/rails/where_not.rb @@ -15,11 +15,13 @@ module Rails # User.where('name IS NOT NULL') # User.where('name NOT IN (?)', ['john', 'jane']) # User.where('name NOT IN (:names)', names: ['john', 'jane']) + # User.where('users.name != :name', name: 'Gabe') # # # good # User.where.not(name: 'Gabe') # User.where.not(name: nil) # User.where.not(name: ['john', 'jane']) + # User.where.not(users: { name: 'Gabe' }) # class WhereNot < Base include RangeHelp @@ -86,7 +88,9 @@ def extract_column_and_value(template_node, value_node) def build_good_method(column, value) if column.include?('.') - "where.not('#{column}' => #{value})" + table, column = column.split('.') + + "where.not(#{table}: { #{column}: #{value} })" else "where.not(#{column}: #{value})" end diff --git a/spec/rubocop/cop/rails/where_not_spec.rb b/spec/rubocop/cop/rails/where_not_spec.rb index 14ebc45f73..bcaa67151e 100644 --- a/spec/rubocop/cop/rails/where_not_spec.rb +++ b/spec/rubocop/cop/rails/where_not_spec.rb @@ -83,22 +83,22 @@ it 'registers an offense and corrects when using `!=` and namespaced columns' do expect_offense(<<~RUBY) Course.where('enrollments.student_id != ?', student.id) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where.not('enrollments.student_id' => student.id)` instead of manually constructing negated SQL in `where`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where.not(enrollments: { student_id: student.id })` instead of manually constructing negated SQL in `where`. RUBY expect_correction(<<~RUBY) - Course.where.not('enrollments.student_id' => student.id) + Course.where.not(enrollments: { student_id: student.id }) RUBY end it 'registers an offense and corrects when using `<>` and namespaced columns' do expect_offense(<<~RUBY) Course.where('enrollments.student_id <> ?', student.id) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where.not('enrollments.student_id' => student.id)` instead of manually constructing negated SQL in `where`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where.not(enrollments: { student_id: student.id })` instead of manually constructing negated SQL in `where`. RUBY expect_correction(<<~RUBY) - Course.where.not('enrollments.student_id' => student.id) + Course.where.not(enrollments: { student_id: student.id }) RUBY end @@ -183,22 +183,22 @@ it 'registers an offense and corrects when using `!=` and namespaced columns' do expect_offense(<<~RUBY) Course.where(['enrollments.student_id != ?', student.id]) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where.not('enrollments.student_id' => student.id)` instead of manually constructing negated SQL in `where`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where.not(enrollments: { student_id: student.id })` instead of manually constructing negated SQL in `where`. RUBY expect_correction(<<~RUBY) - Course.where.not('enrollments.student_id' => student.id) + Course.where.not(enrollments: { student_id: student.id }) RUBY end it 'registers an offense and corrects when using `<>` and namespaced columns' do expect_offense(<<~RUBY) Course.where(['enrollments.student_id <> ?', student.id]) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where.not('enrollments.student_id' => student.id)` instead of manually constructing negated SQL in `where`. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where.not(enrollments: { student_id: student.id })` instead of manually constructing negated SQL in `where`. RUBY expect_correction(<<~RUBY) - Course.where.not('enrollments.student_id' => student.id) + Course.where.not(enrollments: { student_id: student.id }) RUBY end end