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.

Make translated slugs dependent on store

Rafal Cymerys61d5ca72e97e
diff --git a/api/app/serializers/spree/v2/storefront/product_serializer.rb b/api/app/serializers/spree/v2/storefront/product_serializer.rb
index 106ae9f12b..084612031b 100644
--- a/api/app/serializers/spree/v2/storefront/product_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/product_serializer.rb
@@ -24,10 +24,6 @@ module Spree
product.available?
end
- attribute :localized_slugs do |product|
- product.localized_slugs
- end
-
attribute :currency do |_product, params|
params[:currency]
end
@@ -48,6 +44,10 @@ module Spree
display_compare_at_price(product, params[:currency])
end
+ attribute :localized_slugs do |product, params|
+ product.localized_slugs_for_store(params[:store])
+ end
+
has_many :variants
has_many :option_types
has_many :product_properties
diff --git a/api/app/serializers/spree/v2/storefront/taxon_serializer.rb b/api/app/serializers/spree/v2/storefront/taxon_serializer.rb
index 270abfa997..0c4292ac28 100644
--- a/api/app/serializers/spree/v2/storefront/taxon_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/taxon_serializer.rb
@@ -19,8 +19,8 @@ module Spree
taxon.leaf?
end
- attribute :localized_slugs do |taxon|
- taxon.localized_slugs
+ attribute :localized_slugs do |taxon, params|
+ taxon.localized_slugs_for_store(params[:store])
end
belongs_to :parent, record_type: :taxon, serializer: :taxon
diff --git a/api/spec/requests/spree/api/v2/storefront/products_spec.rb b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
index c9b42e17f8..1e60077f58 100644
--- a/api/spec/requests/spree/api/v2/storefront/products_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
@@ -995,17 +995,26 @@ describe 'API V2 Storefront Products Spec', type: :request do
end
context 'with localized_slugs' do
- let!(:product_with_slug) { create(:product) }
+ let(:store2) { create(:store, default_locale: 'en', supported_locales: 'en,pl,es') }
+ let(:product_with_slug) { create(:product, stores: [store2], slug: 'test_slug_en') }
let!(:translation1) { product_with_slug.translations.create(slug: 'test_slug_pl', locale: 'pl') }
let!(:translation2) { product_with_slug.translations.create(slug: 'test_slug_es', locale: 'es') }
+ let(:expected_result) do
+ {
+ en: 'test_slug_en',
+ pl: 'test_slug_pl',
+ es: 'test_slug_es'
+ }
+ end
+
before do
+ allow_any_instance_of(Spree::Api::V2::Storefront::ProductsController).to receive(:current_store).and_return(store2)
get "/api/v2/storefront/products/#{product_with_slug.id}"
end
it 'returns translated slugs' do
- expect(json_response['data']['attributes']['localized_slugs']).to match(product_with_slug.localized_slugs)
- expect(json_response['data']['attributes']['localized_slugs']).to be_an_instance_of(ActiveSupport::HashWithIndifferentAccess)
+ expect(json_response['data']['attributes']['localized_slugs']).to match(expected_result)
end
end
diff --git a/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb b/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb
index b7eec986dc..69ea5c7540 100644
--- a/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb
@@ -219,16 +219,26 @@ describe 'Taxons Spec', type: :request do
end
context 'with localized_slugs' do
- let!(:taxon_with_slug) { create(:taxon) }
- let!(:translations) { taxon_with_slug.translations.create([{ slug: 'test_slug_pl', locale: 'pl' }, { slug: 'test_slug_es', locale: 'es' } ])}
+ let(:store2) { create(:store, default_locale: 'en', supported_locales: 'en,pl,es') }
+ let(:taxonomy) { create(:taxonomy, name: 'categories', store: store2) }
+ let!(:taxon_with_slug) { create(:taxon, taxonomy: taxonomy, permalink: 'test_slug_en') }
+ let!(:translations) { taxon_with_slug.translations.create([{ permalink: 'test_slug_pl', locale: 'pl' }, { slug: 'test_slug_es', locale: 'es' } ])}
+
+ let(:expected_slugs) do
+ {
+ en: 'categories/test_slug_en',
+ pl: 'test_slug_pl',
+ es: 'test_slug_es'
+ }
+ end
before do
+ allow_any_instance_of(Spree::Api::V2::Storefront::TaxonsController).to receive(:current_store).and_return(store2)
get "/api/v2/storefront/taxons/#{taxon_with_slug.id}"
end
it 'returns translated slugs' do
- expect(json_response['data']['attributes']['localized_slugs']).to match(taxon_with_slug.localized_slugs)
- expect(json_response['data']['attributes']['localized_slugs']).to be_an_instance_of(ActiveSupport::HashWithIndifferentAccess)
+ expect(json_response['data']['attributes']['localized_slugs']).to match(expected_slugs)
end
end
diff --git a/core/app/finders/spree/taxons/find.rb b/core/app/finders/spree/taxons/find.rb
index 8b462cd89d..5b46b19a7e 100644
--- a/core/app/finders/spree/taxons/find.rb
+++ b/core/app/finders/spree/taxons/find.rb
@@ -67,11 +67,11 @@ module Spree
if Rails::VERSION::STRING >= '6.1'
taxons.joins(:parent).
- join_translation_table(Taxon, join_on_table_alias = 'parents_spree_taxons').
+ join_translation_table(Taxon, 'parents_spree_taxons').
where(["#{Taxon.translation_table_alias}.permalink = ?", parent_permalink])
else
taxons.joins("INNER JOIN #{Spree::Taxon.table_name} AS parent_taxon ON parent_taxon.id = #{Spree::Taxon.table_name}.parent_id").
- join_translation_table(Taxon, join_on_table_alias = 'parent_taxon').
+ join_translation_table(Taxon, 'parent_taxon').
where(["#{Taxon.translation_table_alias}.permalink = ?", parent_permalink])
end
end
diff --git a/core/app/models/concerns/spree/translatable_resource_slug.rb b/core/app/models/concerns/spree/translatable_resource_slug.rb
new file mode 100644
index 0000000000..2c9acbc87e
--- /dev/null
+++ b/core/app/models/concerns/spree/translatable_resource_slug.rb
@@ -0,0 +1,17 @@
+module Spree
+ module TranslatableResourceSlug
+ extend ActiveSupport::Concern
+
+ included do
+ def localized_slugs_for_store(store)
+ localized_slugs = Hash[translations.pluck(:locale, :slug)]
+ default_locale = store.default_locale
+ supported_locales = store.supported_locales_list
+
+ supported_locales.each_with_object({}) do |locale, hash|
+ hash[locale] = localized_slugs[locale] || localized_slugs[default_locale]
+ end
+ end
+ end
+ end
+end
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index 59321d2f8e..9fa421806e 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -24,6 +24,7 @@ module Spree
include ProductScopes
include MultiStoreResource
include TranslatableResource
+ include TranslatableResourceSlug
include MemoizedData
include Metadata
if defined?(Spree::Webhooks)
@@ -388,10 +389,6 @@ module Spree
shipping_category&.name == I18n.t('spree.seed.shipping.categories.digital')
end
- def localized_slugs
- Hash[translations.pluck(:locale, :slug)]
- end
-
private
def add_associations_from_prototype
diff --git a/core/app/models/spree/taxon.rb b/core/app/models/spree/taxon.rb
index 30b0088c95..bafd4a70e8 100644
--- a/core/app/models/spree/taxon.rb
+++ b/core/app/models/spree/taxon.rb
@@ -4,6 +4,7 @@ require 'stringex'
module Spree
class Taxon < Spree::Base
include TranslatableResource
+ include TranslatableResourceSlug
include Metadata
if defined?(Spree::Webhooks)
include Spree::Webhooks::HasWebhooks
@@ -115,10 +116,6 @@ module Spree
move_to_child_with_index(parent, idx.to_i) unless new_record?
end
- def localized_slugs
- Hash[translations.pluck(:locale, :slug)]
- end
-
private
def sync_taxonomy_name
diff --git a/core/brakeman.ignore b/core/brakeman.ignore
index f4c5a300df..40ea427af1 100644
--- a/core/brakeman.ignore
+++ b/core/brakeman.ignore
@@ -46,6 +46,29 @@
],
"note": ""
},
+ {
+ "warning_type": "SQL Injection",
+ "warning_code": 0,
+ "fingerprint": "1c12fcb833b0ddffa07880acb7e604922c0d1d52de598316186241baf16551cd",
+ "check_name": "SQL",
+ "message": "Possible SQL injection",
+ "file": "app/finders/spree/taxons/find.rb",
+ "line": 75,
+ "link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
+ "code": "taxons.joins(\"INNER JOIN #{Spree::Taxon.table_name} AS parent_taxon ON parent_taxon.id = #{Spree::Taxon.table_name}.parent_id\").join_translation_table(Taxon, \"parent_taxon\").where([\"#{Taxon.translation_table_alias}.permalink = ?\", parent_permalink])",
+ "render_path": null,
+ "location": {
+ "type": "method",
+ "class": "Spree::Taxons::Find",
+ "method": "by_parent_permalink"
+ },
+ "user_input": "Taxon.translation_table_alias",
+ "confidence": "Weak",
+ "cwe_id": [
+ 89
+ ],
+ "note": ""
+ },
{
"warning_type": "SQL Injection",
"warning_code": 0,
@@ -138,6 +161,29 @@
],
"note": "interpolating table name"
},
+ {
+ "warning_type": "SQL Injection",
+ "warning_code": 0,
+ "fingerprint": "98607ecfb86c2d3c2567390f813861edbc42d6ffa9f482afb7c0b3464eaf6e73",
+ "check_name": "SQL",
+ "message": "Possible SQL injection",
+ "file": "app/models/concerns/spree/translatable_resource_scopes.rb",
+ "line": 18,
+ "link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
+ "code": "joins(\"LEFT OUTER JOIN #{translatable_class::Translation.table_name} #{translatable_class.translation_table_alias}\\n ON #{translatable_class.translation_table_alias}.#{\"#{translatable_class.table_name.singularize}_id\"} = #{(translatable_class.table_name or join_on_table_alias)}.id\\n AND #{translatable_class.translation_table_alias}.locale = '#{Mobility.locale}'\")",
+ "render_path": null,
+ "location": {
+ "type": "method",
+ "class": "Spree::TranslatableResourceScopes",
+ "method": "join_translation_table"
+ },
+ "user_input": "translatable_class.translation_table_alias",
+ "confidence": "Medium",
+ "cwe_id": [
+ 89
+ ],
+ "note": ""
+ },
{
"warning_type": "SQL Injection",
"warning_code": 0,
@@ -191,7 +237,7 @@
"check_name": "SQL",
"message": "Possible SQL injection",
"file": "app/models/spree/variant.rb",
- "line": 127,
+ "line": 126,
"link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
"code": "joins(:product).join_translation_table(Product).where(\"LOWER(#{Product.translation_table_alias}.name) LIKE LOWER(:query)\\n OR LOWER(sku) LIKE LOWER(:query)\", :query => (\"%#{query}%\"))",
"render_path": null,
@@ -210,21 +256,21 @@
{
"warning_type": "SQL Injection",
"warning_code": 0,
- "fingerprint": "e7aa61c87f26af5f63f00e9791954d787a559dab1eadf8926861796237bd431d",
+ "fingerprint": "ed253ae6b1b4ea3fe3d87d3652380fecab80133319b1ed041d98d163fd16b815",
"check_name": "SQL",
"message": "Possible SQL injection",
- "file": "app/models/concerns/spree/translatable_resource_scopes.rb",
- "line": 10,
+ "file": "app/finders/spree/taxons/find.rb",
+ "line": 71,
"link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
- "code": "joins(\"LEFT OUTER JOIN #{translatable_class::Translation.table_name} #{translatable_class.translation_table_alias}\\n ON #{translatable_class.translation_table_alias}.#{\"#{translatable_class.table_name.singularize}_id\"} = #{translatable_class.table_name}.id\\n AND #{translatable_class.translation_table_alias}.locale = '#{Mobility.locale}'\")",
+ "code": "taxons.joins(:parent).join_translation_table(Taxon, \"parents_spree_taxons\").where([\"#{Taxon.translation_table_alias}.permalink = ?\", parent_permalink])",
"render_path": null,
"location": {
"type": "method",
- "class": "Spree::TranslatableResourceScopes",
- "method": "join_translation_table"
+ "class": "Spree::Taxons::Find",
+ "method": "by_parent_permalink"
},
- "user_input": "translatable_class.translation_table_alias",
- "confidence": "Medium",
+ "user_input": "Taxon.translation_table_alias",
+ "confidence": "Weak",
"cwe_id": [
89
],
@@ -277,6 +323,6 @@
"note": ""
}
],
- "updated": "2023-02-24 11:32:53 +0100",
+ "updated": "2023-03-22 20:11:32 +0100",
"brakeman_version": "5.4.1"
}
diff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rb
index 9081b32d26..2e4e0ce947 100644
--- a/core/spec/models/spree/product_spec.rb
+++ b/core/spec/models/spree/product_spec.rb
@@ -918,4 +918,43 @@ describe Spree::Product, type: :model do
end
end
end
+
+ describe '#localized_slugs_for_store' do
+ let(:store) { create(:store, default_locale: 'fr', supported_locales: 'en,pl,fr') }
+ let(:product) { create(:product, stores: [store], slug: 'test_slug_en') }
+ let!(:product_translation_fr) { product.translations.create(slug: 'test_slug_fr', locale: 'fr') }
+
+ subject { product.localized_slugs_for_store(store) }
+
+ context 'when there are slugs in locales not supported by the store' do
+ let!(:product_translation_pl) { product.translations.create(slug: 'test_slug_pl', locale: 'pl') }
+ let!(:product_translation_de) { product.translations.create(slug: 'test_slug_de', locale: 'de') }
+
+ let(:expected_slugs) do
+ {
+ 'en' => 'test_slug_en',
+ 'fr' => 'test_slug_fr',
+ 'pl' => 'test_slug_pl'
+ }
+ end
+
+ it 'returns only slugs in locales supported by the store' do
+ expect(subject).to match(expected_slugs)
+ end
+ end
+
+ context 'when one of the supported locales does not have a translation' do
+ let(:expected_slugs) do
+ {
+ 'en' => 'test_slug_en',
+ 'fr' => 'test_slug_fr',
+ 'pl' => 'test_slug_fr'
+ }
+ end
+
+ it "falls back to store's default locale" do
+ expect(subject).to match(expected_slugs)
+ end
+ end
+ end
end
diff --git a/core/spec/models/spree/taxon_spec.rb b/core/spec/models/spree/taxon_spec.rb
index 43e152be7a..a8d31175da 100644
--- a/core/spec/models/spree/taxon_spec.rb
+++ b/core/spec/models/spree/taxon_spec.rb
@@ -182,4 +182,44 @@ describe Spree::Taxon, type: :model do
end
end
end
+
+ describe '#localized_slugs_for_store' do
+ let(:store) { create(:store, default_locale: 'fr', supported_locales: 'en,pl,fr') }
+ let(:taxonomy) { create(:taxonomy, name: 'categories', store: store) }
+ let(:taxon) { create(:taxon, taxonomy: taxonomy, permalink: 'test_slug_en') }
+ let!(:taxon_translation_fr) { taxon.translations.create(slug: 'test_slug_fr', locale: 'fr') }
+
+ subject { taxon.localized_slugs_for_store(store) }
+
+ context 'when there are slugs in locales not supported by the store' do
+ let!(:taxon_translation_pl) { taxon.translations.create(slug: 'test_slug_pl', locale: 'pl') }
+ let!(:taxon_translation_de) { taxon.translations.create(slug: 'test_slug_de', locale: 'de') }
+
+ let(:expected_slugs) do
+ {
+ 'en' => 'categories/test_slug_en',
+ 'fr' => 'test_slug_fr',
+ 'pl' => 'test_slug_pl'
+ }
+ end
+
+ it 'returns only slugs in locales supported by the store' do
+ expect(subject).to match(expected_slugs)
+ end
+ end
+
+ context 'when one of the supported locales does not have a translation' do
+ let(:expected_slugs) do
+ {
+ 'en' => 'categories/test_slug_en',
+ 'fr' => 'test_slug_fr',
+ 'pl' => 'test_slug_fr'
+ }
+ end
+
+ it "falls back to store's default locale" do
+ expect(subject).to match(expected_slugs)
+ end
+ end
+ end
end