Skip to content

Commit

Permalink
Merge pull request #506 from nvasilevski/nvasilevski/fix-find-by-cop-…
Browse files Browse the repository at this point in the history
…triggering-on-regular-arrays

[Fix #504] Rails/FindBy being wrongly triggered on non Active Record methods
  • Loading branch information
koic authored Jun 23, 2021
2 parents a3d93ae + c58e226 commit ae0507b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/rubocop/cop/rails/find_by.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ class FindBy < Base
RESTRICT_ON_SEND = %i[first take].freeze

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

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

Expand Down
20 changes: 20 additions & 0 deletions spec/rubocop/cop/rails/find_by_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,24 @@
expect_no_corrections
end
end

context 'when receiver is not an Active Record' do
context 'when method is Array#take' do
it 'does not register an offence' do
expect_no_offenses(<<~RUBY)
array = Array.new(1) { rand }
array.compact.take
RUBY
end
end

context 'when method is Array#first' do
it 'does not register an offence' do
expect_no_offenses(<<~RUBY)
array = Array.new(1) { rand }
array.compact.first
RUBY
end
end
end
end

0 comments on commit ae0507b

Please sign in to comment.