Merge pull request #12289 from anthonyms/11482-fix-rubocop-rails-issue-find_each

Fix Rubocop Rails issue: Rails/FindEach
This commit is contained in:
David Cook
2024-03-21 09:36:18 +11:00
committed by GitHub
9 changed files with 14 additions and 21 deletions

View File

@@ -390,21 +390,6 @@ Naming/VariableNumber:
- 'spec/models/spree/tax_rate_spec.rb'
- 'spec/requests/api/orders_spec.rb'
# Offense count: 11
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: AllowedMethods, AllowedPatterns.
# AllowedMethods: order, limit, select, lock
Rails/FindEach:
Exclude:
- 'app/controllers/admin/order_cycles_controller.rb'
- 'app/jobs/subscription_confirm_job.rb'
- 'app/services/orders/bulk_cancel_service.rb'
- 'app/services/products_renderer.rb'
- 'lib/tasks/data.rake'
- 'lib/tasks/subscriptions/debug.rake'
- 'spec/system/admin/bulk_order_management_spec.rb'
- 'spec/system/admin/enterprise_relationships_spec.rb'
# Offense count: 11
# Configuration parameters: Include.
# Include: app/models/**/*.rb

View File

@@ -100,7 +100,7 @@ module Admin
def update_nil_subscription_line_items_price_estimate(order_cycle)
order_cycle.schedules.each do |schedule|
Subscription.where(schedule_id: schedule.id).each do |subscription|
Subscription.where(schedule_id: schedule.id).find_each do |subscription|
shop = Enterprise.managed_by(spree_current_user).find_by(id: subscription.shop_id)
fee_calculator = OpenFoodNetwork::EnterpriseFeeCalculator.new(shop, order_cycle)
subscription.subscription_line_items.nil_price_estimate.each do |line_item|

View File

@@ -23,7 +23,7 @@ class SubscriptionConfirmJob < ApplicationJob
unconfirmed_proxy_orders.update_all(confirmed_at: Time.zone.now)
# Confirm these proxy orders
ProxyOrder.where(id: unconfirmed_proxy_orders_ids).each do |proxy_order|
ProxyOrder.where(id: unconfirmed_proxy_orders_ids).find_each do |proxy_order|
JobLogger.logger.info "Confirming Order for Proxy Order #{proxy_order.id}"
confirm_order!(proxy_order.order)
end

View File

@@ -10,11 +10,13 @@ module Orders
end
def call
# rubocop:disable Rails/FindEach # .each returns an array, .find_each returns nil
editable_orders.where(id: @order_ids).each do |order|
order.send_cancellation_email = @send_cancellation_email
order.restock_items = @restock_items
order.cancel
end
# rubocop:enable Rails/FindEach
end
private

View File

@@ -89,10 +89,12 @@ class ProductsRenderer
@variants_for_shop ||= begin
scoper = OpenFoodNetwork::ScopeVariantToHub.new(distributor)
# rubocop:disable Rails/FindEach # .each returns an array, .find_each returns nil
distributed_products.variants_relation.
includes(:default_price, :stock_locations, :product).
where(product_id: products).
each { |v| scoper.scope(v) } # Scope results with variant_overrides
# rubocop:enable Rails/FindEach
end
end

View File

@@ -7,7 +7,7 @@ namespace :ofn do
input = request_months
# For each order cycle which was modified within the past 3 months
OrderCycle.where('updated_at > ?', Date.current - input.months).each do |order_cycle|
OrderCycle.where('updated_at > ?', Date.current - input.months).find_each do |order_cycle|
# Cycle through the incoming exchanges
order_cycle.exchanges.incoming.each do |exchange|
next if exchange.sender == exchange.receiver

View File

@@ -12,7 +12,7 @@ namespace :ofn do
puts "Order Cycle #{order_cycle.name}"
order_cycle.schedules.each do |schedule|
puts "Schedule #{schedule.name}"
Subscription.where(schedule_id: schedule.id).each do |subscription|
Subscription.where(schedule_id: schedule.id).find_each do |subscription|
puts
puts "Subscription #{subscription.id}"
puts subscription.shop.name
@@ -23,7 +23,7 @@ namespace :ofn do
puts "Canceled at #{subscription.canceled_at} and paused at #{subscription.paused_at}"
ProxyOrder.where(order_cycle_id:,
subscription_id: subscription.id).each do |proxy_order|
subscription_id: subscription.id).find_each do |proxy_order|
puts
puts "Proxy Order #{proxy_order.id}"
puts "Canceled at #{proxy_order.canceled_at}"
@@ -42,7 +42,7 @@ namespace :ofn do
puts "Source #{payment.source.to_json}"
end
Spree::LogEntry.where(source_type: "Spree::Payment",
source_id: payment.id).each do |log_entry|
source_id: payment.id).find_each do |log_entry|
puts "Log Entries found"
puts log_entry.details
end

View File

@@ -921,6 +921,7 @@ describe '
expect(page).to have_selector "tr#li_#{li2.id} input[type='checkbox'][name='bulk']"
end
# rubocop:disable Rails/FindEach. # These are Capybara finders
it "displays a checkbox to which toggles the 'checked' state of all checkboxes" do
check "toggle_bulk"
page.all("input[type='checkbox'][name='bulk']").each{ |checkbox|
@@ -931,6 +932,7 @@ describe '
expect(checkbox.checked?).to be false
}
end
# rubocop:enable Rails/FindEach
it "displays a bulk action select box with a list of actions" do
list_of_actions = ['Delete Selected']

View File

@@ -138,6 +138,7 @@ create(:enterprise)
end
end
# rubocop:disable Rails/FindEach. # These are Capybara finders
def find_relationship(parent, child)
page.all('tr').each do |tr|
return tr if tr.find('td:first-child').text == parent.name &&
@@ -146,4 +147,5 @@ create(:enterprise)
end
raise "relationship not found"
end
# rubocop:enable Rails/FindEach.
end