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
Improve `BaseHelper#spree_storefront_resource_url`
Damian Legawieca8afbbc3e1a7
diff --git a/core/app/helpers/spree/base_helper.rb b/core/app/helpers/spree/base_helper.rbindex ee1ef172bd..b886a22c53 100644--- a/core/app/helpers/spree/base_helper.rb+++ b/core/app/helpers/spree/base_helper.rb@@ -106,7 +106,7 @@ module Spreedescription: [object.name, current_store.meta_description].reject(&:blank?).join(', '))elsemeta.reverse_merge!(keywords: (current_store.meta_keywords || current_store.seo_title),- description: (current_store.seo_meta_description))+ description: current_store.seo_meta_description)endendmeta@@ -164,9 +164,17 @@ module Spreeenddef spree_storefront_resource_url(resource, options = {})- if defined?(locale_param) && locale_param.present?- options.merge!(locale: locale_param)- end+ options.merge!(locale: locale_param) if defined?(locale_param) && locale_param.present?++ store = options[:store] || current_store++ base_url = if options[:relative]+ ''+ elsif store.formatted_custom_domain.blank?+ store.formatted_url+ else+ store.formatted_custom_domain+ endlocalize = if options[:locale].present?"/#{options[:locale]}"@@ -175,13 +183,27 @@ module Spreeendif resource.instance_of?(Spree::Product)- "#{current_store.formatted_url + localize}/#{Spree::Config[:storefront_products_path]}/#{resource.slug}"- elsif resource.instance_of?(Spree::Taxon)- "#{current_store.formatted_url + localize}/#{Spree::Config[:storefront_taxons_path]}/#{resource.permalink}"+ preview_id = ("preview_id=#{options[:preview_id]}" if options[:preview_id].present?)++ variant_id = ("variant_id=#{options[:variant_id]}" if options[:variant_id].present?)++ params = [preview_id, variant_id].compact_blank.join('&')+ params = "?#{params}" if params.present?++ "#{base_url + localize}/products/#{resource.slug}#{params}"+ elsif resource.is_a?(Post)+ preview_id = options[:preview_id].present? ? "?preview_id=#{options[:preview_id]}" : ''+ "#{base_url + localize}/posts/#{resource.slug}#{preview_id}"+ elsif resource.is_a?(Spree::Taxon)+ "#{base_url + localize}/t/#{resource.permalink}"+ elsif resource.is_a?(Spree::Page) || resource.is_a?(ActionText::RichText)+ "#{base_url + localize}#{resource.page_builder_url}"+ elsif resource.is_a?(Spree::PageLink)+ resource.linkable_urlelsif localize.blank?- current_store.formatted_url+ base_urlelse- current_store.formatted_url + localize+ base_url + localizeendend@@ -251,8 +273,8 @@ module Spree# Returns style of image or nildef image_style_from_method_name(method_name)- if method_name.to_s.match(/_image$/) && style = method_name.to_s.sub(/_image$/, '')- style if style.in? Spree::Image.styles.with_indifferent_access+ if method_name.to_s.match(/_image$/) && style = method_name.to_s.sub(/_image$/, '') && (style.in? Spree::Image.styles.with_indifferent_access)+ styleendenddiff --git a/core/spec/helpers/base_helper_spec.rb b/core/spec/helpers/base_helper_spec.rbindex 11ab55f0be..336f2262d3 100644--- a/core/spec/helpers/base_helper_spec.rb+++ b/core/spec/helpers/base_helper_spec.rb@@ -70,6 +70,32 @@ describe Spree::BaseHelper, type: :helper doit { expect(helper.spree_storefront_resource_url(product)).to eq("http://www.example.com/fr/products/#{product.slug}") }end++ context 'when preview_id is not present' do+ it 'returns the product url' do+ expect(spree_storefront_resource_url(product)).to eq("http://#{current_store.url}/products/#{product.slug}")+ end+ end++ context 'when preview_id is present' do+ it 'returns the product preview url' do+ expect(spree_storefront_resource_url(product, preview_id: product.id)).to eq("http://#{current_store.url}/products/#{product.slug}?preview_id=#{product.id}")+ end+ end++ context 'for product with custom domain' do+ let!(:custom_domain) { create :custom_domain, store: current_store }++ it 'returns the product url' do+ expect(spree_storefront_resource_url(product)).to eq("http://#{custom_domain.url}/products/#{product.slug}")+ end+ end++ context 'for product with relative option' do+ it 'returns the product url' do+ expect(spree_storefront_resource_url(product, relative: true)).to eq("/products/#{product.slug}")+ end+ endendcontext 'for Taxon URL' do