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
Fix image urls in the products export (#12887)
Łukasz Adamczykbec20c1b2735
diff --git a/core/app/helpers/spree/images_helper.rb b/core/app/helpers/spree/images_helper.rbindex 322c27d613..d968743ec0 100644--- a/core/app/helpers/spree/images_helper.rb+++ b/core/app/helpers/spree/images_helper.rb@@ -15,21 +15,22 @@ module Spreeenddef spree_image_url(image, options = {})+ url_helpers = respond_to?(:main_app) ? main_app : Rails.application.routes.url_helperswidth = options[:width]height = options[:height]width = width * 2 if width.present?height = height * 2 if height.present?- return unless image.attached?- return unless image.variable?+ return unless image&.attached?+ return unless image&.variable?if width.present? && height.present?- main_app.cdn_image_url(+ url_helpers.cdn_image_url(image.variant(spree_image_variant_options(resize_to_fill: [width, height])))else- main_app.cdn_image_url(+ url_helpers.cdn_image_url(image.variant(spree_image_variant_options(resize_to_limit: [width, height])))enddiff --git a/core/app/presenters/spree/csv/product_variant_presenter.rb b/core/app/presenters/spree/csv/product_variant_presenter.rbindex 85092c1bce..3ec2dc6779 100644--- a/core/app/presenters/spree/csv/product_variant_presenter.rb+++ b/core/app/presenters/spree/csv/product_variant_presenter.rb@@ -1,6 +1,8 @@module Spreemodule CSVclass ProductVariantPresenter+ include Spree::ImagesHelper+CSV_HEADERS = ['product_id','sku',@@ -99,9 +101,9 @@ module Spreevariant.backorderable?,variant.tax_category&.name,variant.digital?,- variant.images[0]&.original_url,- variant.images[1]&.original_url,- variant.images[2]&.original_url,+ spree_image_url(variant.images[0], image_url_options),+ spree_image_url(variant.images[1], image_url_options),+ spree_image_url(variant.images[2], image_url_options),index.positive? ? option_type(0)&.presentation : nil,index.positive? ? option_value(option_type(0)) : nil,index.positive? ? option_type(1)&.presentation : nil,@@ -125,6 +127,15 @@ module Spreedef option_value(option_type)variant.option_values.find { |ov| ov.option_type == option_type }&.presentationend++ private++ def image_url_options+ {+ width: 1000,+ height: 1000+ }+ endendendenddiff --git a/core/config/routes.rb b/core/config/routes.rbindex ab595d7e32..b4e980619b 100644--- a/core/config/routes.rb+++ b/core/config/routes.rb@@ -5,6 +5,7 @@ Rails.application.routes.draw doopts = options.slice(:protocol, :host, :port)opts[:host] = Spree.cdn_host if Spree.cdn_host.present?opts[:host] ||= Rails.application.routes.default_url_options[:host]+ opts[:host] ||= Spree::Store.current.url_or_custom_domain if Spree::Store.current.present?opts[:only_path] = true if opts[:host].blank?diff --git a/core/spec/helpers/images_helper_spec.rb b/core/spec/helpers/images_helper_spec.rbindex a4b085f0be..4be0e7dcf3 100644--- a/core/spec/helpers/images_helper_spec.rb+++ b/core/spec/helpers/images_helper_spec.rb@@ -48,7 +48,7 @@ describe Spree::ImagesHelper, type: :helper doit 'returns a url with resize_to_fill' dovariant = double('variant')expect(image).to receive(:variant).with(hash_including(resize_to_fill: [200, 200])).and_return(variant)- expect(helper).to receive(:main_app).and_return(double('main_app', cdn_image_url: 'cdn_url'))+ expect(Rails.application.routes.url_helpers).to receive(:cdn_image_url).and_return('cdn_url')expect(helper.spree_image_url(image, width: 100, height: 100)).to eq('cdn_url')endend@@ -57,7 +57,7 @@ describe Spree::ImagesHelper, type: :helper doit 'returns a url with resize_to_limit' dovariant = double('variant')expect(image).to receive(:variant).with(hash_including(resize_to_limit: [200, nil])).and_return(variant)- expect(helper).to receive(:main_app).and_return(double('main_app', cdn_image_url: 'cdn_url'))+ expect(Rails.application.routes.url_helpers).to receive(:cdn_image_url).and_return('cdn_url')expect(helper.spree_image_url(image, width: 100)).to eq('cdn_url')endenddiff --git a/core/spec/presenters/spree/csv/product_variant_presenter_spec.rb b/core/spec/presenters/spree/csv/product_variant_presenter_spec.rbindex fae88645ad..03a6b4df8f 100644--- a/core/spec/presenters/spree/csv/product_variant_presenter_spec.rb+++ b/core/spec/presenters/spree/csv/product_variant_presenter_spec.rb@@ -43,9 +43,9 @@ RSpec.describe Spree::CSV::ProductVariantPresenter doexpect(subject[26]).to eq variant.backorderable?expect(subject[27]).to eq variant.tax_category&.nameexpect(subject[28]).to eq variant.digital?- expect(subject[29]).to eq variant.images[0].original_url- expect(subject[30]).to eq variant.images[1].original_url- expect(subject[31]).to eq variant.images[2].original_url+ expect(subject[29]).to end_with(variant.images[0].filename.to_s)+ expect(subject[30]).to end_with(variant.images[1].filename.to_s)+ expect(subject[31]).to end_with(variant.images[2].filename.to_s)expect(subject[32]).to eq nilexpect(subject[33]).to eq nilexpect(subject[34]).to eq nil@@ -84,6 +84,44 @@ RSpec.describe Spree::CSV::ProductVariantPresenter doexpect(subject[37]).to eq nilendend++ describe 'images host' do+ context 'when default host is set' do+ before do+ allow(Rails.application.routes).to receive(:default_url_options).and_return({ host: 'test.host' })+ end++ it 'returns images with default host' do+ expect(subject[29]).to start_with('http://test.host')+ expect(subject[30]).to start_with('http://test.host')+ expect(subject[31]).to start_with('http://test.host')+ end+ end++ context 'when there is no default host' do+ before do+ allow(Rails.application.routes).to receive(:default_url_options).and_return({})+ end++ it 'returns images with the store url' do+ expect(subject[29]).to start_with("http://#{store.url}")+ expect(subject[30]).to start_with("http://#{store.url}")+ expect(subject[31]).to start_with("http://#{store.url}")+ end++ context 'when custom domain is set' do+ let!(:custom_domain) { create(:custom_domain, store: store, url: 'custom.domain') }++ before { store.reload }++ it 'returns images with the custom domain' do+ expect(subject[29]).to start_with('http://custom.domain')+ expect(subject[30]).to start_with('http://custom.domain')+ expect(subject[31]).to start_with('http://custom.domain')+ end+ end+ end+ endenddescribe '#option_type' do