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
Deprecate `DefaultPrice` concern (#13881)
Damian Legawiec4bb98ebf34ba
diff --git a/spree/admin/spec/controllers/spree/admin/products_controller_spec.rb b/spree/admin/spec/controllers/spree/admin/products_controller_spec.rbindex f7b3170f64..7d8f746934 100644--- a/spree/admin/spec/controllers/spree/admin/products_controller_spec.rb+++ b/spree/admin/spec/controllers/spree/admin/products_controller_spec.rb@@ -935,7 +935,10 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller dovariants_attributes: {'0' => {id: variant1.id,- price: { 'PLN' => 10, 'USD' => 20 },+ prices_attributes: {+ '0' => { currency: 'PLN', amount: 10 },+ '1' => { currency: 'USD', amount: 20 }+ },options: [{id: nil,@@ -955,7 +958,10 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do},'1' => {id: variant2.id,- price: { 'PLN' => 30, 'USD' => 40 },+ prices_attributes: {+ '0' => { currency: 'PLN', amount: 30 },+ '1' => { currency: 'USD', amount: 40 }+ },stock_items_attributes: {'0' => {id: variant2_stock_item.id,diff --git a/spree/core/app/models/concerns/spree/default_price.rb b/spree/core/app/models/concerns/spree/default_price.rbindex 19528ef0e3..efb2ff4ccf 100644--- a/spree/core/app/models/concerns/spree/default_price.rb+++ b/spree/core/app/models/concerns/spree/default_price.rb@@ -2,24 +2,126 @@ module Spreemodule DefaultPriceextend ActiveSupport::Concern+ DEPRECATION_MSG = 'Spree::DefaultPrice is deprecated and will be removed in Spree 6.0. ' \+ 'Use variant.set_price(currency, amount) and variant.price_in(currency) instead.'+included dohas_one :default_price,-> { with_deleted.where(currency: Spree::Store.default.default_currency) },class_name: 'Spree::Price',dependent: :destroy- delegate :display_price, :display_amount, :price, :currency, :price=,- :price_including_vat_for, :currency=, :display_compare_at_price,- :compare_at_price, :compare_at_price=, to: :find_or_build_default_price+ after_save :save_default_price, if: -> { Spree::Config.enable_legacy_default_price }++ def price+ Spree::Deprecation.warn(Spree::DefaultPrice::DEPRECATION_MSG)+ if Spree::Config.enable_legacy_default_price+ find_or_build_default_price.price+ else+ price_in(Spree::Store.default.default_currency).amount+ end+ end++ def price=(value)+ Spree::Deprecation.warn(Spree::DefaultPrice::DEPRECATION_MSG)+ if Spree::Config.enable_legacy_default_price+ find_or_build_default_price.price = value+ else+ set_price(Spree::Store.default.default_currency, value)+ end+ end++ def currency+ Spree::Deprecation.warn(Spree::DefaultPrice::DEPRECATION_MSG)+ if Spree::Config.enable_legacy_default_price+ find_or_build_default_price.currency+ else+ Spree::Store.default.default_currency+ end+ end++ def currency=(value)+ Spree::Deprecation.warn(Spree::DefaultPrice::DEPRECATION_MSG)+ if Spree::Config.enable_legacy_default_price+ find_or_build_default_price.currency = value+ end+ # no-op when legacy is disabled — currency is determined by the store+ end++ def display_price+ Spree::Deprecation.warn(Spree::DefaultPrice::DEPRECATION_MSG)+ if Spree::Config.enable_legacy_default_price+ find_or_build_default_price.display_price+ else+ price_in(Spree::Store.default.default_currency).display_amount+ end+ end++ def display_amount+ Spree::Deprecation.warn(Spree::DefaultPrice::DEPRECATION_MSG)+ if Spree::Config.enable_legacy_default_price+ find_or_build_default_price.display_amount+ else+ price_in(Spree::Store.default.default_currency).display_amount+ end+ end++ def compare_at_price+ Spree::Deprecation.warn(Spree::DefaultPrice::DEPRECATION_MSG)+ if Spree::Config.enable_legacy_default_price+ find_or_build_default_price.compare_at_price+ else+ price_in(Spree::Store.default.default_currency).compare_at_amount+ end+ end- after_save :save_default_price+ def compare_at_price=(value)+ Spree::Deprecation.warn(Spree::DefaultPrice::DEPRECATION_MSG)+ if Spree::Config.enable_legacy_default_price+ find_or_build_default_price.compare_at_price = value+ else+ default_currency = Spree::Store.default.default_currency+ price_record = price_in(default_currency)+ price_record.compare_at_amount = value+ price_record.save! if price_record.persisted?+ end+ end++ def display_compare_at_price+ Spree::Deprecation.warn(Spree::DefaultPrice::DEPRECATION_MSG)+ if Spree::Config.enable_legacy_default_price+ find_or_build_default_price.display_compare_at_price+ else+ price_in(Spree::Store.default.default_currency).display_compare_at_amount+ end+ end++ def price_including_vat_for(price_options)+ Spree::Deprecation.warn(Spree::DefaultPrice::DEPRECATION_MSG)+ if Spree::Config.enable_legacy_default_price+ find_or_build_default_price.price_including_vat_for(price_options)+ else+ price_in(Spree::Store.default.default_currency).price_including_vat_for(price_options)+ end+ enddef has_default_price?- !default_price.nil?+ Spree::Deprecation.warn(Spree::DefaultPrice::DEPRECATION_MSG)+ if Spree::Config.enable_legacy_default_price+ !default_price.nil?+ else+ prices.base_prices.any? { |p| p.currency == Spree::Store.default.default_currency }+ endenddef find_or_build_default_price- default_price || build_default_price+ Spree::Deprecation.warn(Spree::DefaultPrice::DEPRECATION_MSG)+ if Spree::Config.enable_legacy_default_price+ default_price || build_default_price+ else+ prices.base_prices.find { |p| p.currency == Spree::Store.default.default_currency } ||+ prices.build(currency: Spree::Store.default.default_currency)+ endendprivatediff --git a/spree/core/app/models/spree/price.rb b/spree/core/app/models/spree/price.rbindex fdfdb479d9..192d7334c6 100644--- a/spree/core/app/models/spree/price.rb+++ b/spree/core/app/models/spree/price.rb@@ -135,7 +135,7 @@ module Spree## @return [Boolean]def discounted?- compare_at_amount.to_i.positive? && compare_at_amount > amount+ compare_at_amount.to_i.positive? && amount.present? && compare_at_amount > amountend# returns true if the price was discounteddiff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rbindex a52ed13555..89a5c0f048 100644--- a/spree/core/app/models/spree/product.rb+++ b/spree/core/app/models/spree/product.rb@@ -673,11 +673,12 @@ module Spreevalues = option_values_hash.valuesvalues = values.inject(values.shift) { |memo, value| memo.product(value).map(&:flatten) }+ default_currency = stores.first&.default_currency || Spree::Store.default.default_currency+ master_price = master.price_in(default_currency).amount+values.each do |ids|- variants.create(- option_value_ids: ids,- price: master.price- )+ variant = variants.create!(option_value_ids: ids)+ variant.set_price(default_currency, master_price) if master_price.present?endsaveend@@ -711,6 +712,7 @@ module Spreemaster.new_record? ||master.changed? ||(+ Spree::Config.enable_legacy_default_price &&master.default_price &&(master.default_price.new_record? ||@@ -733,9 +735,13 @@ module Spree# If the master cannot be saved, the Product object will get its errors# and will be destroyeddef validate_master- # We call master.default_price here to ensure price is initialized.- # Required to avoid Variant#check_price validation failing on create.- unless master.default_price && master.valid?+ if Spree::Config.enable_legacy_default_price+ # We call master.default_price here to ensure price is initialized.+ # Required to avoid Variant#check_price validation failing on create.+ master.default_price+ end++ unless master.valid?master.errors.map { |error| { field: error.attribute, message: error&.message } }.each do |err|next if err[:field].blank? || err[:message].blank?diff --git a/spree/core/app/models/spree/variant.rb b/spree/core/app/models/spree/variant.rbindex 66ec9119ee..cf6117f270 100644--- a/spree/core/app/models/spree/variant.rb+++ b/spree/core/app/models/spree/variant.rb@@ -62,14 +62,13 @@ module Spreebefore_validation :set_cost_currency- validate :check_price+ validate :check_price, if: -> { Spree::Config.enable_legacy_default_price }validates :option_value_variants, presence: true, unless: :is_master?- with_options numericality: { greater_than_or_equal_to: 0, allow_nil: true } do- validates :cost_price- validates :price- end+ validates :cost_price, numericality: { greater_than_or_equal_to: 0, allow_nil: true }+ validates :price, numericality: { greater_than_or_equal_to: 0, allow_nil: true },+ if: -> { Spree::Config.enable_legacy_default_price }validates :sku, uniqueness: { conditions: -> { where(deleted_at: nil) }, case_sensitive: false, scope: spree_base_uniqueness_scope },allow_blank: true, unless: :disable_sku_validation?@@ -447,8 +446,8 @@ module Spreedef set_price(currency, amount, compare_at_amount = nil)price = prices.base_prices.find_or_initialize_by(currency: currency)price.amount = amount- price.compare_at_amount = compare_at_amount if compare_at_amount.present?- price.save!+ price.compare_at_amount = compare_at_amount+ price.save! if persisted?end# Returns the price for the given context or options.@@ -607,18 +606,21 @@ module Spree# Ensures a new variant takes the product master price when price is not supplieddef check_price- return if (has_default_price? && default_price.valid?) || prices.any?+ return if prices.any?infer_price_from_default_variant_if_needed- self.currency = Spree::Store.default.default_currency if price.present? && currency.nil?enddef infer_price_from_default_variant_if_needed- if price.nil?+ default_currency = Spree::Store.default.default_currency+ current_price = price_in(default_currency).amount++ if current_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.price+ inferred_price = product.default_variant.price_in(default_currency).amount+ set_price(default_currency, inferred_price) if inferred_price.present?endenddiff --git a/spree/core/app/services/spree/products/prepare_nested_attributes.rb b/spree/core/app/services/spree/products/prepare_nested_attributes.rbindex bef751d0cc..b05b253cfe 100644--- a/spree/core/app/services/spree/products/prepare_nested_attributes.rb+++ b/spree/core/app/services/spree/products/prepare_nested_attributes.rb@@ -33,8 +33,11 @@ module Spreeexisting_variant = variant_params[:id].presence && @product.variants.find_by(id: variant_params[:id])variants_to_remove.delete(variant_params[:id]) if variant_params[:id].present?+ variant_params.delete(:price) # remove legacy price param+if can_update_prices?- # If the variant price is nil then mark it for destruction+ backfill_price_ids!(variant_params, existing_variant)+variant_params[:prices_attributes]&.each do |price_key, price_params|variant_params[:prices_attributes][price_key]['_destroy'] = '1' if price_params[:amount].blank?end@@ -95,6 +98,20 @@ module Spreedelegate :can?, :cannot?, to: :ability+ # Backfill IDs for prices_attributes entries that reference existing prices+ # so that ActiveRecord updates them instead of inserting duplicates+ def backfill_price_ids!(variant_params, existing_variant)+ return unless existing_variant && variant_params[:prices_attributes]++ variant_params[:prices_attributes].each do |_key, price_params|+ next if price_params[:id].present?+ next if price_params[:currency].blank?++ existing_price = existing_variant.prices.base_prices.find_by(currency: price_params[:currency])+ price_params[:id] = existing_price.id if existing_price+ end+ end+def product_option_types_params@product_option_types_params ||= {}enddiff --git a/spree/core/lib/spree/core/configuration.rb b/spree/core/lib/spree/core/configuration.rbindex 61fbe776c7..07689fae83 100644--- a/spree/core/lib/spree/core/configuration.rb+++ b/spree/core/lib/spree/core/configuration.rb@@ -40,6 +40,7 @@ module Spreepreference :currency, :string, default: 'USD', deprecated: truepreference :credit_to_new_allocation, :boolean, default: falsepreference :disable_migration_check, :boolean, default: false # when turned on disables the startup warning about missing engine migrations+ preference :enable_legacy_default_price, :boolean, default: false # when enabled, keeps the legacy DefaultPrice concern active (has_one :default_price, variant.price= delegation, check_price validation). Disable (default) to use set_price exclusively.preference :disable_sku_validation, :boolean, default: false # when turned off disables the built-in SKU uniqueness validationpreference :disable_store_presence_validation, :boolean, default: false # when turned off disables Store presence validation for Products and Payment Methodspreference :events_log_enabled, :boolean, default: true # Log all Spree events to Rails loggerdiff --git a/spree/core/lib/spree/core/controller_helpers/order.rb b/spree/core/lib/spree/core/controller_helpers/order.rbindex 81499fe16c..a57f1f98cb 100644--- a/spree/core/lib/spree/core/controller_helpers/order.rb+++ b/spree/core/lib/spree/core/controller_helpers/order.rb@@ -168,18 +168,20 @@ module Spreeenddef order_includes+ variant_includes = [+ :images,+ :prices,+ :stock_items,+ :stock_locations,+ { option_values: :option_type },+ { stock_items: :stock_location },+ { product: :master }+ ]+ variant_includes << :default_price if Spree::Config.enable_legacy_default_price+{line_items: {- variant: [- :images,- :prices,- :default_price,- :stock_items,- :stock_locations,- { option_values: :option_type },- { stock_items: :stock_location },- { product: :master }- ]+ variant: variant_includes}}enddiff --git a/spree/core/lib/spree/core/pricing/resolver.rb b/spree/core/lib/spree/core/pricing/resolver.rbindex e3aa4cda37..7150ff8edc 100644--- a/spree/core/lib/spree/core/pricing/resolver.rb+++ b/spree/core/lib/spree/core/pricing/resolver.rb@@ -106,11 +106,13 @@ module Spreecontext.variant.pricesend- # Builds an empty price for the variant+ # Returns an empty price placeholder for the variant.+ # Uses Price.new instead of prices.build to avoid polluting the association+ # with an unsaved record that would fail validation on variant save.# @return [Spree::Price]def build_empty_price- context.variant.prices.build(- variant: context.variant,+ Spree::Price.new(+ variant_id: context.variant.id,currency: context.currency,amount: nil,price_list_id: nildiff --git a/spree/core/lib/spree/testing_support/factories/product_factory.rb b/spree/core/lib/spree/testing_support/factories/product_factory.rbindex e88e48e67f..9ce8db25f3 100644--- a/spree/core/lib/spree/testing_support/factories/product_factory.rb+++ b/spree/core/lib/spree/testing_support/factories/product_factory.rb@@ -2,7 +2,6 @@ FactoryBot.define dofactory :base_product, class: Spree::Product dosequence(:name) { |n| "Product #{n}#{Kernel.rand(9999)}" }description { generate(:random_description) }- price { 19.99 }cost_price { 17.00 }sku { generate(:sku) }available_on { 1.year.ago }@@ -12,17 +11,28 @@ FactoryBot.define dostatus { 'active' }stores { [Spree::Store.default] }+ transient do+ price { 19.99 }+ compare_at_price { nil }+ currency { nil }+ end+# ensure stock item will be created for this products master# also attach this product to the default store if no stores are passed inbefore(:create) do |_product|create(:stock_location) unless Spree::StockLocation.any?create(:store, default: true) unless Spree::Store.any?end- after(:create) do |product|+ after(:create) do |product, evaluator|existing_location_ids = product.master.stock_items.pluck(:stock_location_id)Spree::StockLocation.where.not(id: existing_location_ids).find_each do |stock_location|stock_location.propagate_variant(product.master)end++ if evaluator.price.present?+ price_currency = evaluator.currency || product.stores.first&.default_currency || 'USD'+ product.master.set_price(price_currency, evaluator.price, evaluator.compare_at_price)+ endendfactory :custom_product dodiff --git a/spree/core/lib/spree/testing_support/factories/variant_factory.rb b/spree/core/lib/spree/testing_support/factories/variant_factory.rbindex 0a41216353..de195d8a00 100644--- a/spree/core/lib/spree/testing_support/factories/variant_factory.rb+++ b/spree/core/lib/spree/testing_support/factories/variant_factory.rb@@ -2,7 +2,6 @@ FactoryBot.define dosequence(:random_float) { BigDecimal("#{rand(200)}.#{rand(99)}") }factory :base_variant, class: Spree::Variant do- price { 19.99 }cost_price { 17.00 }sku { generate(:sku) }weight { generate(:random_float) }@@ -16,6 +15,9 @@ FactoryBot.define dooption_values { [build(:option_value)] }transient do+ price { 19.99 }+ compare_at_price { nil }+ currency { nil }create_stock { true }end@@ -31,6 +33,11 @@ FactoryBot.define dostock_location.propagate_variant(variant)endend++ if evaluator.price.present?+ price_currency = evaluator.currency || variant.product&.stores&.first&.default_currency || 'USD'+ variant.set_price(price_currency, evaluator.price, evaluator.compare_at_price)+ endendfactory :variant do@@ -44,7 +51,6 @@ FactoryBot.define dotrait :with_no_price doprice { nil }cost_price { nil }- currency { nil }endenddiff --git a/spree/core/spec/models/concerns/spree/default_price_spec.rb b/spree/core/spec/models/concerns/spree/default_price_spec.rbnew file mode 100644index 0000000000..3683cb6e34--- /dev/null+++ b/spree/core/spec/models/concerns/spree/default_price_spec.rb@@ -0,0 +1,170 @@+require 'spec_helper'++RSpec.describe Spree::DefaultPrice do+ let(:store) { Spree::Store.default }+ let(:product) { create(:product, stores: [store]) }+ let(:variant) { product.master }++ describe 'with enable_legacy_default_price disabled (default)' do+ before do+ allow(Spree::Config).to receive(:enable_legacy_default_price).and_return(false)+ end++ describe '#price' do+ it 'returns amount from price_in for the default currency' do+ variant.set_price('USD', 29.99)+ expect(variant.price).to eq(29.99)+ end++ it 'returns nil when no price exists for the default currency' do+ variant.prices.destroy_all+ expect(variant.price).to be_nil+ end+ end++ describe '#price=' do+ it 'sets price via set_price' do+ variant.price = 19.99+ expect(variant.price_in('USD').amount).to eq(19.99)+ end+ end++ describe '#currency' do+ it 'returns the default store currency' do+ expect(variant.currency).to eq(store.default_currency)+ end+ end++ describe '#display_price' do+ it 'returns display amount for the default currency' do+ variant.set_price('USD', 10.55)+ expect(variant.display_price.to_s).to eq('$10.55')+ end+ end++ describe '#has_default_price?' do+ it 'returns true when a base price exists in the default currency' do+ variant.set_price('USD', 10)+ expect(variant.has_default_price?).to be true+ end++ it 'returns false when no base price exists in the default currency' do+ variant.prices.destroy_all+ expect(variant.has_default_price?).to be false+ end+ end++ describe '#set_price' do+ it 'persists the price when variant is persisted' do+ variant.set_price('USD', 42.00)+ expect(variant.price_in('USD').amount).to eq(42.00)+ expect(variant.price_in('USD')).to be_persisted+ end++ it 'builds price in-memory when variant is not persisted' do+ new_variant = Spree::Variant.new(product: product)+ new_variant.set_price('USD', 15.00)+ price = new_variant.prices.detect { |p| p.currency == 'USD' }+ expect(price.amount).to eq(15.00)+ expect(price).not_to be_persisted+ end++ it 'supports multiple currencies' do+ variant.set_price('USD', 29.99)+ variant.set_price('EUR', 27.99)+ expect(variant.price_in('USD').amount).to eq(29.99)+ expect(variant.price_in('EUR').amount).to eq(27.99)+ end+ end++ describe 'variant save without price' do+ it 'allows saving a variant without any price' do+ new_product = create(:product, stores: [store])+ master = new_product.master+ master.prices.destroy_all+ expect { master.save! }.not_to raise_error+ end+ end++ describe 'save_default_price callback' do+ it 'does not run' do+ variant.set_price('USD', 50.00)+ expect(variant).not_to receive(:save_default_price)+ variant.save!+ end+ end+ end++ describe 'with enable_legacy_default_price enabled' do+ before do+ allow(Spree::Config).to receive(:enable_legacy_default_price).and_return(true)+ end++ describe '#price' do+ it 'returns price from the default price record' do+ expect(variant.price).to eq(variant.default_price.amount)+ end+ end++ describe '#price=' do+ it 'sets price on the default price record' do+ variant.price = 25.00+ expect(variant.default_price.amount).to eq(25.00)+ end+ end++ describe '#has_default_price?' do+ it 'returns true when default_price exists' do+ expect(variant.has_default_price?).to be true+ end+ end++ describe 'save_default_price callback' do+ it 'saves the default price when it has changed' do+ variant.reload+ variant.default_price.price = 99.99+ expect(variant.default_price).to receive(:save)+ variant.save!+ end+ end++ describe 'check_price validation' do+ it 'runs the check_price validation' do+ new_variant = build(:variant, product: product)+ expect(new_variant).to be_valid+ end+ end+ end++ describe 'deprecation warnings' do+ it 'warns on #price' do+ expect(Spree::Deprecation).to receive(:warn).with(Spree::DefaultPrice::DEPRECATION_MSG).at_least(:once)+ variant.price+ end++ it 'warns on #price=' do+ expect(Spree::Deprecation).to receive(:warn).with(Spree::DefaultPrice::DEPRECATION_MSG).at_least(:once)+ variant.price = 10+ end++ it 'warns on #display_price' do+ expect(Spree::Deprecation).to receive(:warn).with(Spree::DefaultPrice::DEPRECATION_MSG).at_least(:once)+ variant.display_price+ end++ it 'warns on #currency' do+ expect(Spree::Deprecation).to receive(:warn).with(Spree::DefaultPrice::DEPRECATION_MSG).at_least(:once)+ variant.currency+ end++ it 'warns on #has_default_price?' do+ expect(Spree::Deprecation).to receive(:warn).with(Spree::DefaultPrice::DEPRECATION_MSG).at_least(:once)+ variant.has_default_price?+ end++ it 'warns on #find_or_build_default_price' do+ expect(Spree::Deprecation).to receive(:warn).with(Spree::DefaultPrice::DEPRECATION_MSG).at_least(:once)+ variant.find_or_build_default_price+ end+ end+enddiff --git a/spree/core/spec/models/spree/calculator/shipping/flat_percent_item_total_spec.rb b/spree/core/spec/models/spree/calculator/shipping/flat_percent_item_total_spec.rbindex 5f45dc184e..e17d0ab653 100644--- a/spree/core/spec/models/spree/calculator/shipping/flat_percent_item_total_spec.rb+++ b/spree/core/spec/models/spree/calculator/shipping/flat_percent_item_total_spec.rb@@ -5,15 +5,15 @@ module Spreedescribe FlatPercentItemTotal, type: :model dosubject { FlatPercentItemTotal.new(preferred_flat_percent: 10) }- let(:variant1) { build(:variant, price: 10.11) }- let(:variant2) { build(:variant, price: 20.2222) }+ let(:variant1) { create(:variant, price: 10.11) }+ let(:variant2) { create(:variant, price: 20.2222) }let(:inventory_unit1) { build(:inventory_unit, quantity: 2, variant: variant1, line_item: line_item1) }let(:inventory_unit2) { build(:inventory_unit, quantity: 1, variant: variant2, line_item: line_item2) }let(:inventory_units) { [inventory_unit1, inventory_unit2] }- let(:line_item1) { build(:line_item, variant: variant1, price: variant1.price) }- let(:line_item2) { build(:line_item, variant: variant2, price: variant2.price) }+ let(:line_item1) { build(:line_item, variant: variant1, price: 10.11) }+ let(:line_item2) { build(:line_item, variant: variant2, price: 20.2222) }let(:package) dobuild(:stock_package, contents: inventory_units.map { |iu| ::Spree::Stock::ContentItem.new(iu) })diff --git a/spree/core/spec/models/spree/calculator/shipping/flat_rate_spec.rb b/spree/core/spec/models/spree/calculator/shipping/flat_rate_spec.rbindex 2e79529542..ba1ec19ec0 100644--- a/spree/core/spec/models/spree/calculator/shipping/flat_rate_spec.rb+++ b/spree/core/spec/models/spree/calculator/shipping/flat_rate_spec.rb@@ -1,8 +1,8 @@require 'spec_helper'RSpec.describe Spree::Calculator::Shipping::FlatRate, type: :model do- let(:variant1) { build(:variant, price: 10, weight: 0.75) }- let(:variant2) { build(:variant, price: 15, weight: 2.5) }+ let(:variant1) { create(:variant, price: 10, weight: 0.75) }+ let(:variant2) { create(:variant, price: 15, weight: 2.5) }let(:line_item1) { build(:line_item, variant: variant1, price: 10) }let(:line_item2) { build(:line_item, variant: variant2, price: 15) }diff --git a/spree/core/spec/models/spree/calculator/shipping/flexi_rate_spec.rb b/spree/core/spec/models/spree/calculator/shipping/flexi_rate_spec.rbindex 4d5635155d..33c695717f 100644--- a/spree/core/spec/models/spree/calculator/shipping/flexi_rate_spec.rb+++ b/spree/core/spec/models/spree/calculator/shipping/flexi_rate_spec.rb@@ -3,8 +3,8 @@ require 'spec_helper'module Spreemodule Calculator::Shippingdescribe FlexiRate, type: :model do- let(:variant1) { build(:variant, price: 10) }- let(:variant2) { build(:variant, price: 20) }+ let(:variant1) { create(:variant, price: 10) }+ let(:variant2) { create(:variant, price: 20) }let(:package) dobuild(:stock_package, variants_contents: { variant1 => 4, variant2 => 6 })diff --git a/spree/core/spec/models/spree/calculator/shipping/price_sack_spec.rb b/spree/core/spec/models/spree/calculator/shipping/price_sack_spec.rbindex 737458e384..b12ba8bca5 100644--- a/spree/core/spec/models/spree/calculator/shipping/price_sack_spec.rb+++ b/spree/core/spec/models/spree/calculator/shipping/price_sack_spec.rb@@ -12,7 +12,7 @@ module Spreeendlet(:line_item) { build(:line_item, variant: variant, price: variant.price) }- let(:variant) { build(:variant, price: 2) }+ let(:variant) { create(:variant, price: 2) }let(:inventory_unit1) { }diff --git a/spree/core/spec/models/spree/line_item_spec.rb b/spree/core/spec/models/spree/line_item_spec.rbindex 457dd2ab26..1744b14399 100644--- a/spree/core/spec/models/spree/line_item_spec.rb+++ b/spree/core/spec/models/spree/line_item_spec.rb@@ -503,16 +503,18 @@ describe Spree::LineItem, type: :model doenddescribe '#compare_at_amount' do- let(:variant) { create(:variant, compare_at_price: 15) }+ let(:variant) { create(:variant) }let(:line_item) { build(:line_item, variant: variant, quantity: 2) }- it 'returns the compare at amount for the line item' do- expect(line_item.compare_at_amount).to eq(30)+ context 'when compare_at_price is set' do+ before { variant.set_price('USD', 19.99, 15) }++ it 'returns the compare at amount for the line item' do+ expect(line_item.compare_at_amount).to eq(30)+ endendcontext 'when compare_at_price is nil' do- let(:variant) { create(:variant, compare_at_price: nil) }-it 'returns zero' doexpect(line_item.compare_at_amount).to eq(0)expect(line_item.display_compare_at_amount.to_s).to eq('$0.00')@@ -520,7 +522,7 @@ describe Spree::LineItem, type: :model doendcontext 'when compare_at_price is zero' do- let(:variant) { create(:variant, compare_at_price: 0) }+ before { variant.set_price('USD', 19.99, 0) }it 'returns zero' doexpect(line_item.compare_at_amount).to eq(0)@@ -530,9 +532,11 @@ describe Spree::LineItem, type: :model doenddescribe '#display_compare_at_amount' do- let(:variant) { create(:variant, compare_at_price: 15) }+ let(:variant) { create(:variant) }let(:line_item) { build(:line_item, variant: variant, quantity: 2) }+ before { variant.set_price('USD', 19.99, 15) }+it 'returns the compare at amount for the line item' doexpect(line_item.display_compare_at_amount.to_s).to eq('$30.00')enddiff --git a/spree/core/spec/models/spree/product_spec.rb b/spree/core/spec/models/spree/product_spec.rbindex a69fe0fe2a..e689f74240 100644--- a/spree/core/spec/models/spree/product_spec.rb+++ b/spree/core/spec/models/spree/product_spec.rb@@ -164,12 +164,13 @@ describe Spree::Product, type: :model doendend- context 'when master default price changed' do+ context 'when master default price changed', enable_legacy_default_price: true dobefore do- master = product.master+ allow(Spree::Config).to receive(:enable_legacy_default_price).and_return(true)+ master = product.master.reloadmaster.default_price.price = 11master.save!- product.master.default_price.price = 12+ product.master.reload.default_price.price = 12endit 'saves the master' do@@ -244,13 +245,11 @@ describe Spree::Product, type: :model docontext 'with currency set to JPY' dobefore do- product.master.default_price.currency = 'JPY'- product.master.default_price.save!- Spree::Config[:currency] = 'JPY'+ product.master.set_price('JPY', 11)endit 'displays the currency in yen' do- expect(product.display_price.to_s).to eq('¥11')+ expect(product.master.price_in('JPY').display_amount.to_s).to eq('¥11')endendenddiff --git a/spree/core/spec/models/spree/shipping_calculator_spec.rb b/spree/core/spec/models/spree/shipping_calculator_spec.rbindex ac5b59e649..95cec450ef 100644--- a/spree/core/spec/models/spree/shipping_calculator_spec.rb+++ b/spree/core/spec/models/spree/shipping_calculator_spec.rb@@ -4,16 +4,16 @@ module Spreedescribe ShippingCalculator, type: :model dosubject { ShippingCalculator.new }- let(:variant1) { build(:variant, price: 10) }- let(:variant2) { build(:variant, price: 20) }+ let(:variant1) { create(:variant, price: 10) }+ let(:variant2) { create(:variant, price: 20) }let(:shipment) { build(:shipment) }let(:inventory_unit1) { build(:inventory_unit, quantity: 2, variant: variant1, line_item: line_item1) }let(:inventory_unit2) { build(:inventory_unit, quantity: 1, variant: variant2, line_item: line_item2) }let(:inventory_units) { [inventory_unit1, inventory_unit2] }- let(:line_item1) { build(:line_item, variant: variant1, price: variant1.price) }- let(:line_item2) { build(:line_item, variant: variant2, price: variant2.price) }+ let(:line_item1) { build(:line_item, variant: variant1, price: 10) }+ let(:line_item2) { build(:line_item, variant: variant2, price: 20) }let(:package) dobuild(:stock_package, contents: inventory_units.map { |iu| ::Spree::Stock::ContentItem.new(iu) })diff --git a/spree/core/spec/models/spree/variant_spec.rb b/spree/core/spec/models/spree/variant_spec.rbindex 1abbf2317d..c84e9c8f25 100644--- a/spree/core/spec/models/spree/variant_spec.rb+++ b/spree/core/spec/models/spree/variant_spec.rb@@ -17,6 +17,7 @@ describe Spree::Variant, type: :model docontext 'validations' doit 'validates price is greater than 0' do+ allow(Spree::Config).to receive(:enable_legacy_default_price).and_return(true)variant.price = -1expect(variant).to be_invalidend@@ -240,7 +241,10 @@ describe Spree::Variant, type: :model dolet(: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)+ expect(product.master.prices.count).to eq(1)+ variant.save!+ variant.set_price(store.default_currency, 19.99)+ expect(product.master.prices.reload.count).to eq(0)endend@@ -1250,31 +1254,31 @@ describe Spree::Variant, type: :model dodescribe 'validate :check_price' dosubject { variant.save }- let(:currency) { store.default_currency }+ before do+ allow(Spree::Config).to receive(:enable_legacy_default_price).and_return(true)+ end- context 'when variant has a default price' do- let(:variant) { build(:variant, product: product, default_price: default_price) }+ let(:currency) { store.default_currency }- let(:product) { create(:product, master: master) }- let(:master) { create(:master_variant, price: 11.11, currency: currency) }+ context 'when variant has a price' do+ let(:variant) { build(:variant, :with_no_price, product: product) }- let(:default_price) { build(:price, amount: 12.34, currency: currency) }+ let(:product) { create(:product, price: 11.11) }- it 'keeps the default price' do+ it 'keeps the existing price' do+ variant.set_price(currency, 12.34)expect(subject).to be(true)expect(variant.price_in(currency).amount).to eq(12.34)end- context 'when the default price is invalid' do- let(:default_price) { build(:price, amount: nil, currency: currency) }-+ context 'when variant has no price' doit 'infers price from the default variant' doexpect(subject).to be(true)expect(variant.price_in(currency).amount).to eq(11.11)end- context 'when there is no default variant' do- let(:product) { nil }+ context 'when there is no product' do+ let(:variant) { build(:variant, :with_no_price, product: nil) }it 'adds an error' doexpect(subject).to be(false)@@ -1289,8 +1293,7 @@ describe Spree::Variant, type: :model docontext 'when variant has no default price' dolet(:variant) { build(:variant, :with_no_price, product: product) }- let(:product) { create(:product, master: master) }- let(:master) { create(:master_variant, price: 11.11, currency: currency) }+ let(:product) { create(:product, price: 11.11) }it 'infers price from the default variant' doexpect(subject).to be(true)diff --git a/spree/core/spec/support/concerns/default_price.rb b/spree/core/spec/support/concerns/default_price.rbindex df54056f7d..b9e1a7d923 100644--- a/spree/core/spec/support/concerns/default_price.rb+++ b/spree/core/spec/support/concerns/default_price.rb@@ -1,8 +1,16 @@shared_examples_for 'default_price' do- subject(:instance) { FactoryBot.build(model.name.demodulize.downcase.to_sym) }+ subject(:instance) do+ obj = FactoryBot.create(model.name.demodulize.downcase.to_sym)+ obj.reload+ obj+ endlet(:model) { described_class }+ before do+ allow(Spree::Config).to receive(:enable_legacy_default_price).and_return(true)+ end+describe '.has_one :default_price' dolet(:default_price_association) { model.reflect_on_association(:default_price) }@@ -28,8 +36,8 @@ shared_examples_for 'default_price' doendit 'delegates price_including_vat_for' do- expect(instance.default_price).to receive(:price_including_vat_for)- instance.price_including_vat_for+ expect(instance.default_price).to receive(:price_including_vat_for).with({})+ instance.price_including_vat_for({})endend