Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Disallow creation of `Spree::Price` records with empty values (#12126)
Damian Legawiec9385db79c4a0
diff --git a/core/app/models/spree/price.rb b/core/app/models/spree/price.rbindex fb0d638cc2..45d2c549e8 100644--- a/core/app/models/spree/price.rb+++ b/core/app/models/spree/price.rb@@ -14,10 +14,17 @@ module Spreebefore_validation :ensure_currencybefore_save :remove_compare_at_amount_if_equals_amount+ # legacy behaviorvalidates :amount, allow_nil: true, numericality: {greater_than_or_equal_to: 0,less_than_or_equal_to: MAXIMUM_AMOUNT- }+ }, if: -> { Spree::RuntimeConfig.allow_empty_price_amount }++ # new behavior+ validates :amount, allow_nil: false, numericality: {+ greater_than_or_equal_to: 0,+ less_than_or_equal_to: MAXIMUM_AMOUNT+ }, unless: -> { Spree::RuntimeConfig.allow_empty_price_amount }validates :compare_at_amount, allow_nil: true, numericality: {greater_than_or_equal_to: 0,diff --git a/core/lib/spree/core/runtime_configuration.rb b/core/lib/spree/core/runtime_configuration.rbindex 72c898fb03..3864038232 100644--- a/core/lib/spree/core/runtime_configuration.rb+++ b/core/lib/spree/core/runtime_configuration.rb@@ -4,6 +4,7 @@ module Spreemodule Coreclass RuntimeConfiguration < Preferences::RuntimeConfigurationpreference :always_use_translations, :boolean, default: false+ preference :allow_empty_price_amount, :boolean, default: falseendendenddiff --git a/core/spec/models/spree/order/currency_updater_spec.rb b/core/spec/models/spree/order/currency_updater_spec.rbindex f15ca8419e..d06306c324 100644--- a/core/spec/models/spree/order/currency_updater_spec.rb+++ b/core/spec/models/spree/order/currency_updater_spec.rb@@ -24,7 +24,10 @@ describe Spree::Order, type: :model doendcontext 'when there is a price with nil amount' do- let!(:euro_price) { create(:price, variant: line_item.variant, amount: nil, currency: 'EUR') }+ let!(:euro_price) do+ allow(Spree::RuntimeConfig).to receive(:allow_empty_price_amount).and_return(true)+ create(:price, variant: line_item.variant, amount: nil, currency: 'EUR')+ endit 'destroys the line item when we switch to that price\'s currency' doexpect { line_item.order.update!(currency: 'EUR') }.to change(Spree::LineItem, :count).by(-1)diff --git a/core/spec/models/spree/price_spec.rb b/core/spec/models/spree/price_spec.rbindex 21e98659cf..cad8ea5068 100644--- a/core/spec/models/spree/price_spec.rb+++ b/core/spec/models/spree/price_spec.rb@@ -86,7 +86,17 @@ describe Spree::Price, type: :model docontext 'when the amount is nil' dolet(:amount) { nil }- it { is_expected.to be_valid }+ context 'legacy behavior' do+ before do+ allow(Spree::RuntimeConfig).to receive(:allow_empty_price_amount).and_return(true)+ end++ it { is_expected.to be_valid }+ end++ context 'new behavior' do+ it { is_expected.not_to be_valid }+ endendcontext 'when the amount is less than 0' dodiff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rbindex 5b6a75411d..026fb6f626 100644--- a/core/spec/models/spree/variant_spec.rb+++ b/core/spec/models/spree/variant_spec.rb@@ -176,6 +176,10 @@ describe Spree::Variant, type: :model doendcontext 'when price do not have amount' do+ before do+ allow(Spree::RuntimeConfig).to receive(:allow_empty_price_amount).and_return(true)+ end+let!(:price_1) { create(:price, currency: currency, variant: variant, amount: nil) }it { expect(Spree::Variant.for_currency_and_available_price_amount(currency)).not_to include(variant) }@@ -192,6 +196,10 @@ describe Spree::Variant, type: :model doendcontext 'when price do not have amount' do+ before do+ allow(Spree::RuntimeConfig).to receive(:allow_empty_price_amount).and_return(true)+ end+let!(:price_1) { create(:price, currency: unavailable_currency, variant: variant, amount: nil) }it { expect(Spree::Variant.for_currency_and_available_price_amount(currency)).not_to include(variant) }diff --git a/core/spec/services/spree/cart/add_item_spec.rb b/core/spec/services/spree/cart/add_item_spec.rbindex cfdb7a79cd..2c17204957 100644--- a/core/spec/services/spree/cart/add_item_spec.rb+++ b/core/spec/services/spree/cart/add_item_spec.rb@@ -236,6 +236,7 @@ module Spreelet(:call_service) { subject.call(order: order, variant: variant, quantity: 1) }before do+ allow(Spree::RuntimeConfig).to receive(:allow_empty_price_amount).and_return(true)variant.prices.first.update(amount: nil)end