From c165ade4baed80a99c0466a2f94a032db3b4e926 Mon Sep 17 00:00:00 2001 From: David Cook Date: Tue, 10 Mar 2026 15:40:11 +1100 Subject: [PATCH] Avoid unnecessary save Actually, the variant factory is still adding an extra save. We should refactor Variant to avoid that.. but the afternoon slump has got me. --- app/models/spree/variant.rb | 2 +- spec/models/spree/variant_spec.rb | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/app/models/spree/variant.rb b/app/models/spree/variant.rb index 952f48d23d..eb36f6dacc 100644 --- a/app/models/spree/variant.rb +++ b/app/models/spree/variant.rb @@ -282,8 +282,8 @@ module Spree dup.tap do |variant| variant.price = price - variant.save! variant.source_variants = [self] + variant.stock_items << Spree::StockItem.new(variant:) variant.owner_id = owner_id variant.on_demand = on_demand variant.on_hand = on_hand diff --git a/spec/models/spree/variant_spec.rb b/spec/models/spree/variant_spec.rb index a53b3f533b..fad05d63bb 100644 --- a/spec/models/spree/variant_spec.rb +++ b/spec/models/spree/variant_spec.rb @@ -1018,13 +1018,9 @@ RSpec.describe Spree::Variant do child: enterprise, permissions_list: [:create_sourced_variants]) } + let(:variant) { create(:variant, price: 10.95, on_demand: false, on_hand: 5) } it "clones the variant, retaining a link to the source" do - variant.price = 10.95 - variant.save! - variant.on_demand = false - variant.on_hand = 5 - sourced_variant = variant.create_sourced_variant(user) expect(sourced_variant.source_variants).to eq [variant]