Skip to content

Fix Rubocop Rails issue: Rails/FindEach#12289

Merged
dacook merged 1 commit intoopenfoodfoundation:masterfrom
anthonyms:11482-fix-rubocop-rails-issue-find_each
Mar 20, 2024
Merged

Fix Rubocop Rails issue: Rails/FindEach#12289
dacook merged 1 commit intoopenfoodfoundation:masterfrom
anthonyms:11482-fix-rubocop-rails-issue-find_each

Conversation

@anthonyms
Copy link
Copy Markdown
Contributor

@anthonyms anthonyms commented Mar 19, 2024

What? Why?

Closes #12290

Fix all violations of the Rails/FindEach cop.

Rubocop recommends using find_each in place of each whenever the call is chained to a where, all or not.

This PR is one of many PR's addressing issue #11482.

What should we test?

N/A

Release notes

Fix all violations of the Rails/FindEach cop.

Changelog Category (reviewers may add a label for the release notes):

  • User facing changes
  • API changes (V0, V1, DFC or Webhook)
  • Technical changes only
  • Feature toggled

Dependencies

N/A

Documentation updates

N/A

@sigmundpetersen sigmundpetersen linked an issue Mar 19, 2024 that may be closed by this pull request
@anthonyms anthonyms force-pushed the 11482-fix-rubocop-rails-issue-find_each branch from 5cf049a to f173575 Compare March 19, 2024 14:16
Copy link
Copy Markdown
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one ! Thanks for your help 🙏

@rioug rioug added the technical changes only These pull requests do not contain user facing changes and are grouped in release notes label Mar 19, 2024
Copy link
Copy Markdown
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

I have a comment about a couple of the changes, which I think need changing.

Comment on lines +13 to +20
cancelled_orders = []
editable_orders.where(id: @order_ids).find_each do |order|
order.send_cancellation_email = @send_cancellation_email
order.restock_items = @restock_items
order.cancel
cancelled_orders << order
end
cancelled_orders
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me a few minutes to work out why this was needed: because each returns the array of records, but find_each doesn't retain an array, to avoid taking up too much memory.

Hmm, so then if we need to store the records in an array, there's no reason to use find_each here!

± Could you please change this one back to each with a rubocop:disable?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theres some (little) benefit in retaining the find_each since the processing will be done in batches. The cancelled_orders array will expand gradually rather than suddenly.

The little benefit is negated by the lack of readability.

Will use a rubocop:disable for this one.

Comment thread app/services/products_renderer.rb Outdated
Comment on lines +92 to +99
scoped_variants = []

distributed_products.variants_relation.
includes(:default_price, :stock_locations, :product).
where(product_id: products).
each { |v| scoper.scope(v) } # Scope results with variant_overrides
find_each { |v| scoped_variants << scoper.scope(v) } # Scope results with variant_overrides

scoped_variants
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

± same as for other comment.

@anthonyms anthonyms force-pushed the 11482-fix-rubocop-rails-issue-find_each branch from f173575 to 25e3f30 Compare March 20, 2024 12:34
@anthonyms anthonyms requested a review from dacook March 20, 2024 12:43
Copy link
Copy Markdown
Member

@mkllnk mkllnk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thank you.

Copy link
Copy Markdown
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, and bonus points for comments explaining why! 💯

@dacook dacook merged commit 579965c into openfoodfoundation:master Mar 20, 2024
@anthonyms anthonyms deleted the 11482-fix-rubocop-rails-issue-find_each branch March 21, 2024 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

technical changes only These pull requests do not contain user facing changes and are grouped in release notes

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Rubocop Rails/FindEach

4 participants