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.

API: fixed Taxon's `header_url` to always use CDN when generating ima… (#12930)

Damian Legawiecb0d3e584bf8b
diff --git a/api/app/helpers/spree/api/v2/store_media_serializer_images_concern.rb b/api/app/helpers/spree/api/v2/store_media_serializer_images_concern.rb
index 768a893b08..80da812016 100644
--- a/api/app/helpers/spree/api/v2/store_media_serializer_images_concern.rb
+++ b/api/app/helpers/spree/api/v2/store_media_serializer_images_concern.rb
@@ -9,7 +9,6 @@ module Spree
attachment = store.send(attribute_name)
return unless attachment.attached?
- url_helpers = Rails.application.routes.url_helpers
url_helpers.cdn_image_url(attachment)
end
diff --git a/api/app/serializers/spree/api/v2/base_serializer.rb b/api/app/serializers/spree/api/v2/base_serializer.rb
index a7bc0485cc..7e04d8eb6b 100644
--- a/api/app/serializers/spree/api/v2/base_serializer.rb
+++ b/api/app/serializers/spree/api/v2/base_serializer.rb
@@ -33,6 +33,10 @@ module Spree
super(opts, fieldset, include_list, params)
end
+
+ def self.url_helpers
+ Rails.application.routes.url_helpers
+ end
end
end
end
diff --git a/api/app/serializers/spree/api/v2/platform/digital_serializer.rb b/api/app/serializers/spree/api/v2/platform/digital_serializer.rb
index e3a03c795c..66206b5673 100644
--- a/api/app/serializers/spree/api/v2/platform/digital_serializer.rb
+++ b/api/app/serializers/spree/api/v2/platform/digital_serializer.rb
@@ -6,7 +6,6 @@ module Spree
set_type :digital
attribute :url do |digital|
- url_helpers = Rails.application.routes.url_helpers
url_helpers.polymorphic_url(digital.attachment, only_path: true)
end
diff --git a/api/app/serializers/spree/api/v2/platform/taxon_serializer.rb b/api/app/serializers/spree/api/v2/platform/taxon_serializer.rb
index 0b0d7034c2..2432aeaed0 100644
--- a/api/app/serializers/spree/api/v2/platform/taxon_serializer.rb
+++ b/api/app/serializers/spree/api/v2/platform/taxon_serializer.rb
@@ -12,7 +12,7 @@ module Spree
end
attribute :header_url do |taxon|
- taxon.image.attachment&.url
+ url_helpers.cdn_image_url(taxon.image.attachment) if taxon.image.present? && taxon.image.attached?
end
attribute :is_root do |taxon|
diff --git a/api/app/serializers/spree/v2/storefront/taxon_serializer.rb b/api/app/serializers/spree/v2/storefront/taxon_serializer.rb
index e9b1a13316..44a0987156 100644
--- a/api/app/serializers/spree/v2/storefront/taxon_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/taxon_serializer.rb
@@ -16,7 +16,7 @@ module Spree
end
attribute :header_url do |taxon|
- taxon.image.attachment&.url
+ url_helpers.cdn_image_url(taxon.image.attachment) if taxon.image.present? && taxon.image.attached?
end
attribute :is_root do |taxon|
diff --git a/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb
index 2fa80cc459..d2c2d555fd 100644
--- a/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb
@@ -8,6 +8,7 @@ describe Spree::Api::V2::Platform::TaxonSerializer do
let(:taxonomy) { create(:taxonomy, store: store) }
let(:taxon) { create(:taxon, :with_description, taxonomy: taxonomy, products: create_list(:product, 2, stores: [store])) }
let!(:children) { create_list(:taxon, 2, taxonomy: taxonomy, parent: taxon) }
+ let(:url_helpers) { Rails.application.routes.url_helpers }
context 'without products' do
it do
@@ -37,7 +38,7 @@ describe Spree::Api::V2::Platform::TaxonSerializer do
automatic: taxon.automatic?,
sort_order: taxon.sort_order,
rules_match_policy: taxon.rules_match_policy,
- header_url: taxon.image.attachment&.url,
+ header_url: nil,
public_metadata: {},
private_metadata: {}
},
@@ -111,7 +112,7 @@ describe Spree::Api::V2::Platform::TaxonSerializer do
automatic: taxon.automatic?,
sort_order: taxon.sort_order,
rules_match_policy: taxon.rules_match_policy,
- header_url: taxon.image.attachment&.url,
+ header_url: nil,
public_metadata: {},
private_metadata: {}
},
@@ -165,5 +166,13 @@ describe Spree::Api::V2::Platform::TaxonSerializer do
end
end
+ context 'with header image' do
+ let(:taxon) { create(:taxon, :with_header_image, taxonomy: taxonomy) }
+
+ it do
+ expect(subject[:data][:attributes][:header_url]).to eq(url_helpers.cdn_image_url(taxon.image.attachment))
+ end
+ end
+
it_behaves_like 'an ActiveJob serializable hash'
end
diff --git a/api/spec/serializers/spree/v2/storefront/taxon_serializer_spec.rb b/api/spec/serializers/spree/v2/storefront/taxon_serializer_spec.rb
new file mode 100644
index 0000000000..c9a92f27e5
--- /dev/null
+++ b/api/spec/serializers/spree/v2/storefront/taxon_serializer_spec.rb
@@ -0,0 +1,174 @@
+require 'spec_helper'
+
+describe Spree::V2::Storefront::TaxonSerializer do
+ subject { described_class.new(taxon, params: serializer_params).serializable_hash }
+
+ include_context 'API v2 serializers params'
+
+ let(:taxonomy) { create(:taxonomy, store: store) }
+ let(:taxon) { create(:taxon, :with_description, taxonomy: taxonomy, products: create_list(:product, 2, stores: [store])) }
+ let!(:children) { create_list(:taxon, 2, taxonomy: taxonomy, parent: taxon) }
+ let(:url_helpers) { Rails.application.routes.url_helpers }
+
+ before do
+ taxon.reload # Reload taxon to ensure all associations are loaded
+ end
+
+ context 'without products' do
+ it do
+ expect(subject).to match(
+ {
+ data: {
+ id: taxon.id.to_s,
+ type: :taxon,
+ attributes: {
+ name: taxon.name,
+ pretty_name: taxon.pretty_name,
+ permalink: taxon.permalink,
+ seo_title: taxon.seo_title,
+ meta_title: taxon.meta_title,
+ meta_description: taxon.meta_description,
+ meta_keywords: taxon.meta_keywords,
+ left: taxon.left,
+ right: taxon.right,
+ position: taxon.position,
+ depth: taxon.depth,
+ updated_at: taxon.updated_at,
+ public_metadata: {},
+ description: taxon.description.to_plain_text,
+ has_products: taxon.active_products_with_descendants.exists?,
+ header_url: nil,
+ is_root: taxon.root?,
+ is_child: taxon.child?,
+ is_leaf: taxon.leaf?,
+ localized_slugs: taxon.localized_slugs_for_store(store)
+ },
+ relationships: {
+ parent: {
+ data: {
+ id: taxon.parent.id.to_s,
+ type: :taxon
+ }
+ },
+ taxonomy: {
+ data: {
+ id: taxon.taxonomy.id.to_s,
+ type: :taxonomy
+ }
+ },
+ children: {
+ data: [
+ {
+ id: taxon.children.first.id.to_s,
+ type: :taxon
+ },
+ {
+ id: taxon.children.second.id.to_s,
+ type: :taxon
+ }
+ ]
+ },
+ image: {
+ data: {
+ id: taxon.icon.id.to_s,
+ type: :taxon_image
+ }
+ }
+ }
+ }
+ }
+ )
+ end
+ end
+
+ context 'with products' do
+ before do
+ serializer_params[:include_products] = true
+ end
+
+ it do
+ expect(subject).to match(
+ {
+ data: {
+ id: taxon.id.to_s,
+ type: :taxon,
+ attributes: {
+ name: taxon.name,
+ pretty_name: taxon.pretty_name,
+ permalink: taxon.permalink,
+ seo_title: taxon.seo_title,
+ meta_title: taxon.meta_title,
+ meta_description: taxon.meta_description,
+ meta_keywords: taxon.meta_keywords,
+ left: taxon.left,
+ right: taxon.right,
+ position: taxon.position,
+ depth: taxon.depth,
+ updated_at: taxon.updated_at,
+ public_metadata: {},
+ description: taxon.description.to_plain_text,
+ has_products: taxon.active_products_with_descendants.exists?,
+ header_url: nil,
+ is_root: taxon.root?,
+ is_child: taxon.child?,
+ is_leaf: taxon.leaf?,
+ localized_slugs: taxon.localized_slugs_for_store(store)
+ },
+ relationships: {
+ parent: {
+ data: {
+ id: taxon.parent.id.to_s,
+ type: :taxon
+ }
+ },
+ taxonomy: {
+ data: {
+ id: taxon.taxonomy.id.to_s,
+ type: :taxonomy
+ }
+ },
+ children: {
+ data: contain_exactly(
+ {
+ id: taxon.children.first.id.to_s,
+ type: :taxon
+ },
+ {
+ id: taxon.children.second.id.to_s,
+ type: :taxon
+ }
+ )
+ },
+ products: {
+ data: [
+ {
+ id: taxon.products.first.id.to_s,
+ type: :product
+ },
+ {
+ id: taxon.products.second.id.to_s,
+ type: :product
+ }
+ ]
+ },
+ image: {
+ data: {
+ id: taxon.icon.id.to_s,
+ type: :taxon_image
+ }
+ }
+ }
+ }
+ }
+ )
+ end
+ end
+
+ context 'with header image' do
+ let(:taxon) { create(:taxon, :with_header_image, taxonomy: taxonomy) }
+
+ it do
+ expect(subject[:data][:attributes][:header_url]).to eq(url_helpers.cdn_image_url(taxon.image.attachment))
+ end
+ end
+end
diff --git a/core/lib/spree/testing_support/factories/taxon_factory.rb b/core/lib/spree/testing_support/factories/taxon_factory.rb
index c2d6817867..702df9d04a 100644
--- a/core/lib/spree/testing_support/factories/taxon_factory.rb
+++ b/core/lib/spree/testing_support/factories/taxon_factory.rb
@@ -9,6 +9,12 @@ FactoryBot.define do
trait :with_description do
description { '<div>Test <strong>description</strong></div>' }
end
+
+ trait :with_header_image do
+ after(:create) do |taxon|
+ taxon.image.attach(io: File.new(Spree::Core::Engine.root.join('spec', 'fixtures', 'thinking-cat.jpg')), filename: 'thinking-cat.jpg')
+ end
+ end
end
factory :automatic_taxon, parent: :taxon do