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
Open source variant validations and callbacks
Łukasz Adamczyk3908fb81e835
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rbindex 0ed0b70928..5fa7ac5537 100644--- a/core/app/models/spree/variant.rb+++ b/core/app/models/spree/variant.rb@@ -69,6 +69,9 @@ module Spreeafter_commit :clear_line_items_cache, on: :updateafter_update_commit :handle_track_inventory_change+ after_commit :remove_prices_from_master_variant, on: [:create, :update], unless: :is_master?+ after_commit :remove_stock_items_from_master_variant, on: :create, unless: :is_master?+after_touch :clear_in_stock_cachescope :in_stock, -> { left_joins(:stock_items).where("#{Spree::StockItem.table_name}.count_on_hand > ? OR #{Spree::Variant.table_name}.track_inventory = ?", 0, false) }@@ -499,14 +502,18 @@ module Spree# Ensures a new variant takes the product master price when price is not supplieddef check_price- if price.nil? && Spree::Config[:require_master_price]- return errors.add(:base, :no_master_variant_found_to_infer_price) unless product&.master- return errors.add(:base, :must_supply_price_for_variant_or_master) if self == product.master+ return if (has_default_price? && default_price.valid?) || prices.any?- self.price = product.master.price- end- if price.present? && currency.nil?- self.currency = Spree::Store.default.default_currency+ infer_price_from_default_variant_if_needed+ self.currency = Spree::Store.default.default_currency if price.present? && currency.nil?+ end++ def infer_price_from_default_variant_if_needed+ if price.nil?+ return errors.add(:base, :no_master_variant_found_to_infer_price) unless product&.master++ # At this point, master can have or have no price, so let's use price from the default variant+ self.price = product.default_variant.priceendend@@ -538,5 +545,13 @@ module Spreestock_items.delete_allend++ def remove_prices_from_master_variant+ product.master.prices.delete_all if prices.exists?+ end++ def remove_stock_items_from_master_variant+ product.master.stock_items.delete_all+ endendenddiff --git a/core/lib/spree/testing_support/factories/variant_factory.rb b/core/lib/spree/testing_support/factories/variant_factory.rbindex eb88e53c2b..fecf0af30f 100644--- a/core/lib/spree/testing_support/factories/variant_factory.rb+++ b/core/lib/spree/testing_support/factories/variant_factory.rb@@ -41,6 +41,12 @@ FactoryBot.define dofactory :with_image_variant doimages { create_list(:image, 1) }end++ trait :with_no_price do+ price { nil }+ cost_price { nil }+ currency { nil }+ endendfactory :master_variant dodiff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rbindex 02f76fddfa..b11ad4a5d1 100644--- a/core/spec/models/spree/variant_spec.rb+++ b/core/spec/models/spree/variant_spec.rb@@ -134,6 +134,34 @@ describe Spree::Variant, type: :model doendend+ describe 'after_commit :remove_prices_from_master_variant' do+ let(:variant) { build(:variant, product: product) }+ let(:product) { create(:product, stores: [store]) }++ let(:master) { product.master }++ it 'removes prices from master when variant with price is created' do+ expect { variant.save! }.to change(product.master.prices, :count).from(1).to(0)+ end+ end++ describe 'after_commit :remove_stock_items_from_master_variant' do+ let(:variant) { build(:variant, product: product) }+ let(:product) { create(:product, stores: [store]) }++ let(:master) { product.master }++ before do+ master.stock_items << create(:stock_item, variant: master)+ expect(master.stock_items.reload.count).to be >= 1+ end++ it 'removes stock items from master when variant is created' do+ variant.save!+ expect(product.master.stock_items.reload.count).to eq(0)+ end+ end+describe 'scope' dodescribe '.eligible' docontext 'when only master variants' do@@ -1062,97 +1090,84 @@ describe Spree::Variant, type: :model doendend- describe '#check_price' do- let(:variant) { create(:variant) }- let(:variant2) { create(:variant) }+ describe 'validate :check_price' do+ subject { variant.save }- context 'require_master_price set false' do- before { Spree::Config.set(require_master_price: false) }+ let(:currency) { Spree::Store.default.default_currency }- context 'price present and currency present' do- it { expect(variant.send(:check_price)).to be(nil) }- end+ context 'when variant has a default price' do+ let(:variant) { build(:variant, product: product, default_price: default_price) }++ let(:product) { create(:product, master: master) }+ let(:master) { create(:master_variant, price: 11.11, currency: currency) }- context 'price present and currency nil' do- before { variant.currency = nil }+ let(:default_price) { build(:price, amount: 12.34, currency: currency) }- it { expect(variant.send(:check_price)).to eq(Spree::Store.default.default_currency) }+ it 'keeps the default price' do+ expect(subject).to be(true)+ expect(variant.price_in(currency).amount).to eq(12.34)end- context 'price nil and currency present' do- before { variant.price = nil }+ context 'when the default price is invalid' do+ let(:default_price) { build(:price, amount: nil, currency: currency) }- it { expect(variant.send(:check_price)).to be(nil) }- end+ it 'infers price from the default variant' do+ expect(subject).to be(true)+ expect(variant.price_in(currency).amount).to eq(11.11)+ end- context 'price nil and currency nil' do- before { variant.price = nil }+ context 'when there is no default variant' do+ let(:product) { nil }- it { expect(variant.send(:check_price)).to be(nil) }+ it 'adds an error' do+ expect(subject).to be(false)+ expect(variant.errors[:base]).to contain_exactly(+ I18n.t('activerecord.errors.models.spree/variant.attributes.base.no_master_variant_found_to_infer_price')+ )+ end+ endendend- context 'require_master_price set true' do- before { Spree::Config.set(require_master_price: true) }+ context 'when variant has no default price' do+ let(:variant) { build(:variant, :with_no_price, product: product) }- context 'price present and currency present' do- it { expect(variant.send(:check_price)).to be(nil) }- end+ let(:product) { create(:product, master: master) }+ let(:master) { create(:master_variant, price: 11.11, currency: currency) }- context 'price present and currency nil' do- before { variant.currency = nil }-- it { expect(variant.send(:check_price)).to eq(Spree::Store.default.default_currency) }+ it 'infers price from the default variant' do+ expect(subject).to be(true)+ expect(variant.price_in(currency).amount).to eq(11.11)end- context 'product and master_variant present and equal' do- context 'price nil and currency present' do- before { variant.price = nil }-- it { expect(variant.send(:check_price)).to be(nil) }-- context 'check variant price' do- before { variant.send(:check_price) }-- it { expect(variant.price).to eq(variant.product.master.price) }- end- end-- context 'price nil and currency nil' do- before do- variant.price = nil- variant.send(:check_price)- end+ context 'when there is no default variant' do+ let(:product) { nil }- it { expect(variant.price).to eq(variant.product.master.price) }- it { expect(variant.currency).to eq(Spree::Config[:currency]) }+ it 'adds an error' do+ expect(subject).to be(false)+ expect(variant.errors[:base]).to contain_exactly(+ I18n.t('activerecord.errors.models.spree/variant.attributes.base.no_master_variant_found_to_infer_price')+ )endend+ end- context 'product not present' do- context 'product not present' do- before { variant.product = nil }+ context 'when variant has prices' do+ let(:variant) { build(:variant, :with_no_price, prices: [price_1, price_2]) }- context 'price nil and currency present' do- before { variant.price = nil }+ let(:price_1) { build(:price, amount: 10, currency: 'PLN') }+ let(:price_2) { build(:price, amount: 11, currency: 'GBP') }- it 'adds absence of master error' do- variant.send(:check_price)- expect(variant.errors[:base]).to include I18n.t('activerecord.errors.models.spree/variant.attributes.base.no_master_variant_found_to_infer_price')- end- end+ it 'keeps the prices' do+ expect(subject).to be(true)- context 'price nil and currency nil' do- before { variant.price = nil }-- it 'adds absence of master error' do- variant.send(:check_price)- expect(variant.errors[:base]).to include I18n.t('activerecord.errors.models.spree/variant.attributes.base.no_master_variant_found_to_infer_price')- end- end- end+ expect(variant.prices.count).to eq(2)+ expect(variant.price_in('PLN').amount).to eq(10)+ expect(variant.price_in('GBP').amount).to eq(11)endend++ context 'when variant price 'enddescribe '#created_at' do