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 bug in Rails/FindEach #408

Merged
merged 1 commit into from
Dec 12, 2020
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
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

* [#408](https://github.com/rubocop-hq/rubocop-rails/pull/408): Fix bug in `Rails/FindEach` where config was ignored. ([@ghiculescu][])

## 2.9.0 (2020-12-09)

### New features
Expand Down
11 changes: 4 additions & 7 deletions lib/rubocop/cop/rails/find_each.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class FindEach < Base
def on_send(node)
return unless node.receiver&.send_type?
return unless SCOPE_METHODS.include?(node.receiver.method_name)
return if method_chain(node).any? { |m| ignored?(m) }
return if ignored?(node)

range = node.loc.selector
add_offense(range) do |corrector|
Expand All @@ -40,12 +40,9 @@ def on_send(node)

private

def method_chain(node)
node.each_node(:send).map(&:method_name)
end

def ignored?(relation_method)
cop_config['IgnoredMethods'].include?(relation_method)
def ignored?(node)
method_chain = node.each_node(:send).map(&:method_name)
(cop_config['IgnoredMethods'].map(&:to_sym) & method_chain).any?
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/rubocop/cop/rails/find_each_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ class C; User.all.find_each { |u| u.x }; end
expect_no_offenses('User.order(:name).each { |u| u.something }')
end

it 'does not register an offense when using order(...) chained with other things' do
expect_no_offenses('User.order(:name).includes(:company).each { |u| u.something }')
end

it 'does not register an offense when using lock earlier' do
expect_no_offenses('User.lock.each { |u| u.something }')
end
Expand Down