Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Don't add `default_currency` to `supported_currencies` in the `preferred_currencies` helper (#12253)

Damian Kaczmarczyk52d4910ca9f3
diff --git a/admin/app/helpers/spree/admin/currencies_helper.rb b/admin/app/helpers/spree/admin/currencies_helper.rb
index 0bcd43ce08..6ab4006342 100644
--- a/admin/app/helpers/spree/admin/currencies_helper.rb
+++ b/admin/app/helpers/spree/admin/currencies_helper.rb
@@ -2,7 +2,7 @@ module Spree
module Admin
module CurrenciesHelper
def preferred_currencies
- @preferred_currencies ||= ([current_store.default_currency] + current_store.supported_currencies_list).uniq
+ @preferred_currencies ||= current_store.supported_currencies_list
end
def currency_money(currency = current_currency)
diff --git a/admin/spec/helpers/spree/admin/currencies_helper_spec.rb b/admin/spec/helpers/spree/admin/currencies_helper_spec.rb
new file mode 100644
index 0000000000..26887cc3e8
--- /dev/null
+++ b/admin/spec/helpers/spree/admin/currencies_helper_spec.rb
@@ -0,0 +1,38 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Admin::CurrenciesHelper, type: :helper do
+ let(:store) { create(:store, supported_currencies: 'USD,EUR,GBP') }
+
+ before do
+ allow(helper).to receive(:current_store).and_return(store)
+ allow(helper).to receive(:current_currency).and_return('USD')
+ end
+
+ describe '#preferred_currencies' do
+ it 'returns array of supported currencies from current store as `Money::Currency`' do
+ expect(helper.preferred_currencies).to eq([Money::Currency.find('USD'), Money::Currency.find('EUR'), Money::Currency.find('GBP')])
+ end
+ end
+
+ describe '#currency_money' do
+ it 'returns Money::Currency instance for given currency' do
+ expect(helper.currency_money('EUR')).to be_a(Money::Currency)
+ expect(helper.currency_money('EUR').iso_code).to eq('EUR')
+ end
+
+ it 'uses current_currency when no argument is provided' do
+ expect(helper.currency_money.iso_code).to eq('USD')
+ end
+ end
+
+ describe '#currency_symbol' do
+ it 'returns symbol for given currency' do
+ expect(helper.currency_symbol('USD')).to eq('$')
+ expect(helper.currency_symbol('EUR')).to eq('€')
+ end
+
+ it 'uses current_currency when no argument is provided' do
+ expect(helper.currency_symbol).to eq('$')
+ end
+ end
+end
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb
index c2f834cea2..dfb413c2e0 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -157,9 +157,9 @@ module Spree
end
def supported_currencies_list
- @supported_currencies_list ||= (read_attribute(:supported_currencies).to_s.split(',') << default_currency).sort.map(&:to_s).map do |code|
+ @supported_currencies_list ||= (read_attribute(:supported_currencies).to_s.split(',') << default_currency).map(&:to_s).map do |code|
::Money::Currency.find(code.strip)
- end.uniq.compact
+ end.uniq.compact.sort_by { |currency| currency.iso_code == default_currency ? 0 : 1 }
end
def homepage(requested_locale)