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: looked up translation targets through the current store

Damian Legawiecd0da45308df8
diff --git a/spree/api/app/controllers/spree/api/v3/admin/translations/batches_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/translations/batches_controller.rb
index 5d8168c18e..cc49caf05b 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/translations/batches_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/translations/batches_controller.rb
@@ -39,7 +39,7 @@ module Spree
raw = params[:translations]
return render_empty_batch_error unless raw.is_a?(Array) && raw.any?
- batch = Spree::Translations::Batch.new(batch_params)
+ batch = Spree::Translations::Batch.new(batch_params, store: current_store)
return unless require_batch_scopes!(batch)
records = batch.process! { |record| authorize!(:update, record) }
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 75a7831c2a..3685f4e886 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
@@ -43,7 +43,7 @@ module Spree
# translation tables.
def parent_relation
klass = parent_lookup.klass
- relation = klass.respond_to?(:for_store) ? klass.for_store(current_store) : klass
+ relation = klass.respond_to?(:translatable_scope) ? klass.translatable_scope(current_store) : klass.for_store(current_store)
children = klass.try(:translatable_children)
children ? relation.includes(children => :translations) : relation
diff --git a/spree/core/app/models/concerns/spree/translatable_resource.rb b/spree/core/app/models/concerns/spree/translatable_resource.rb
index 349aba9e55..a91e865cc1 100644
--- a/spree/core/app/models/concerns/spree/translatable_resource.rb
+++ b/spree/core/app/models/concerns/spree/translatable_resource.rb
@@ -48,13 +48,12 @@ module Spree
# Every record of this type belonging to +store+, for surfaces that read
# a whole translatable resource type at once (the coverage grid).
#
- # +for_store+ resolves through the store's own association, so it is
- # right for a store column (Product), a polymorphic owner (Policy) and
- # genuinely global reference data alike (OptionType, which has no store
- # dimension and returns everything by design). Its one blind spot is
- # +Spree::Store+: with no `stores` association to follow it falls back to
- # the UNSCOPED class, which would answer with every store in the
- # installation, so that case is narrowed here.
+ # +for_store+ resolves through the store's own association, or through a
+ # model's own +for_store+ scope where there is none to follow (OptionType
+ # by its store column, OptionValue through its option type). Its one
+ # blind spot is +Spree::Store+: with no `stores` association to follow it
+ # falls back to the UNSCOPED class, which would answer with every store in
+ # the installation, so that case is narrowed here.
#
# @param store [Spree::Store]
# @return [ActiveRecord::Relation]
diff --git a/spree/core/app/models/spree/option_value.rb b/spree/core/app/models/spree/option_value.rb
index 6ea189fd02..460d027c11 100644
--- a/spree/core/app/models/spree/option_value.rb
+++ b/spree/core/app/models/spree/option_value.rb
@@ -48,6 +48,13 @@ module Spree
distinct
}
+ # Option values are owned through their option type, and the store has no
+ # `option_values` association for `Spree::Base.for_store` to follow, which
+ # would otherwise fall back to every store's values.
+ scope :for_store, lambda { |store|
+ joins(:option_type).where(Spree::OptionType.table_name => { store_id: store.id })
+ }
+
scope :for_products, lambda { |products|
# we need to use map(&:id) to avoid SQL errors when merging with other scopes
joins(:variants).where(Spree::Variant.table_name => { product_id: products.map(&:id) })
diff --git a/spree/core/app/models/spree/translations/batch.rb b/spree/core/app/models/spree/translations/batch.rb
index bac8da2f16..427de82148 100644
--- a/spree/core/app/models/spree/translations/batch.rb
+++ b/spree/core/app/models/spree/translations/batch.rb
@@ -38,8 +38,10 @@ module Spree
attr_reader :records
# @param entries [Array<Hash>] [{ resource_type:, resource_id:, values: { locale => { field => value } } }]
- def initialize(entries)
+ # @param store [Spree::Store] the store every record must belong to
+ def initialize(entries, store: Spree::Store.current)
@entries = Array(entries)
+ @store = store
@records = []
end
@@ -86,7 +88,7 @@ module Spree
klass = resource_class(entry[:resource_type])
raise EntryError.new("Unknown translatable resource type: #{entry[:resource_type]}", index) if klass.nil?
- relation = klass.respond_to?(:for_store) ? klass.for_store(Spree::Store.current) : klass
+ relation = klass.respond_to?(:translatable_scope) ? klass.translatable_scope(@store) : klass.for_store(@store)
relation.find_by_prefix_id!(entry[:resource_id])
rescue ActiveRecord::RecordNotFound
raise EntryError.new("Resource not found: #{entry[:resource_id]}", index)
diff --git a/spree/core/spec/models/spree/translations/batch_spec.rb b/spree/core/spec/models/spree/translations/batch_spec.rb
index 284ea2e13e..8155831cd3 100644
--- a/spree/core/spec/models/spree/translations/batch_spec.rb
+++ b/spree/core/spec/models/spree/translations/batch_spec.rb
@@ -58,6 +58,18 @@ RSpec.describe Spree::Translations::Batch do
expect { batch.process! }.to raise_error(described_class::EntryError)
end
+ # Neither a store nor an option value can be found through a `stores` or
+ # `option_values` association, so both have to be narrowed explicitly.
+ it "raises EntryError for another store's store or option value" do
+ other_store = create(:store)
+ foreign_value = create(:option_value, option_type: create(:option_type, store: other_store))
+
+ [entry('store', other_store, 'de' => { 'name' => 'X' }),
+ entry('option_value', foreign_value, 'de' => { 'label' => 'X' })].each do |foreign|
+ expect { described_class.new([foreign], store: store).process! }.to raise_error(described_class::EntryError)
+ end
+ end
+
it 'raises EmptyError when there are no entries' do
expect { described_class.new([]).process! }.to raise_error(described_class::EmptyError)
end