Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #504] Rails/FindBy being wrongly triggered on non Active Record methods #506

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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