mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
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:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user