Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an error for Rails/FindEach #573

Merged
merged 1 commit into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_rails_find_each.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#573](https://github.com/rubocop/rubocop-rails/pull/573): Fix an error for `Rails/FindEach` when using `where` with no receiver. ([@koic][])
2 changes: 2 additions & 0 deletions lib/rubocop/cop/rails/find_each.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def active_model_error_where?(node)
end

def active_model_error?(node)
return false if node.nil?

node.send_type? && node.method?(:errors)
end
end
Expand Down
13 changes: 12 additions & 1 deletion spec/rubocop/cop/rails/find_each_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,18 @@
# Active Model Errors slice from the new query interface introduced in Rails 6.1.
it 'does not register an offense when using `model.errors.where`' do
expect_no_offenses(<<~RUBY)
model.errors.where(:title).each { |error| do_something(error) }
class Model < ApplicationRecord
model.errors.where(:title).each { |error| do_something(error) }
end
RUBY
end

it 'registers an offense when using `where` with no receiver' do
expect_offense(<<~RUBY)
class Model < ApplicationRecord
where(record: [record1, record2]).each(&:touch)
^^^^ Use `find_each` instead of `each`.
end
RUBY
end

Expand Down