Rubocop: FindEach cop should not be active with Mongoid

Created on 2 May 2018  路  5Comments  路  Source: rubocop-hq/rubocop

There is a dependency with ActiveRecord. Class RuboCop::Cop::FindEach should be aware not to be active when Mongoid is the ORM, because the method find_each does not exist in class Mongoid::Criteria


Expected behaviour

When Mongoid is setting up as ORM, and the method each is used in a Mongoid::Criteria object, FindEach cop should not be triggered because method find_each does not exist in Mongoid::Critera.

Actual behavior

If you are using Mongoid and you have code like this:

User.where(name: 'John Doe').each do |user|
  # any code
end

FindEach cop is active and it gives the following message:

Rails/FindEach: Use find_each instead of each.

Steps to reproduce the problem

Use Mongoid as ORM and use the method each in a Mongoid::Criteria, for example:

User.where(name: 'John Doe').each do |user|
  # any code
end

RuboCop version

$ rubocop -V
0.54.0 (using Parser 2.5.0.5, running on ruby 2.3.0 x86_64-darwin16)

Most helpful comment

Once the Rails cops are extracted into their own gem, I guess an ActiveRecord department might make sense, since Rails is technically not a single library.

All 5 comments

Rubocop can't know which ORM is behind this code:

User.where(name: 'John Doe').each do |user|
  # any code
end

I suppose this cop could inspect the Gemfile to see which ORM is installed. What if both Mongoid and ActiveRecord are found? 馃

I think in such cases users can simply disable it, as I'm not sure it's worth making the code more complex for something that basic.

I know that to disable this option it's the simplest option. By default, this cop inspects the model folder, so in case that there is a include Mongoid::Document not to create the offence.
But again, I'm aware that the simplest option is to disable it.

Once the Rails cops are extracted into their own gem, I guess an ActiveRecord department might make sense, since Rails is technically not a single library.

Yeah, feel free to move this over to https://github.com/rubocop-hq/rubocop-rails.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kirrmann picture kirrmann  路  3Comments

AndreiMotinga picture AndreiMotinga  路  3Comments

benoittgt picture benoittgt  路  3Comments

bbatsov picture bbatsov  路  3Comments

ecbrodie picture ecbrodie  路  3Comments