Skip to content

Commit

Permalink
Add scope methods to Rails/FindEach cop
Browse files Browse the repository at this point in the history
Makes the cop also check for the following scopes: `eager_load`,
`includes`, `joins`, `left_joins`, `left_outer_joins`, `preload`,
`references`, and `unscoped`.
  • Loading branch information
repinel committed Aug 7, 2018
1 parent 63cdaa2 commit 0f044af
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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)
Expand Down Expand Up @@ -3507,3 +3508,4 @@
[@nijikon]: https://github.com/nijikon
[@mikeyhew]: https://github.com/mikeyhew
[@mkenyon]: https://github.com/mkenyon
[@repinel]: https://github.com/repinel
5 changes: 4 additions & 1 deletion lib/rubocop/cop/rails/find_each.rb
Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion spec/rubocop/cop/rails/find_each_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 0f044af

Please sign in to comment.