Skip to content

Commit

Permalink
Rails/WhereEquals add table/column split
Browse files Browse the repository at this point in the history
  • Loading branch information
mobilutz committed Dec 14, 2020
1 parent 0728968 commit 8e4425c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
### Changes

* [#404](https://github.com/rubocop-hq/rubocop-rails/issues/404): Make `Rails/HelperInstanceVariable` accepts of instance variables when a class which inherits `ActionView::Helpers::FormBuilder`. ([@koic][])
* [#406](https://github.com/rubocop-hq/rubocop-rails/pull/406): Deconstruct "table.column" in `Rails/WhereEquals`. ([@mobilutz][])

## 2.9.0 (2020-12-09)

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 @@ -4448,11 +4448,13 @@ User.where('name = :name', name: 'Gabe')
User.where('name IS NULL')
User.where('name IN (?)', ['john', 'jane'])
User.where('name IN (:names)', names: ['john', 'jane'])
User.where('users.name = :name', name: 'Gabe')
# good
User.where(name: 'Gabe')
User.where(name: nil)
User.where(name: ['john', 'jane'])
User.where(users: { name: 'Gabe' })
----

=== References
Expand Down
6 changes: 5 additions & 1 deletion lib/rubocop/cop/rails/where_equals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ module Rails
# User.where('name IS NULL')
# User.where('name IN (?)', ['john', 'jane'])
# User.where('name IN (:names)', names: ['john', 'jane'])
# User.where('users.name = :name', name: 'Gabe')
#
# # good
# User.where(name: 'Gabe')
# User.where(name: nil)
# User.where(name: ['john', 'jane'])
# User.where(users: { name: 'Gabe' })
class WhereEquals < Base
include RangeHelp
extend AutoCorrector
Expand Down Expand Up @@ -83,7 +85,9 @@ def extract_column_and_value(template_node, value_node)

def build_good_method(column, value)
if column.include?('.')
"where('#{column}' => #{value})"
table, column = column.split('.')

"where(#{table}: { #{column}: #{value} })"
else
"where(#{column}: #{value})"
end
Expand Down
8 changes: 4 additions & 4 deletions spec/rubocop/cop/rails/where_equals_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@
it 'registers an offense and corrects when using `=` and namespaced columns' do
expect_offense(<<~RUBY)
Course.where('enrollments.student_id = ?', student.id)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where('enrollments.student_id' => student.id)` instead of manually constructing SQL.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where(enrollments: { student_id: student.id })` instead of manually constructing SQL.
RUBY

expect_correction(<<~RUBY)
Course.where('enrollments.student_id' => student.id)
Course.where(enrollments: { student_id: student.id })
RUBY
end

Expand Down Expand Up @@ -128,11 +128,11 @@
it 'registers an offense and corrects when using `=` and namespaced columns' do
expect_offense(<<~RUBY)
Course.where(['enrollments.student_id = ?', student.id])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where('enrollments.student_id' => student.id)` instead of manually constructing SQL.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where(enrollments: { student_id: student.id })` instead of manually constructing SQL.
RUBY

expect_correction(<<~RUBY)
Course.where('enrollments.student_id' => student.id)
Course.where(enrollments: { student_id: student.id })
RUBY
end
end
Expand Down

0 comments on commit 8e4425c

Please sign in to comment.