Skip to content

Commit

Permalink
Fix multiple Money gem deprecations.
Browse files Browse the repository at this point in the history
* [DEPRECATION] `use_i18n` is deprecated - use `Money.locale_backend = :i18n` instead

* [DEPRECATION] `html` is deprecated - use `html_wrap` instead. Please note that `html_wrap` will wrap all parts of currency and if you use `with_currency` option, currency element class changes from `currency` to `money-currency`.

* [DEPRECATION] `symbol_position:` option is deprecated - use `format` to specify the formatting template.

* Initialize Money in initializer for main app to override.
  • Loading branch information
JDutil authored and jiz4oh committed Apr 15, 2022
1 parent f786e85 commit 410545c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 37 deletions.
31 changes: 21 additions & 10 deletions backend/spec/features/admin/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
create(:completed_order_with_totals, user: user_a, number: "R456").tap do |o|
li = o.line_items.last
li.update_column(:price, li.price + 10)
o.recalculate
end
end

Expand Down Expand Up @@ -246,14 +247,24 @@ def always_invalid_email
end
end

[:number, :state, :total].each do |attr|
context attr do
it_behaves_like "a sortable attribute" do
let(:text_match_1) { order.send(attr).to_s }
let(:text_match_2) { order_2.send(attr).to_s }
let(:table_id) { "listing_orders" }
let(:sort_link) { "orders_#{attr}_title" }
end
context :number do
it_behaves_like "a sortable attribute" do
let(:text_match_1) { order.number }
let(:text_match_2) { order_2.number }
let(:table_id) { "listing_orders" }
let(:sort_link) { "orders_number_title" }
end
end

context :total do
it_behaves_like "a sortable attribute" do
# Since Spree::Money renders each piece of the total in it's own span,
# we are just checking the dollar total matches. Mainly due to how
# RSpec matcher appear_before works since it can't index the broken up total.
let(:text_match_1) { order.total.to_i.to_s }
let(:text_match_2) { order_2.total.to_i.to_s }
let(:table_id) { "listing_orders" }
let(:sort_link) { "orders_total_title" }
end
end
end
Expand Down Expand Up @@ -293,9 +304,9 @@ def always_invalid_email
within_table('listing_items') do
items.each do |item|
expect(page).to have_selector(".item-name", text: item.product.name)
expect(page).to have_selector(".item-price", text: item.single_money.to_html)
expect(page).to have_selector(".item-price", text: item.single_money.to_html(html_wrap: false))
expect(page).to have_selector(".item-quantity", text: item.quantity)
expect(page).to have_selector(".item-total", text: item.money.to_html)
expect(page).to have_selector(".item-total", text: item.money.to_html(html_wrap: false))
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions core/config/initializers/money.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

require 'money'

Money.locale_backend = :i18n
20 changes: 13 additions & 7 deletions core/lib/spree/money.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,22 @@ def format(options = {})
@money.format(@options.merge(options))
end

# @note If you pass in options, ensure you pass in the html: true as well.
# @note If you pass in options, ensure you pass in the { html_wrap: true } as well.
# @param options [Hash] additional formatting options
# @return [String] the value of this money object formatted according to
# its options and any additional options, by default as html.
def to_html(options = { html: true })
# its options and any additional options, by default with html_wrap.
def to_html(options = { html_wrap: true })
output = format(options)
if options[:html]
# 1) prevent blank, breaking spaces
# 2) prevent escaping of HTML character entities
output = output.sub(" ", " ").html_safe
# Maintain compatibility by checking html option renamed to html_wrap.
if options[:html] || options[:html] == false
Spree::Deprecation.warn <<-WARN.squish, caller
Spree::Money#to_html called with Spree::Money#to_html(html: #{options[:html]}),
which will not be supported in the future.
Instead use :html_wrap e.g. Spree::Money#to_html(html_wrap: #{options[:html]})
WARN
end
if options[:html_wrap] || options[:html]
output = output.html_safe
end
output
end
Expand Down
14 changes: 7 additions & 7 deletions core/spec/helpers/products_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ module Spree
context "when variant is more than master" do
let(:variant_price) { 15 }

it { is_expected.to eq("(Add: $5.00)") }
it { is_expected.to eq("(Add: <span class=\"money-currency-symbol\">$</span><span class=\"money-whole\">5</span><span class=\"money-decimal-mark\">.</span><span class=\"money-decimal\">00</span>)") }
# Regression test for https://github.com/spree/spree/issues/2737
it { is_expected.to be_html_safe }
end

context "when variant is less than master" do
let(:product_price) { 15 }

it { is_expected.to eq("(Subtract: $5.00)") }
it { is_expected.to eq("(Subtract: <span class=\"money-currency-symbol\">$</span><span class=\"money-whole\">5</span><span class=\"money-decimal-mark\">.</span><span class=\"money-decimal\">00</span>)") }
end
end

Expand All @@ -56,13 +56,13 @@ module Spree
context "when variant is more than master" do
let(:variant_price) { 150 }

