Skip to content

Commit

Permalink
Fix an error for Rails/FindBy
Browse files Browse the repository at this point in the history
Follow up rubocop#506 (comment).

This PR fixes an error for `Rails/FindBy` when calling `take` after block.
  • Loading branch information
koic committed Jun 22, 2021
1 parent 6986c02 commit 1407795
Show file tree
Hide file tree
Showing 3 changed files with 11 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

* [#507](https://github.com/rubocop/rubocop-rails/pull/507): Fix an error for `Rails/FindBy` when calling `take` after block. ([@koic][])

## 2.11.0 (2021-06-21)

### New features
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/find_by.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class FindBy < Base
RESTRICT_ON_SEND = %i[first take].freeze

def on_send(node)
return if ignore_where_first? && node.method?(:first)
return if node.receiver.block_type? || ignore_where_first? && node.method?(:first)

range = range_between(node.receiver.loc.selector.begin_pos, node.loc.selector.end_pos)

Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/rails/find_by_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
expect_no_offenses('User.find_by(id: x)')
end

it 'does not register an offense when calling `take` after block' do
expect_no_offenses(<<~RUBY)
do_something {}.take(5)
RUBY
end

context 'when `IgnoreWhereFirst: true' do
let(:cop_config) do
{ 'IgnoreWhereFirst' => true }
Expand Down

0 comments on commit 1407795

Please sign in to comment.