From 17a0dad86d1ee19eb26bdc1f90391fe781aabb97 Mon Sep 17 00:00:00 2001 From: Mike Conlin Date: Thu, 25 Mar 2021 10:33:16 -0700 Subject: [PATCH] Update usage of #price_for to #price_for_options The #price_for method will be deprecated in favor of the price_for_options method, which returns a Spree::Price. Converting the price to a Spree::Money object is now a frontend concern. --- .../views/spree/api/products/_product.json.jbuilder | 4 ++-- .../views/spree/api/variants/_small.json.jbuilder | 4 ++-- core/app/helpers/spree/base_helper.rb | 2 +- core/app/helpers/spree/products_helper.rb | 2 +- core/app/models/spree/line_item.rb | 4 ++-- core/app/models/spree/variant.rb | 6 +++--- core/spec/models/spree/line_item_spec.rb | 8 ++++---- core/spec/models/spree/variant_spec.rb | 12 ++++++------ .../app/views/spree/products/_cart_form.html.erb | 6 +++--- frontend/app/views/spree/shared/_products.html.erb | 2 +- 10 files changed, 25 insertions(+), 25 deletions(-) diff --git a/api/app/views/spree/api/products/_product.json.jbuilder b/api/app/views/spree/api/products/_product.json.jbuilder index 4984a4107a1..f6f3725364f 100644 --- a/api/app/views/spree/api/products/_product.json.jbuilder +++ b/api/app/views/spree/api/products/_product.json.jbuilder @@ -4,8 +4,8 @@ json.cache! [I18n.locale, @current_user_roles.include?('admin'), current_pricing_options, @product_attributes, @exclude_data, product] do json.(product, *(@product_attributes - [:total_on_hand])) json.total_on_hand(total_on_hand_for(product)) - json.price(product.price_for(current_pricing_options).try(:to_d)) - json.display_price(product.price_for(current_pricing_options).to_s) + json.price(product.price_for_options(current_pricing_options)&.amount) + json.display_price(product.price_for_options(current_pricing_options)&.money&.to_s) @exclude_data ||= {} unless @exclude_data[:variants] diff --git a/api/app/views/spree/api/variants/_small.json.jbuilder b/api/app/views/spree/api/variants/_small.json.jbuilder index d74f80e1661..d807e82ec3b 100644 --- a/api/app/views/spree/api/variants/_small.json.jbuilder +++ b/api/app/views/spree/api/variants/_small.json.jbuilder @@ -2,8 +2,8 @@ json.cache! [I18n.locale, current_pricing_options, variant] do json.(variant, *variant_attributes) - json.price(variant.price_for(current_pricing_options).try(:to_d)) - json.display_price(variant.price_for(current_pricing_options).to_s) + json.price(variant.price_for_options(current_pricing_options)&.amount) + json.display_price(variant.price_for_options(current_pricing_options)&.money&.to_s) json.options_text(variant.options_text) json.track_inventory(variant.should_track_inventory?) json.in_stock(variant.in_stock?) diff --git a/core/app/helpers/spree/base_helper.rb b/core/app/helpers/spree/base_helper.rb index f3974d1e080..e41ec1cad7c 100644 --- a/core/app/helpers/spree/base_helper.rb +++ b/core/app/helpers/spree/base_helper.rb @@ -130,7 +130,7 @@ def seo_url(taxon) end def display_price(product_or_variant) - product_or_variant.price_for(current_pricing_options).to_html + product_or_variant.price_for_options(current_pricing_options)&.money&.to_html end def pretty_time(time, format = :long) diff --git a/core/app/helpers/spree/products_helper.rb b/core/app/helpers/spree/products_helper.rb index a000b405966..18100b17ac5 100644 --- a/core/app/helpers/spree/products_helper.rb +++ b/core/app/helpers/spree/products_helper.rb @@ -38,7 +38,7 @@ def variant_full_price(variant) .with_prices(current_pricing_options) .all? { |variant_with_prices| variant_with_prices.price_same_as_master?(current_pricing_options) } - variant.price_for(current_pricing_options).to_html + variant.price_for_options(current_pricing_options)&.money&.to_html end # Converts line breaks in product description into

