Skip to content

Commit

Permalink
Merge pull request #409 from mobilutz/patch-2
Browse files Browse the repository at this point in the history
`Rails/WhereNot` add table/column split
  • Loading branch information
koic authored Dec 18, 2020
2 parents 39bd393 + b330fb5 commit 6af302b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docs/modules/ROOT/pages/cops_rails.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/rubocop/cop/rails/where_not.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions spec/rubocop/cop/rails/where_not_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6af302b

Please sign in to comment.