Skip to content

Commit

Permalink
Merge pull request #507 from koic/fix_error_for_rails_find_by
Browse files Browse the repository at this point in the history
Fix an error for `Rails/FindBy`
  • Loading branch information
koic authored Jun 22, 2021
2 parents 6986c02 + 1407795 commit 8838e3c
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 8838e3c

Please sign in to comment.