tags. diff --git a/core/app/models/spree/line_item.rb b/core/app/models/spree/line_item.rb index dc5c284f925..719a40ed8ec 100644 --- a/core/app/models/spree/line_item.rb +++ b/core/app/models/spree/line_item.rb @@ -123,7 +123,7 @@ def options=(options = {}) # a price for this line item, even if there is no existing price # for the associated line item in the order currency. unless options.key?(:price) || options.key?('price') - self.money_price = variant.price_for(pricing_options) + self.money_price = variant.price_for_options(pricing_options)&.money end end @@ -149,7 +149,7 @@ def set_required_attributes # Set price, cost_price and currency. def set_pricing_attributes self.cost_price ||= variant.cost_price - self.money_price = variant.price_for(pricing_options) if price.nil? + self.money_price = variant.price_for_options(pricing_options)&.money if price.nil? true end diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb index 9d400abc50d..d4a31ee43b2 100644 --- a/core/app/models/spree/variant.rb +++ b/core/app/models/spree/variant.rb @@ -283,10 +283,10 @@ def 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(pricing_options) - variant_price = price_for(pricing_options) + master_price = product.master.price_for_options(pricing_options) + variant_price = price_for_options(pricing_options) return unless master_price && variant_price - variant_price - master_price + Spree::Money.new(variant_price.amount - master_price.amount, currency: pricing_options.currency) end def price_same_as_master?(pricing_options = Spree::Config.default_pricing_options) diff --git a/core/spec/models/spree/line_item_spec.rb b/core/spec/models/spree/line_item_spec.rb index 26ee5900971..55c8b5e6d90 100644 --- a/core/spec/models/spree/line_item_spec.rb +++ b/core/spec/models/spree/line_item_spec.rb @@ -104,10 +104,10 @@ let(:variant) { Spree::Variant.new(product: Spree::Product.new) } let(:line_item) { Spree::LineItem.new(order: order, variant: variant) } - before { expect(variant).to receive(:price_for).at_least(:once).and_return(price) } + before { expect(variant).to receive(:price_for_options).at_least(:once).and_return(price) } context "when a price exists in order currency" do - let(:price) { Spree::Money.new(99.00, currency: "RUB") } + let(:price) { Spree::Price.new(amount: 99.00, currency: "RUB") } it "is a valid line item" do expect(line_item.valid?).to be_truthy @@ -140,8 +140,8 @@ it "sets price anyway, retrieving it from line item options" do expect(line_item.variant) - .to receive(:price_for) - .and_return(Spree::Money.new(123, currency: "USD")) + .to receive(:price_for_options) + .and_return(Spree::Price.new(amount: 123, currency: "USD")) line_item.options = options diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rb index 7f1d8dbb5a3..0d8c19e4923 100644 --- a/core/spec/models/spree/variant_spec.rb +++ b/core/spec/models/spree/variant_spec.rb @@ -236,8 +236,8 @@ end it "displays default price" do - expect(variant.price_for(pricing_options_united_states).to_s).to eq("$19.99") - expect(variant.price_for(pricing_options_germany).to_s).to eq("€29.99") + expect(variant.price_for_options(pricing_options_united_states).money.to_s).to eq("$19.99") + expect(variant.price_for_options(pricing_options_germany).money.to_s).to eq("€29.99") end end @@ -331,7 +331,7 @@ let(:variant) { create(:variant, product: product, price: 35) } before do - allow(product.master).to receive(:price_for).and_return(nil) + allow(product.master).to receive(:price_for_options).and_return(nil) end it { is_expected.to be_nil } @@ -342,7 +342,7 @@ let(:variant) { create(:variant, product: product, price: 35) } before do - allow(variant).to receive(:price_for).and_return(nil) + allow(variant).to receive(:price_for_options).and_return(nil) end it { is_expected.to be_nil } @@ -373,7 +373,7 @@ let(:variant) { create(:variant, price: 10, product: master.product) } before do - allow(master).to receive(:price_for).and_return(nil) + allow(master).to receive(:price_for_options).and_return(nil) end subject { variant.price_same_as_master? } @@ -386,7 +386,7 @@ let(:variant) { create(:variant, price: 10, product: master.product) } before do - allow(variant).to receive(:price_for).and_return(nil) + allow(variant).to receive(:price_for_options).and_return(nil) end subject { variant.price_same_as_master? } diff --git a/frontend/app/views/spree/products/_cart_form.html.erb b/frontend/app/views/spree/products/_cart_form.html.erb index a1d20e1d804..072b800b125 100644 --- a/frontend/app/views/spree/products/_cart_form.html.erb +++ b/frontend/app/views/spree/products/_cart_form.html.erb @@ -7,7 +7,7 @@