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.
This commit is contained in:
David Cook
2024-05-27 10:21:49 +10:00
parent c71eb2d6b5
commit 35c2297d55
2 changed files with 27 additions and 0 deletions

View File

@@ -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)

View File

@@ -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