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.

Added StoreHelper#store_link

Damian Legawiec2083f87b3dbb
diff --git a/frontend/app/helpers/spree/store_helper.rb b/frontend/app/helpers/spree/store_helper.rb
index 76be480c3c..6e3bab088c 100644
--- a/frontend/app/helpers/spree/store_helper.rb
+++ b/frontend/app/helpers/spree/store_helper.rb
@@ -1,27 +1,24 @@
module Spree
module StoreHelper
- def store_country_iso(store)
- store ||= current_store
- return unless store
- return unless store.default_country
+ def store_country_iso(store = nil)
+ store ||= current_store if defined?(current_store)
- store.default_country.iso.downcase
+ store&.default_country&.iso&.downcase
end
def stores
@stores ||= Spree::Store.includes(:default_country).order(:id)
end
- def store_currency_symbol(store)
- store ||= current_store
- return unless store
- return unless store.default_currency
+ def store_currency_symbol(store = nil)
+ store ||= current_store if defined?(current_store)
+ return unless store&.default_currency
::Money::Currency.find(store.default_currency).symbol
end
- def store_locale_name(store)
- store ||= current_store
+ def store_locale_name(store = nil)
+ store ||= current_store if defined?(current_store)
return unless store
return store.name if store.default_locale.blank?
@@ -31,5 +28,12 @@ module Spree
def should_render_store_chooser?
Spree::Config.show_store_selector && stores.size > 1
end
+
+ def store_link(store = nil, html_opts = {})
+ store ||= current_store if defined?(current_store)
+ return unless store
+
+ link_to "#{store_locale_name(store)} (#{store_currency_symbol(store)})", store.formatted_url, **html_opts
+ end
end
end
diff --git a/frontend/spec/helpers/store_helper_spec.rb b/frontend/spec/helpers/store_helper_spec.rb
index 158129c79b..7198ba3925 100644
--- a/frontend/spec/helpers/store_helper_spec.rb
+++ b/frontend/spec/helpers/store_helper_spec.rb
@@ -2,8 +2,8 @@ require 'spec_helper'
module Spree
describe StoreHelper, type: :helper do
- let(:germany) { create(:country, name: 'Germany', iso: 'GR') }
- let(:eu_store) { create(:store, default_currency: 'EUR', default_locale: 'de', default_country: germany) }
+ let(:germany) { build(:country, name: 'Germany', iso: 'GR') }
+ let(:eu_store) { build(:store, url: 'eu.spreecommerce.org', default_currency: 'EUR', default_locale: 'de', default_country: germany) }
before do
I18n.backend.store_translations(:de,
@@ -26,16 +26,25 @@ module Spree
it { expect(store_country_iso(eu_store)).to eq('gr') }
it { expect(store_country_iso(Spree::Store.default)).to be_nil }
+ it { expect { store_country_iso(nil) }.not_to raise_error }
end
describe '#store_currency_symbol' do
it { expect(store_currency_symbol(Spree::Store.default)).to eq('$') }
it { expect(store_currency_symbol(eu_store)).to eq('€') }
+ it { expect { store_currency_symbol(nil) }.not_to raise_error }
end
describe '#store_locale_name' do
it { expect(store_locale_name(Spree::Store.default)).to eq('English (US)') }
it { expect(store_locale_name(eu_store)).to eq('Deutsch (DE)') }
+ it { expect { store_locale_name(nil) }.not_to raise_error }
+ end
+
+ describe '#store_link' do
+ it { expect(store_link(eu_store)).to eq('<a href="https://eu.spreecommerce.org">Deutsch (DE) (€)</a>') }
+ it { expect(store_link(eu_store, class: 'some-class')).to eq('<a class="some-class" href="https://eu.spreecommerce.org">Deutsch (DE) (€)</a>') }
+ it { expect { store_link(nil) }.not_to raise_error }
end
describe '#should_render_store_chooser?' do