From 0f044afc4a59bc6ff13e478b28f7479473210e15 Mon Sep 17 00:00:00 2001 From: Roque Pinel <1685896+repinel@users.noreply.github.com> Date: Mon, 6 Aug 2018 16:53:55 -0400 Subject: [PATCH] Add scope methods to `Rails/FindEach` cop Makes the cop also check for the following scopes: `eager_load`, `includes`, `joins`, `left_joins`, `left_outer_joins`, `preload`, `references`, and `unscoped`. --- CHANGELOG.md | 2 ++ lib/rubocop/cop/rails/find_each.rb | 5 ++++- spec/rubocop/cop/rails/find_each_spec.rb | 10 +++++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a30cff33795d..7d60162549d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ ### Changes +* [#6161](https://github.com/rubocop-hq/rubocop/pull/6161): Add scope methods to `Rails/FindEach` cop. Makes the cop also check for the following scopes: `eager_load`, `includes`, `joins`, `left_joins`, `left_outer_joins`, `preload`, `references`, and `unscoped`. ([@repinel][]) * [#6137](https://github.com/rubocop-hq/rubocop/pull/6137): Allow `db` to allowed names of `Naming/UncommunicativeMethodParamName` cop in default config. ([@mkenyon][]) ## 0.58.2 (2018-07-23) @@ -3507,3 +3508,4 @@ [@nijikon]: https://github.com/nijikon [@mikeyhew]: https://github.com/mikeyhew [@mkenyon]: https://github.com/mkenyon +[@repinel]: https://github.com/repinel diff --git a/lib/rubocop/cop/rails/find_each.rb b/lib/rubocop/cop/rails/find_each.rb index d35fb2af4d86..3843070b9067 100644 --- a/lib/rubocop/cop/rails/find_each.rb +++ b/lib/rubocop/cop/rails/find_each.rb @@ -15,7 +15,10 @@ module Rails class FindEach < Cop MSG = 'Use `find_each` instead of `each`.'.freeze - SCOPE_METHODS = %i[all where not].freeze + SCOPE_METHODS = %i[ + all eager_load includes joins left_joins left_outer_joins not preload + references unscoped where + ].freeze IGNORED_METHODS = %i[order limit select].freeze def on_send(node) diff --git a/spec/rubocop/cop/rails/find_each_spec.rb b/spec/rubocop/cop/rails/find_each_spec.rb index add2beb1f1ac..bac57a76b4c8 100644 --- a/spec/rubocop/cop/rails/find_each_spec.rb +++ b/spec/rubocop/cop/rails/find_each_spec.rb @@ -30,8 +30,16 @@ end end - it_behaves_like('register_offense', 'where(name: name)') it_behaves_like('register_offense', 'all') + it_behaves_like('register_offense', 'eager_load(:association_name)') + it_behaves_like('register_offense', 'includes(:association_name)') + it_behaves_like('register_offense', 'joins(:association_name)') + it_behaves_like('register_offense', 'left_joins(:association_name)') + it_behaves_like('register_offense', 'left_outer_joins(:association_name)') + it_behaves_like('register_offense', 'preload(:association_name)') + it_behaves_like('register_offense', 'references(:association_name)') + it_behaves_like('register_offense', 'unscoped') + it_behaves_like('register_offense', 'where(name: name)') it_behaves_like('register_offense', 'where.not(name: name)') it 'does not register an offense when using find_by' do