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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Added Store.available_locales
Damian Legawiec630d8af165ec
diff --git a/core/app/helpers/spree/locale_helper.rb b/core/app/helpers/spree/locale_helper.rbindex d90c7a42ee..28c890d4bf 100644--- a/core/app/helpers/spree/locale_helper.rb+++ b/core/app/helpers/spree/locale_helper.rb@@ -4,6 +4,10 @@ module Spreesupported_locales_for_all_stores.map { |locale| locale_presentation(locale) }end+ def available_locales_options+ available_locales.map { |locale| locale_presentation(locale) }+ end+def locale_presentation(locale)if defined?(SpreeI18n)[Spree.t('i18n.this_file_language', locale: locale), locale]diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rbindex 3901798551..7756a5cd37 100644--- a/core/app/models/spree/store.rb+++ b/core/app/models/spree/store.rb@@ -43,33 +43,39 @@ module Spreeendend+ def self.available_locales+ Rails.cache.fetch('stores_available_locales') do+ Spree::Store.all.map(&:supported_locales_list).flatten.uniq+ end+ end+def supported_currencies_list- (read_attribute(:supported_currencies).to_s.split(',') << default_currency).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.compactenddef supported_locales_list# TODO: add support of multiple supported languages to a single Store- [default_locale].compact.uniq+ @supported_locales_list ||= [default_locale].compact.uniqenddef unique_name- "#{name} (#{code})"+ @unique_name ||= "#{name} (#{code})"enddef formatted_urlreturn if url.blank?- if url.match(/http:\/\/|https:\/\//)- url- else- Rails.env.development? ? "http://#{url}" : "https://#{url}"- end+ @formatted_url ||= if url.match(/http:\/\/|https:\/\//)+ url+ else+ Rails.env.development? ? "http://#{url}" : "https://#{url}"+ endenddef countries_available_for_checkout- checkout_zone_or_default.try(:country_list) || Spree::Country.all+ @countries_available_for_checkout ||= checkout_zone_or_default.try(:country_list) || Spree::Country.allenddef states_available_for_checkout(country)@@ -77,7 +83,7 @@ module Spreeenddef checkout_zone_or_default- checkout_zone || Spree::Zone.default_checkout_zone+ @checkout_zone_or_default ||= checkout_zone || Spree::Zone.default_checkout_zoneendprivate@@ -99,6 +105,7 @@ module Spreedef clear_cacheRails.cache.delete('default_store')+ Rails.cache.delete('stores_available_locales')endendenddiff --git a/core/lib/spree/core/controller_helpers/locale.rb b/core/lib/spree/core/controller_helpers/locale.rbindex 461df1784f..ac57b85512 100644--- a/core/lib/spree/core/controller_helpers/locale.rb+++ b/core/lib/spree/core/controller_helpers/locale.rb@@ -11,6 +11,7 @@ module Spreehelper_method :supported_locales_for_all_storeshelper_method :current_localehelper_method :supported_locale?+ helper_method :available_localesenddef set_locale@@ -46,6 +47,10 @@ module Spree[Rails.application.config.i18n.default_locale, I18n.locale, :en]end).uniq.compactend++ def available_locales+ Spree::Store.available_locales+ endendendenddiff --git a/core/spec/models/spree/store_spec.rb b/core/spec/models/spree/store_spec.rbindex c6c31f48b2..9d92965775 100644--- a/core/spec/models/spree/store_spec.rb+++ b/core/spec/models/spree/store_spec.rb@@ -72,67 +72,14 @@ describe Spree::Store, type: :model doexpect(Spree::Store.default.default).to be(true)endend+ end- context 'when footer info is provided' do- let!(:store) { create(:store, description: 'Some description', address: 'Address street 123, City 17', contact_phone: '123123123', contact_email: '[email protected]') }-- it 'sets footer info fields' do- expect(store.description).to eq('Some description')- expect(store.address).to eq('Address street 123, City 17')- expect(store.contact_phone).to eq('123123123')- end- end-- describe '#unique_name' do- let!(:store) { create(:store) }-- it 'returns the Store Name followed by the Store Code in parentheses' do- expect(store.unique_name).to eq("#{store.name} (#{store.code})")- end- end-- describe '#supported_currencies_list' do- context 'with supported currencies set' do- let(:currencies) { 'USD, EUR, dummy' }- let!(:store) { create(:store, default_currency: 'USD', supported_currencies: currencies) }-- it 'returns supported currencies list' do- expect(store.supported_currencies_list).to contain_exactly(- ::Money::Currency.find('USD'), ::Money::Currency.find('EUR')- )- end- end-- context 'without supported currencies set' do- let!(:store) { create(:store, default_currency: 'EUR', supported_currencies: nil) }-- it 'returns supported currencies list' do- expect(store.supported_currencies_list).to contain_exactly(- ::Money::Currency.find('EUR')- )- end- end- end-- describe '#supported_locales_list' do- context 'with supported locale set' do- let(:store) { create(:store, default_locale: 'fr') }-- it 'returns supported currencies list' do- expect(store.supported_locales_list).to be_an_instance_of(Array)- expect(store.supported_locales_list).to contain_exactly('fr')- end- end-- context 'without supported currencies set' do- let(:store) { create(:store) }+ describe '.available_locales' do+ let!(:store) { create(:store, default_locale: 'en') }+ let!(:store_2) { create(:store, default_locale: 'de') }+ let!(:store_3) { create(:store, default_locale: 'en') }- it 'returns supported currencies list' do- expect(store.supported_locales_list).to be_an_instance_of(Array)- expect(store.supported_locales_list).to be_empty- end- end- end+ it { expect(described_class.available_locales).to contain_exactly('en', 'de') }endshared_context 'with checkout zone set' do@@ -281,4 +228,55 @@ describe Spree::Store, type: :model doendendend++ describe '#unique_name' do+ let!(:store) { create(:store) }++ it 'returns the Store Name followed by the Store Code in parentheses' do+ expect(store.unique_name).to eq("#{store.name} (#{store.code})")+ end+ end++ describe '#supported_currencies_list' do+ context 'with supported currencies set' do+ let(:currencies) { 'USD, EUR, dummy' }+ let!(:store) { create(:store, default_currency: 'USD', supported_currencies: currencies) }++ it 'returns supported currencies list' do+ expect(store.supported_currencies_list).to contain_exactly(+ ::Money::Currency.find('USD'), ::Money::Currency.find('EUR')+ )+ end+ end++ context 'without supported currencies set' do+ let!(:store) { create(:store, default_currency: 'EUR', supported_currencies: nil) }++ it 'returns supported currencies list' do+ expect(store.supported_currencies_list).to contain_exactly(+ ::Money::Currency.find('EUR')+ )+ end+ end+ end++ describe '#supported_locales_list' do+ context 'with supported locale set' do+ let(:store) { create(:store, default_locale: 'fr') }++ it 'returns supported currencies list' do+ expect(store.supported_locales_list).to be_an_instance_of(Array)+ expect(store.supported_locales_list).to contain_exactly('fr')+ end+ end++ context 'without supported currencies set' do+ let(:store) { create(:store) }++ it 'returns supported currencies list' do+ expect(store.supported_locales_list).to be_an_instance_of(Array)+ expect(store.supported_locales_list).to be_empty+ end+ end+ endend