From 36cd10f755210dc0e3ca5de8b3856b78801d6d58 Mon Sep 17 00:00:00 2001 From: Alberto Vena Date: Tue, 28 Mar 2023 09:34:06 +0200 Subject: [PATCH] Remove deprecated price_for from Product, Variant and PriceSelector Please, use price_for_options now. Ref https://github.com/solidusio/solidus/pull/3925 --- core/app/models/spree/product.rb | 1 - core/app/models/spree/variant.rb | 18 +----------- .../models/spree/variant/price_selector.rb | 15 +--------- .../spree/variant/price_selector_spec.rb | 12 -------- core/spec/models/spree/variant_spec.rb | 28 ------------------- 5 files changed, 2 insertions(+), 72 deletions(-) diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb index 4feae6e507d..12390266901 100644 --- a/core/app/models/spree/product.rb +++ b/core/app/models/spree/product.rb @@ -95,7 +95,6 @@ def find_or_build_master :display_price, :has_default_price?, :images, - :price_for, :price_for_options, :rebuild_vat_prices=, to: :find_or_build_master diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb index bb0d78f7373..3a9c8741e99 100644 --- a/core/app/models/spree/variant.rb +++ b/core/app/models/spree/variant.rb @@ -39,6 +39,7 @@ class Variant < Spree::Base delegate :shipping_category, :shipping_category_id, to: :product, prefix: true delegate :tax_rates, to: :tax_category + delegate :price_for_options, to: :price_selector has_many :inventory_units, inverse_of: :variant has_many :line_items, inverse_of: :variant @@ -287,12 +288,6 @@ def price_selector @price_selector ||= Spree::Config.variant_price_selector_class.new(self) end - # Chooses an appropriate price for the given pricing options - # This has been deprecated in favor of #price_for_options. - # - # @see Spree::Variant::PriceSelector#price_for_options - delegate :price_for, to: :price_selector - # Returns the difference in price from the master variant def price_difference_from_master(pricing_options = Spree::Config.default_pricing_options) master_price = product.master.price_for_options(pricing_options) @@ -306,17 +301,6 @@ def price_same_as_master?(pricing_options = Spree::Config.default_pricing_option diff && diff.zero? end - def price_for_options(price_options) - if price_selector.respond_to?(:price_for_options) - price_selector.price_for_options(price_options) - else - money = price_for(price_options) - return if money.nil? - - Spree::Price.new(amount: money.to_d, variant: self, currency: price_options.currency) - end - end - # Generates a friendly name and sku string. # # @return [String] diff --git a/core/app/models/spree/variant/price_selector.rb b/core/app/models/spree/variant/price_selector.rb index cb9566f2e86..b61d1acc5cb 100644 --- a/core/app/models/spree/variant/price_selector.rb +++ b/core/app/models/spree/variant/price_selector.rb @@ -3,7 +3,7 @@ module Spree class Variant < Spree::Base # This class is responsible for selecting a price for a variant given certain pricing options. - # A variant can have multiple or even dynamic prices. The `price_for` + # A variant can have multiple or even dynamic prices. The `price_for_options` # method determines which price applies under the given circumstances. # class PriceSelector @@ -22,19 +22,6 @@ def initialize(variant) @variant = variant end - # The variant's price, given a set of pricing options - # @param [Spree::Variant::PricingOptions] price_options Pricing Options to abide by - # @return [Spree::Money, nil] The most specific price for this set of pricing options. - def price_for(price_options) - Spree::Deprecation.warn( - "price_for is deprecated and will be removed. The price_for method - should return a Spree::Price as described. Please use - #price_for_options and adjust your frontend code to explicitly call - &.money where required" - ) - price_for_options(price_options)&.money - end - # The variant's Spree::Price record, given a set of pricing options # @param [Spree::Variant::PricingOptions] price_options Pricing Options to abide by # @return [Spree::Price, nil] The most specific price for this set of pricing options. diff --git a/core/spec/models/spree/variant/price_selector_spec.rb b/core/spec/models/spree/variant/price_selector_spec.rb index fe0dfca5717..e94c9ed2757 100644 --- a/core/spec/models/spree/variant/price_selector_spec.rb +++ b/core/spec/models/spree/variant/price_selector_spec.rb @@ -8,7 +8,6 @@ subject { described_class.new(variant) } it { is_expected.to respond_to(:variant) } - it { is_expected.to respond_to(:price_for) } it { is_expected.to respond_to(:price_for_options) } describe ".pricing_options_class" do @@ -17,17 +16,6 @@ end end - describe "#price_for(options)" do - let(:variant) { create(:variant, price: 12.34) } - let(:pricing_options) { described_class.pricing_options_class.new(currency: "USD") } - - it "returns the correct (default) price as a Spree::Money object", :aggregate_failures do - expect(Spree::Deprecation).to receive(:warn). - with(/^price_for is deprecated and will be removed/, any_args) - expect(subject.price_for(pricing_options)).to eq(Spree::Money.new(12.34, currency: "USD")) - end - end - describe "#price_for_options(options)" do let(:variant) { create(:variant, price: 12.34) } diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rb index 9fda49e4224..6f350844afc 100644 --- a/core/spec/models/spree/variant_spec.rb +++ b/core/spec/models/spree/variant_spec.rb @@ -376,21 +376,6 @@ end end - context "#price_for(price_options)" do - let(:price_options) { Spree::Config.variant_price_selector_class.pricing_options_class.new } - - it "calls the price selector with the given options object" do - expect(variant.price_selector).to receive(:price_for).with(price_options) - variant.price_for(price_options) - end - - it "returns a Spree::Money object with a deprecation warning", :aggregate_failures do - expect(Spree::Deprecation).to receive(:warn). - with(/^price_for is deprecated and will be removed/, any_args) - expect(variant.price_for(price_options)).to eq Spree::Money.new(19.99) - end - end - context "#price_for_options(price_options)" do subject { variant.price_for_options(price_options) } @@ -405,19 +390,6 @@ expect(subject).to be_a Spree::Price expect(subject.amount).to eq 19.99 end - - context "when the price_selector does not implement #price_for_options" do - before do - allow(variant.price_selector).to receive(:respond_to?).with(:price_for_options).and_return false - end - - it "returns an unpersisted Spree::Price", :aggregate_failures do - expect(Spree::Deprecation).to receive(:warn). - with(/^price_for is deprecated and will be removed/, any_args) - expect(subject).to be_a Spree::Price - expect(subject.amount).to eq 19.99 - end - end end context "#price_difference_from_master" do