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
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.
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.
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 -V
0.54.0 (using Parser 2.5.0.5, running on ruby 2.3.0 x86_64-darwin16)
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.
Most helpful comment
Once the Rails cops are extracted into their own gem, I guess an
ActiveRecorddepartment might make sense, since Rails is technically not a single library.