Skip to content

Commit

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

This PR fixes an error for `Rails/WhereEquals` when
using only named placeholder template without replacement argument.
  • Loading branch information
koic committed Dec 10, 2020
1 parent 209e963 commit 7bfa1f6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
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)

### Bug fixes

* [#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)

### New features
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/where_equals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/rails/where_equals_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 7bfa1f6

Please sign in to comment.