it { is_expected.to eq("(Add: &#x00A5;50)") }
it { is_expected.to eq("(Add: <span class=\"money-currency-symbol\">&#x00A5;</span><span class=\"money-whole\">50</span>)") }
end

context "when variant is less than master" do
let(:product_price) { 150 }

it { is_expected.to eq("(Subtract: &#x00A5;50)") }
it { is_expected.to eq("(Subtract: <span class=\"money-currency-symbol\">&#x00A5;</span><span class=\"money-whole\">50</span>)") }
end
end
end
Expand All @@ -79,8 +79,8 @@ module Spree

context "when currency is default" do
it "should return the variant price if the price is different than master" do
expect(helper.variant_price(variant)).to eq("$15.00")
expect(helper.variant_price(variant_2)).to eq("$20.00")
expect(helper.variant_price(variant)).to eq("<span class=\"money-currency-symbol\">$</span><span class=\"money-whole\">15</span><span class=\"money-decimal-mark\">.</span><span class=\"money-decimal\">00</span>")
expect(helper.variant_price(variant_2)).to eq("<span class=\"money-currency-symbol\">$</span><span class=\"money-whole\">20</span><span class=\"money-decimal-mark\">.</span><span class=\"money-decimal\">00</span>")
end
end

Expand All @@ -95,7 +95,7 @@ module Spree
end

it "should return the variant price if the price is different than master" do
expect(helper.variant_price(variant)).to eq("&#x00A5;150")
expect(helper.variant_price(variant)).to eq("<span class=\"money-currency-symbol\">&#x00A5;</span><span class=\"money-whole\">150</span>")
end
end

Expand Down
27 changes: 14 additions & 13 deletions core/spec/lib/spree/money_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# coding: utf-8
require 'spec_helper'
# frozen_string_literal: true

require 'rails_helper'

describe Spree::Money do
before do
Expand Down Expand Up @@ -95,7 +96,7 @@

context "with currency" do
it "passed in option" do
money = Spree::Money.new(10, with_currency: true, html: false)
money = Spree::Money.new(10, with_currency: true, html_wrap: false)
expect(money.to_s).to eq("$10.00 USD")
end
end
Expand All @@ -115,27 +116,27 @@
context "currency parameter" do
context "when currency is specified in Canadian Dollars" do
it "uses the currency param over the global configuration" do
money = Spree::Money.new(10, currency: 'CAD', with_currency: true, html: false)
money = Spree::Money.new(10, currency: 'CAD', with_currency: true, html_wrap: false)
expect(money.to_s).to eq("$10.00 CAD")
end
end

context "when currency is specified in Japanese Yen" do
it "uses the currency param over the global configuration" do
money = Spree::Money.new(100, currency: 'JPY', html: false)
money = Spree::Money.new(100, currency: 'JPY', html_wrap: false)
expect(money.to_s).to eq("¥100")
end
end
end

context "symbol positioning" do
it "passed in option" do
money = Spree::Money.new(10, symbol_position: :after, html: false)
money = Spree::Money.new(10, format: '%n %u', html_wrap: false)
expect(money.to_s).to eq("10.00 $")
end

it "config option" do
money = Spree::Money.new(10, symbol_position: :after, html: false)
money = Spree::Money.new(10, format: '%n %u', html_wrap: false)
expect(money.to_s).to eq("10.00 $")
end
end
Expand All @@ -160,7 +161,7 @@
end

it "formats correctly" do
money = Spree::Money.new(1000, html: false)
money = Spree::Money.new(1000, html_wrap: false)
expect(money.to_s).to eq("¥1,000")
end
end
Expand All @@ -174,20 +175,20 @@

# Regression test for https://github.com/spree/spree/issues/2634
it "formats as plain by default" do
money = Spree::Money.new(10, symbol_position: :after)
money = Spree::Money.new(10, format: '%n %u')
expect(money.to_s).to eq("10.00 €")
end

it "formats as HTML if asked (nicely) to" do
money = Spree::Money.new(10, symbol_position: :after)
money = Spree::Money.new(10, format: '%n %u')
# The HTML'ified version of "10.00 €"
expect(money.to_html).to eq("10.00&nbsp;&#x20AC;")
expect(money.to_html).to eq("<span class=\"money-whole\">10</span><span class=\"money-decimal-mark\">.</span><span class=\"money-decimal\">00</span> <span class=\"money-currency-symbol\">&#x20AC;</span>")
end

it "formats as HTML with currency" do
money = Spree::Money.new(10, symbol_position: :after, with_currency: true)
money = Spree::Money.new(10, format: '%n %u', with_currency: true)
# The HTML'ified version of "10.00 €"
expect(money.to_html).to eq("10.00&nbsp;&#x20AC; <span class=\"currency\">EUR</span>")
expect(money.to_html).to eq("<span class=\"money-whole\">10</span><span class=\"money-decimal-mark\">.</span><span class=\"money-decimal\">00</span> <span class=\"money-currency-symbol\">&#x20AC;</span> <span class=\"money-currency\">EUR</span>")
end
end

Expand Down

0 comments on commit 410545c

Please sign in to comment.