Skip to content

Commit

Permalink
Remove deprecated price_for from Product, Variant and PriceSelector
Browse files Browse the repository at this point in the history
Please, use price_for_options now.

Ref solidusio#3925
  • Loading branch information
kennyadsl committed Apr 14, 2023
1 parent 7b37ac5 commit 4c0501c
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 72 deletions.
1 change: 0 additions & 1 deletion core/app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 1 addition & 17 deletions core/app/models/spree/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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]
Expand Down
15 changes: 1 addition & 14 deletions core/app/models/spree/variant/price_selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
12 changes: 0 additions & 12 deletions core/spec/models/spree/variant/price_selector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) }

Expand Down
28 changes: 0 additions & 28 deletions core/spec/models/spree/variant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand All @@ -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
Expand Down

0 comments on commit 4c0501c

Please sign in to comment.