From 35c2297d55700541bbd3fa7b33244bf8db7f6226 Mon Sep 17 00:00:00 2001 From: David Cook Date: Mon, 27 May 2024 10:21:49 +1000 Subject: [PATCH] Detect changes in price Price is actually an association with lots of custom methods to make it look like a field, and so changes were ignored. Now this issue is fixed, perhaps it should be moved to a concern.. Note, there are other delegated fields: product name and description may be assigned from the variant. But there's no hooks to save the prroduct, so I didn't include it when checking for changes. --- app/models/spree/variant.rb | 5 +++++ spec/models/spree/variant_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/app/models/spree/variant.rb b/app/models/spree/variant.rb index 294c1edfe0..78e5819107 100644 --- a/app/models/spree/variant.rb +++ b/app/models/spree/variant.rb @@ -199,6 +199,11 @@ module Spree price_in(currency).try(:amount) end + def changed? + # Changes to price are saved after_save + super || default_price.changed? + end + # can_supply? is implemented in VariantStock def in_stock?(quantity = 1) can_supply?(quantity) diff --git a/spec/models/spree/variant_spec.rb b/spec/models/spree/variant_spec.rb index 832cb6d931..3cea5ba96f 100644 --- a/spec/models/spree/variant_spec.rb +++ b/spec/models/spree/variant_spec.rb @@ -58,6 +58,28 @@ RSpec.describe Spree::Variant do end end + describe "#changed?" do + subject(:variant) { create(:variant) } + + it { is_expected.not_to be_changed } + + it "is changed when basic fields are changed" do + subject.display_name = "blah" + expect(subject).to be_changed + end + + describe "default_price" do + it "price" do + subject.price = 100 + expect(subject).to be_changed + end + it "currency" do + subject.currency = "USD" + expect(subject).to be_changed + end + end + end + context "price parsing" do context "price=" do context "with decimal point" do