diff --git a/CHANGELOG.md b/CHANGELOG.md index 13d779fe58..be050df761 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/rails/find_each.rb b/lib/rubocop/cop/rails/find_each.rb index b34d3556b7..c5ef88a785 100644 --- a/lib/rubocop/cop/rails/find_each.rb +++ b/lib/rubocop/cop/rails/find_each.rb @@ -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| @@ -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 diff --git a/spec/rubocop/cop/rails/find_each_spec.rb b/spec/rubocop/cop/rails/find_each_spec.rb index 51ac8e57f4..9e23297a1f 100644 --- a/spec/rubocop/cop/rails/find_each_spec.rb +++ b/spec/rubocop/cop/rails/find_each_spec.rb @@ -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