Skip to content

Commit

Permalink
[Fix #401] Fix an error for Rails/WhereEquals
Browse files Browse the repository at this point in the history
Fixes #401.

This PR fixes an error for `Rails/WhereEquals` when
using only named placeholder template without replacement argument.
  • Loading branch information
koic committed Dec 12, 2020
1 parent abbb4fa commit 3eecd98
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
### Bug fixes

* [#408](https://github.com/rubocop-hq/rubocop-rails/pull/408): Fix bug in `Rails/FindEach` where config was ignored. ([@ghiculescu][])
* [#401](https://github.com/rubocop-hq/rubocop-rails/issues/401): Fix an error for `Rails/WhereEquals` using only named placeholder template without replacement argument. ([@koic][])

## 2.9.0 (2020-12-09)

2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/where_equals.rb
Original file line number Diff line number Diff line change
@@ -68,7 +68,7 @@ def extract_column_and_value(template_node, value_node)
when EQ_ANONYMOUS_RE, IN_ANONYMOUS_RE
value_node.source
when EQ_NAMED_RE, IN_NAMED_RE
return unless value_node.hash_type?
return unless value_node&.hash_type?

pair = value_node.pairs.find { |p| p.key.value.to_sym == Regexp.last_match(2).to_sym }
pair.value.source
8 changes: 8 additions & 0 deletions spec/rubocop/cop/rails/where_equals_spec.rb
Original file line number Diff line number Diff line change
@@ -160,4 +160,12 @@
User.where('name = ? AND age = ?', 'john', 19)
RUBY
end

it 'does not register an offense when using only named placeholder template without replacement argument' do
expect_no_offenses(<<~'RUBY')
sql = User.where('name = :name').select(:id).to_sql
User.where("id IN (#{sql})", name: 'Lastname').first
RUBY
end
end

0 comments on commit 3eecd98

Please sign in to comment.