Skip to content

Commit

Permalink
Update usage of #price_for to #price_for_options
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Mike Conlin committed Mar 30, 2021
1 parent 9321d04 commit 17a0dad
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 25 deletions.
4 changes: 2 additions & 2 deletions api/app/views/spree/api/products/_product.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions api/app/views/spree/api/variants/_small.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
Expand Down
2 changes: 1 addition & 1 deletion core/app/helpers/spree/base_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion core/app/helpers/spree/products_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <p> tags.
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/line_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

Expand Down
6 changes: 3 additions & 3 deletions core/app/models/spree/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions core/spec/models/spree/line_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

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

Expand Down Expand Up @@ -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 }
Expand All @@ -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 }
Expand Down Expand Up @@ -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? }
Expand All @@ -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? }
Expand Down
6 changes: 3 additions & 3 deletions frontend/app/views/spree/products/_cart_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ul>
<% @product.variants_and_option_values_for(current_pricing_options).each_with_index do |variant, index| %>
<li>
<%= radio_button_tag "variant_id", variant.id, index == 0, 'data-price' => variant.price_for(current_pricing_options) %>
<%= radio_button_tag "variant_id", variant.id, index == 0, 'data-price' => variant.price_for_options(current_pricing_options)&.money %>
<%= label_tag "variant_id_#{ variant.id }" do %>
<span class="variant-description">
<%= variant_options variant %>
Expand All @@ -28,13 +28,13 @@
<%= hidden_field_tag "variant_id", @product.master.id %>
<% end %>

<% if @product.price_for(current_pricing_options) and !@product.price.nil? %>
<% if @product.price_for_options(current_pricing_options) and !@product.price.nil? %>
<div data-hook="product_price" class="columns five <%= !@product.has_variants? ? 'alpha' : 'omega' %>">

<div id="product-price">
<h6 class="product-section-title"><%= t('spree.price') %></h6>
<div>
<span class="price selling" itemprop="price" content="<%= @product.price_for(current_pricing_options).to_d %>">
<span class="price selling" itemprop="price" content="<%= @product.price_for_options(current_pricing_options).amount %>">
<%= display_price(@product) %>
</span>
<span itemprop="priceCurrency" content="<%= current_pricing_options.currency %>"></span>
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/views/spree/shared/_products.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</div>
<%= link_to truncate(product.name, length: 50), url, class: 'info', itemprop: "name", title: product.name %>
<span itemprop="offers" itemscope itemtype="http://schema.org/Offer">
<% if price = product.price_for(current_pricing_options) %>
<% if price = product.price_for_options(current_pricing_options)&.money %>
<span class="price selling" itemprop="price" content="<%= price.to_d %>">
<%= price.to_html %>
</span>
Expand Down

0 comments on commit 17a0dad

Please sign in to comment.