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.

Fixed Admin API translation routes for Categories

Damian Legawiec8b2462d20754
diff --git a/spree/api/app/controllers/spree/api/v3/admin/translations_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/translations_controller.rb
index 8010cbf3ad..0ecc1d765c 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/translations_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/translations_controller.rb
@@ -55,8 +55,18 @@ module Spree
parent_lookup&.segment&.pluralize&.to_sym
end
+ # Translatable by class hierarchy, not strict equality, so a subclass
+ # instance (e.g. Spree::Category < Spree::Taxon) passes when its base
+ # is registered.
def ensure_translatable!
- raise ActiveRecord::RecordNotFound unless Spree.translatable_resources.include?(@parent.class)
+ # Compare by class NAME, not object identity: in dev the registry is
+ # populated at boot, but the model constants are reloaded on edits,
+ # so `is_a?(registered_class)` fails against a stale class object even
+ # when @parent's live class (or an ancestor) matches by name. Walk the
+ # ancestor names so a subclass (Spree::Category < Spree::Taxon) passes.
+ registered = Spree.translatable_resources.map(&:name).to_set
+ translatable = @parent.class.ancestors.any? { |ancestor| registered.include?(ancestor.name) }
+ raise ActiveRecord::RecordNotFound unless translatable
end
ParentLookup = Struct.new(:klass, :value, :segment)
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/translations_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/translations_controller_spec.rb
index d7b683b28c..f00009829a 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/translations_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/translations_controller_spec.rb
@@ -57,6 +57,35 @@ RSpec.describe Spree::Api::V3::Admin::TranslationsController, type: :controller
end
end
+ context 'when the parent is a category (Spree::Category < Spree::Taxon)' do
+ let!(:taxonomy) { create(:taxonomy, store: store) }
+ let!(:category) { create(:taxon, name: 'Clothing', taxonomy: taxonomy, parent: taxonomy.root) }
+
+ it 'returns the translation matrix for the category' do
+ get :index, params: { category_id: category.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ data = json_response['data']
+ expect(data['resource_type']).to eq('category')
+ expect(data['resource_id']).to eq(category.prefixed_id)
+ end
+
+ it 'works for a top-level taxonomy root category' do
+ get :index, params: { category_id: taxonomy.root.prefixed_id }, as: :json
+ expect(response).to have_http_status(:ok)
+ end
+
+ it 'resolves a legacy taxonomy-backed taxon whose store_id was never backfilled' do
+ # Regression: for_store must fall back to the taxonomy join for rows with
+ # store_id IS NULL, otherwise the parent lookup 404s.
+ category.update_columns(store_id: nil)
+
+ get :index, params: { category_id: category.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ end
+ end
+
context 'when the parent param resolves to nothing translatable' do
it 'has no route for a non-translatable parent' do
# tax_categories are not in Spree.translatable_resources and the