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.

Fix `store#formatted_url` when app is on production env (#12532)

Damian Kaczmarczykc9f20b6e96e8
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb
index ffb16d88db..5b919414dc 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -263,25 +263,29 @@ module Spree
end
def formatted_url
- @formatted_url ||= if Rails.env.development? || Rails.env.test?
- scheme = Rails.application.routes.default_url_options[:protocol] || :http
- port = Rails.application.routes.default_url_options[:port].presence || (Rails.env.development? ? 3000 : nil)
+ @formatted_url ||= begin
clean_url = url.to_s.sub(%r{^https?://}, '').split(':').first
- if scheme.to_sym == :https
- URI::HTTPS.build(
- host: clean_url,
- port: port
- ).to_s
+
+ if Rails.env.development? || Rails.env.test?
+ scheme = Rails.application.routes.default_url_options[:protocol] || :http
+ port = Rails.application.routes.default_url_options[:port].presence || (Rails.env.development? ? 3000 : nil)
+
+ if scheme.to_sym == :https
+ URI::HTTPS.build(
+ host: clean_url,
+ port: port
+ ).to_s
+ else
+ URI::HTTP.build(
+ host: clean_url,
+ port: port
+ ).to_s
+ end
else
- URI::HTTP.build(
- host: clean_url,
- port: port
+ URI::HTTPS.build(
+ host: clean_url
).to_s
end
- else
- URI::HTTPS.build(
- host: clean_url
- ).to_s
end
end
diff --git a/core/spec/models/spree/store_spec.rb b/core/spec/models/spree/store_spec.rb
index 4d6ace3af9..f0d986cd8e 100644
--- a/core/spec/models/spree/store_spec.rb
+++ b/core/spec/models/spree/store_spec.rb
@@ -794,6 +794,12 @@ describe Spree::Store, type: :model do
it { expect(store.formatted_url).to eq('http://localhost:3000') }
end
+
+ context 'on production' do
+ before { allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new('production')) }
+
+ it { expect(store.formatted_url).to eq('https://mystore.mydomain.dev') }
+ end
end
describe '#formatted_url_or_custom_domain' do