From bf05c6595adf5d197ac9a7e9753c1694d11b5151 Mon Sep 17 00:00:00 2001 From: Elia Schito Date: Wed, 13 Sep 2023 10:05:05 +0200 Subject: [PATCH 1/2] Update all find_each "violations" --- .rubocop.yml | 4 ++++ api/spec/requests/spree/api/checkouts_spec.rb | 2 +- backend/spec/features/admin/orders/order_details_spec.rb | 2 +- core/app/models/spree/reimbursement_tax_calculator.rb | 2 +- core/app/models/spree/stock_location.rb | 2 +- core/app/models/spree/variant.rb | 2 +- core/db/default/spree/states.rb | 2 +- ...80710170104_create_spree_store_credit_reasons_table.rb | 4 ++-- ...6_set_promotions_with_any_policy_to_all_if_possible.rb | 2 +- core/lib/spree/core/controller_helpers/order.rb | 2 +- core/spec/models/spree/exchange_spec.rb | 2 +- core/spec/models/spree/stock/estimator_spec.rb | 8 ++++---- .../spec/models/spree/variant/vat_price_generator_spec.rb | 2 +- sample/db/samples/product_option_types.rb | 2 +- sample/db/samples/stock.rb | 2 +- 15 files changed, 22 insertions(+), 18 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index a5e9d417242..770b8947b96 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -339,3 +339,7 @@ Lint/MissingSuper: Exclude: - 'core/lib/spree/deprecation.rb' # this is a known class that doesn't require super - 'core/lib/spree/preferences/configuration.rb' # this class has no superclass defining `self.inherited` + +Rails/FindEach: + Exclude: + - 'db/migrate/**/*' diff --git a/api/spec/requests/spree/api/checkouts_spec.rb b/api/spec/requests/spree/api/checkouts_spec.rb index 7edf722e26c..193db21ad59 100644 --- a/api/spec/requests/spree/api/checkouts_spec.rb +++ b/api/spec/requests/spree/api/checkouts_spec.rb @@ -101,7 +101,7 @@ module Spree::Api # Regression Spec for https://github.com/spree/spree/issues/5389 and https://github.com/spree/spree/issues/5880 it "can update addresses but not transition to delivery w/o shipping setup" do - Spree::ShippingMethod.all.each(&:destroy) + Spree::ShippingMethod.all.find_each(&:destroy) put spree.api_checkout_path(order), params: { order_token: order.guest_token, order: { bill_address_attributes: address, diff --git a/backend/spec/features/admin/orders/order_details_spec.rb b/backend/spec/features/admin/orders/order_details_spec.rb index 5abc6b66fcb..459a4c5e94c 100644 --- a/backend/spec/features/admin/orders/order_details_spec.rb +++ b/backend/spec/features/admin/orders/order_details_spec.rb @@ -90,7 +90,7 @@ product.stock_items.where.not(stock_location_id: stock_location_ids).discard_all - product.stock_items.where(stock_location_id: stock_location_ids).each do |stock_item| + product.stock_items.where(stock_location_id: stock_location_ids).find_each do |stock_item| stock_item.set_count_on_hand 1 end diff --git a/core/app/models/spree/reimbursement_tax_calculator.rb b/core/app/models/spree/reimbursement_tax_calculator.rb index 854cde1ff1e..780c3972fc3 100644 --- a/core/app/models/spree/reimbursement_tax_calculator.rb +++ b/core/app/models/spree/reimbursement_tax_calculator.rb @@ -12,7 +12,7 @@ module Spree class ReimbursementTaxCalculator class << self def call(reimbursement) - reimbursement.return_items.includes(:inventory_unit).each do |return_item| + reimbursement.return_items.includes(:inventory_unit).find_each do |return_item| set_tax!(return_item) end end diff --git a/core/app/models/spree/stock_location.rb b/core/app/models/spree/stock_location.rb index 0a8915cef64..f3f6215d62d 100644 --- a/core/app/models/spree/stock_location.rb +++ b/core/app/models/spree/stock_location.rb @@ -121,7 +121,7 @@ def create_stock_items def ensure_one_default if default - Spree::StockLocation.where(default: true).where.not(id: id).each do |stock_location| + Spree::StockLocation.where(default: true).where.not(id: id).find_each do |stock_location| stock_location.default = false stock_location.save! end diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb index 3a9c8741e99..90ecf56b0cd 100644 --- a/core/app/models/spree/variant.rb +++ b/core/app/models/spree/variant.rb @@ -397,7 +397,7 @@ def set_cost_currency end def create_stock_items - StockLocation.where(propagate_all_variants: true).each do |stock_location| + StockLocation.where(propagate_all_variants: true).find_each do |stock_location| stock_location.propagate_variant(self) end end diff --git a/core/db/default/spree/states.rb b/core/db/default/spree/states.rb index dc8b130a16c..e4aaab0ad51 100644 --- a/core/db/default/spree/states.rb +++ b/core/db/default/spree/states.rb @@ -9,7 +9,7 @@ def create_states(subregions, country) end ActiveRecord::Base.transaction do - Spree::Country.all.each do |country| + Spree::Country.all.find_each do |country| carmen_country = Carmen::Country.coded(country.iso) next unless carmen_country.subregions? diff --git a/core/db/migrate/20180710170104_create_spree_store_credit_reasons_table.rb b/core/db/migrate/20180710170104_create_spree_store_credit_reasons_table.rb index cd50bd414fc..9d805df56f2 100644 --- a/core/db/migrate/20180710170104_create_spree_store_credit_reasons_table.rb +++ b/core/db/migrate/20180710170104_create_spree_store_credit_reasons_table.rb @@ -17,7 +17,7 @@ def up t.timestamps end - StoreCreditUpdateReason.all.each do |update_reason| + StoreCreditUpdateReason.all.find_each do |update_reason| StoreCreditReason.create!(name: update_reason.name) end @@ -45,7 +45,7 @@ def down end end - StoreCreditReason.all.each do |store_credit_reason| + StoreCreditReason.all.find_each do |store_credit_reason| StoreCreditUpdateReason.create!(name: store_credit_reason.name) end diff --git a/core/db/migrate/20220317165036_set_promotions_with_any_policy_to_all_if_possible.rb b/core/db/migrate/20220317165036_set_promotions_with_any_policy_to_all_if_possible.rb index a36bb5efe90..7c49f8fdac5 100644 --- a/core/db/migrate/20220317165036_set_promotions_with_any_policy_to_all_if_possible.rb +++ b/core/db/migrate/20220317165036_set_promotions_with_any_policy_to_all_if_possible.rb @@ -2,7 +2,7 @@ class SetPromotionsWithAnyPolicyToAllIfPossible < ActiveRecord::Migration[5.2] def up - Spree::Promotion.where(match_policy: :any).includes(:promotion_rules).all.each do |promotion| + Spree::Promotion.where(match_policy: :any).includes(:promotion_rules).all.find_each do |promotion| if promotion.promotion_rules.length <= 1 promotion.update!(match_policy: :all) else diff --git a/core/lib/spree/core/controller_helpers/order.rb b/core/lib/spree/core/controller_helpers/order.rb index d63bff9ecf8..e3bb2b61c69 100644 --- a/core/lib/spree/core/controller_helpers/order.rb +++ b/core/lib/spree/core/controller_helpers/order.rb @@ -45,7 +45,7 @@ def associate_user def set_current_order if spree_current_user && current_order - spree_current_user.orders.by_store(current_store).incomplete.where('id != ?', current_order.id).each do |order| + spree_current_user.orders.by_store(current_store).incomplete.where('id != ?', current_order.id).find_each do |order| current_order.merge!(order, spree_current_user) end end diff --git a/core/spec/models/spree/exchange_spec.rb b/core/spec/models/spree/exchange_spec.rb index e780330d68e..4c95f7572e7 100644 --- a/core/spec/models/spree/exchange_spec.rb +++ b/core/spec/models/spree/exchange_spec.rb @@ -54,7 +54,7 @@ module Spree context "when it cannot create shipments for all items" do before do - StockItem.where(variant_id: return_item.exchange_variant_id).each(&:destroy) + StockItem.where(variant_id: return_item.exchange_variant_id).find_each(&:destroy) end it 'raises an UnableToCreateShipments error' do diff --git a/core/spec/models/spree/stock/estimator_spec.rb b/core/spec/models/spree/stock/estimator_spec.rb index 9979fc4eff2..1d6d35c0ca7 100644 --- a/core/spec/models/spree/stock/estimator_spec.rb +++ b/core/spec/models/spree/stock/estimator_spec.rb @@ -85,7 +85,7 @@ module Stock end it "sorts shipping rates by cost" do - ShippingMethod.all.each(&:destroy) + ShippingMethod.all.find_each(&:destroy) create(:shipping_method, cost: 5) create(:shipping_method, cost: 3) create(:shipping_method, cost: 4) @@ -94,7 +94,7 @@ module Stock end context "general shipping methods" do - before { Spree::ShippingMethod.all.each(&:destroy) } + before { Spree::ShippingMethod.all.find_each(&:destroy) } context 'with a custom shipping calculator with no preference' do class Spree::Calculator::Shipping::NoPreferences < Spree::ShippingCalculator @@ -168,7 +168,7 @@ def compute_package(_package) end context "involves backend only shipping methods" do - before{ Spree::ShippingMethod.all.each(&:destroy) } + before{ Spree::ShippingMethod.all.find_each(&:destroy) } let!(:backend_method) { create(:shipping_method, available_to_users: false, cost: 0.00) } let!(:generic_method) { create(:shipping_method, cost: 5.00) } @@ -183,7 +183,7 @@ def compute_package(_package) end context "excludes shipping methods from other stores" do - before{ Spree::ShippingMethod.all.each(&:destroy) } + before{ Spree::ShippingMethod.all.find_each(&:destroy) } let!(:other_method) do create( diff --git a/core/spec/models/spree/variant/vat_price_generator_spec.rb b/core/spec/models/spree/variant/vat_price_generator_spec.rb index 2e15a020d13..b07e023186d 100644 --- a/core/spec/models/spree/variant/vat_price_generator_spec.rb +++ b/core/spec/models/spree/variant/vat_price_generator_spec.rb @@ -37,7 +37,7 @@ # We need to remove the price for FR from the database so it is created in memory, and then run VatPriceGenerator twice to trigger the duplicate price issue. it "will not build duplicate prices on multiple runs" do - variant.prices.where(country_iso: "FR").each(&:destroy) + variant.prices.where(country_iso: "FR").find_each(&:destroy) variant.reload described_class.new(variant).run expect { subject }.not_to change { variant.prices.size } diff --git a/sample/db/samples/product_option_types.rb b/sample/db/samples/product_option_types.rb index 6ebf087f6bb..7f5ac048260 100644 --- a/sample/db/samples/product_option_types.rb +++ b/sample/db/samples/product_option_types.rb @@ -9,7 +9,7 @@ "Solidus T-Shirt", "Solidus Long Sleeve", "Solidus Women's T-Shirt" ] -Spree::Product.all.each do |product| +Spree::Product.all.find_each do |product| product.option_types = [size] product.option_types << color if colored_clothes.include?(product.name) product.save! diff --git a/sample/db/samples/stock.rb b/sample/db/samples/stock.rb index e28fdbd4de4..2cc23eee5eb 100644 --- a/sample/db/samples/stock.rb +++ b/sample/db/samples/stock.rb @@ -7,7 +7,7 @@ location.active = true location.save! -Spree::Variant.all.each do |variant| +Spree::Variant.all.find_each do |variant| variant.stock_items.each do |stock_item| Spree::StockMovement.create(quantity: 10, stock_item: stock_item) end From d81437cf6d135970b3ec8c2483b2328fbb9febaa Mon Sep 17 00:00:00 2001 From: Elia Schito Date: Wed, 13 Sep 2023 10:20:15 +0200 Subject: [PATCH 2/2] Parse time using the current zone --- backend/spec/helpers/admin/base_helper_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/spec/helpers/admin/base_helper_spec.rb b/backend/spec/helpers/admin/base_helper_spec.rb index 2dc5a431cc0..da3e78834a3 100644 --- a/backend/spec/helpers/admin/base_helper_spec.rb +++ b/backend/spec/helpers/admin/base_helper_spec.rb @@ -12,7 +12,7 @@ end it "should return a formatted date when date is present" do - date = "2013-08-14".to_time + date = Time.zone.parse("2013-08-14") expect(datepicker_field_value(date)).to eq("2013/08/14") end end