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.

Translations for Taxonomies (#11845)

nciemniak4f523d7ed69c
diff --git a/api/spec/integration/api/v2/platform/taxonomies_spec.rb b/api/spec/integration/api/v2/platform/taxonomies_spec.rb
index 6ec35dfb59..728ff598fb 100644
--- a/api/spec/integration/api/v2/platform/taxonomies_spec.rb
+++ b/api/spec/integration/api/v2/platform/taxonomies_spec.rb
@@ -11,7 +11,12 @@ describe 'Taxonomies API', swagger: true do
let(:id) { create(:taxonomy, store: store).id }
let(:records_list) { create_list(:taxonomy, 2) }
- let(:valid_create_param_value) { build(:taxonomy).attributes }
+ let(:valid_create_param_value) do
+ {
+ name: 'First Taxonomy',
+ store: Spree::Store.default
+ }
+ end
let(:valid_update_param_value) do
{
name: 'Categories',
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index 99367998d0..47dcf3f63a 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -41,7 +41,11 @@ module Spree
TRANSLATABLE_FIELDS = %i[name description slug meta_description meta_keywords meta_title].freeze
translates(*TRANSLATABLE_FIELDS)
- self::Translation.class_eval { acts_as_paranoid }
+ self::Translation.class_eval do
+ acts_as_paranoid
+ # deleted translation values also need to be accessible for index views listing deleted resources
+ default_scope { unscope(where: :deleted_at) }
+ end
friendly_id :slug_candidates, use: [:history, :mobility]
acts_as_paranoid
@@ -355,11 +359,16 @@ module Spree
end
def brand
- @brand ||= taxons.joins(:taxonomy).find_by(spree_taxonomies: { name: Spree.t(:taxonomy_brands_name) })
+ @brand ||= taxons.joins(:taxonomy).
+ join_translation_table(Taxonomy).
+ find_by(Taxonomy.translation_table_alias => { name: Spree.t(:taxonomy_brands_name) })
end
def category
- @category ||= taxons.joins(:taxonomy).order(depth: :desc).find_by(spree_taxonomies: { name: Spree.t(:taxonomy_categories_name) })
+ @category ||= taxons.joins(:taxonomy).
+ join_translation_table(Taxonomy).
+ order(depth: :desc).
+ find_by(Taxonomy.translation_table_alias => { name: Spree.t(:taxonomy_categories_name) })
end
def taxons_for_store(store)
diff --git a/core/app/models/spree/taxon.rb b/core/app/models/spree/taxon.rb
index 6589fbf9a1..a17ce3cb2c 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 TranslatableResourceScopes
include Metadata
if defined?(Spree::Webhooks)
include Spree::Webhooks::HasWebhooks
diff --git a/core/app/models/spree/taxonomy.rb b/core/app/models/spree/taxonomy.rb
index 4af1ae5437..f41d598f5a 100644
--- a/core/app/models/spree/taxonomy.rb
+++ b/core/app/models/spree/taxonomy.rb
@@ -1,10 +1,14 @@
module Spree
class Taxonomy < Spree::Base
+ include TranslatableResource
include Metadata
if defined?(Spree::Webhooks)
include Spree::Webhooks::HasWebhooks
end
+ TRANSLATABLE_FIELDS = %i[name]
+ translates(*TRANSLATABLE_FIELDS)
+
acts_as_list
validates :name, presence: true, uniqueness: { case_sensitive: false, allow_blank: true, scope: :store_id }
diff --git a/core/brakeman.ignore b/core/brakeman.ignore
index a072ae9c04..146de579c7 100644
--- a/core/brakeman.ignore
+++ b/core/brakeman.ignore
@@ -69,6 +69,29 @@
],
"note": ""
},
+ {
+ "warning_type": "SQL Injection",
+ "warning_code": 0,
+ "fingerprint": "857c335935a00f584137f31dbcb1a4532af5c8bb5cf53a86058b4af98c6597dc",
+ "check_name": "SQL",
+ "message": "Possible SQL injection",
+ "file": "lib/spree/translation_migrations.rb",
+ "line": 21,
+ "link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
+ "code": "ActiveRecord::Base.connection.execute(\"\\n UPDATE #{resource_class.table_name}\\n SET #{resource_class.translatable_fields.map do\n \"#{f}=null\"\n end.join(\", \")};\\n \")",
+ "render_path": null,
+ "location": {
+ "type": "method",
+ "class": "Spree::TranslationMigrations",
+ "method": "transfer_translation_data"
+ },
+ "user_input": "resource_class.translatable_fields.map do\n \"#{f}=null\"\n end.join(\", \")",
+ "confidence": "Medium",
+ "cwe_id": [
+ 89
+ ],
+ "note": ""
+ },
{
"warning_type": "SQL Injection",
"warning_code": 0,
@@ -115,6 +138,29 @@
],
"note": "interpolating table name"
},
+ {
+ "warning_type": "SQL Injection",
+ "warning_code": 0,
+ "fingerprint": "c1c97347a2d74ea41d46519e3bfbd94c511a1bd9c285f3f2a1fa0cb7e624d232",
+ "check_name": "SQL",
+ "message": "Possible SQL injection",
+ "file": "lib/spree/translation_migrations.rb",
+ "line": 32,
+ "link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
+ "code": "ActiveRecord::Base.connection.execute(\"\\n UPDATE #{resource_class.table_name}\\n SET (#{resource_class.translatable_fields.join(\", \")}) = #{(\"ROW\" or \"\")}(#{resource_class.translatable_fields.map do\n \"#{resource_class::Translation.table_name}.#{f}\"\n end.join(\", \")})\\n FROM #{resource_class::Translation.table_name}\\n WHERE #{resource_class::Translation.table_name}.#{\"#{resource_class.table_name.singularize}_id\"} = #{resource_class.table_name}.id\\n \")",
+ "render_path": null,
+ "location": {
+ "type": "method",
+ "class": "Spree::TranslationMigrations",
+ "method": "revert_translation_data_transfer"
+ },
+ "user_input": "resource_class.translatable_fields.join(\", \")",
+ "confidence": "Medium",
+ "cwe_id": [
+ 89
+ ],
+ "note": ""
+ },
{
"warning_type": "SQL Injection",
"warning_code": 0,
@@ -183,8 +229,31 @@
89
],
"note": "interpolating table name"
+ },
+ {
+ "warning_type": "SQL Injection",
+ "warning_code": 0,
+ "fingerprint": "f14dd62fac0dd1e9d5532dd5efc770e2eb873a8db80faf366b6295378634754a",
+ "check_name": "SQL",
+ "message": "Possible SQL injection",
+ "file": "lib/spree/translation_migrations.rb",
+ "line": 16,
+ "link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
+ "code": "ActiveRecord::Base.connection.execute(\"\\n INSERT INTO #{resource_class::Translation.table_name} (#{resource_class.translatable_fields.join(\", \")}, #{\"#{resource_class.table_name.singularize}_id\"}, locale, created_at, updated_at)\\n SELECT #{resource_class.translatable_fields.join(\", \")}, id, '#{default_locale}' as locale, created_at, updated_at FROM #{resource_class.table_name};\\n \")",
+ "render_path": null,
+ "location": {
+ "type": "method",
+ "class": "Spree::TranslationMigrations",
+ "method": "transfer_translation_data"
+ },
+ "user_input": "resource_class.translatable_fields.join(\", \")",
+ "confidence": "Medium",
+ "cwe_id": [
+ 89
+ ],
+ "note": ""
}
],
- "updated": "2023-01-13 11:44:19 +0100",
+ "updated": "2023-01-18 14:40:41 +0100",
"brakeman_version": "5.4.0"
}
diff --git a/core/db/migrate/20220706112554_create_product_name_and_description_translations_for_mobility_table_backend.rb b/core/db/migrate/20220706112554_create_product_name_and_description_translations_for_mobility_table_backend.rb
index fd00e191c8..1e732aeb24 100644
--- a/core/db/migrate/20220706112554_create_product_name_and_description_translations_for_mobility_table_backend.rb
+++ b/core/db/migrate/20220706112554_create_product_name_and_description_translations_for_mobility_table_backend.rb
@@ -2,8 +2,10 @@ class CreateProductNameAndDescriptionTranslationsForMobilityTableBackend < Activ
def change
# create translation table only if spree_globalize has not already created it
if ActiveRecord::Base.connection.table_exists? 'spree_product_translations'
- # replacing this with index on spree_product_id and locale
- remove_index :spree_product_translations, name: "index_spree_product_translations_on_spree_product_id", if_exists: true
+ # manually check for index since Rails if_exists does not always work correctly
+ if ActiveRecord::Migration.connection.index_exists?(:spree_product_translations, :spree_product_id)
+ remove_index :spree_product_translations, name: "index_spree_product_translations_on_spree_product_id", if_exists: true
+ end
else
create_table :spree_product_translations do |t|
diff --git a/core/db/migrate/20220718100743_create_spree_taxon_name_and_description_translations_for_mobility_table_backend.rb b/core/db/migrate/20220718100743_create_spree_taxon_name_and_description_translations_for_mobility_table_backend.rb
index 4b654a69f8..1ffa0f7737 100644
--- a/core/db/migrate/20220718100743_create_spree_taxon_name_and_description_translations_for_mobility_table_backend.rb
+++ b/core/db/migrate/20220718100743_create_spree_taxon_name_and_description_translations_for_mobility_table_backend.rb
@@ -2,8 +2,11 @@ class CreateSpreeTaxonNameAndDescriptionTranslationsForMobilityTableBackend < Ac
def change
# create translation table only if spree_globalize has not already created it
if ActiveRecord::Base.connection.table_exists? 'spree_taxon_translations'
- # replacing this with index on spree_taxon_id and locale
- remove_index :spree_taxon_translations, name: "index_spree_taxon_translations_on_spree_taxon_id", if_exists: true
+ # manually check for index since Rails if_exists does not always work correctly
+ if ActiveRecord::Migration.connection.index_exists?(:spree_taxon_translations, :spree_taxon_id)
+ # replacing this with index on spree_taxon_id and locale
+ remove_index :spree_taxon_translations, name: "index_spree_taxon_translations_on_spree_taxon_id", if_exists: true
+ end
else
create_table :spree_taxon_translations do |t|
# Translated attribute(s)
diff --git a/core/db/migrate/20230103144439_create_option_type_translations.rb b/core/db/migrate/20230103144439_create_option_type_translations.rb
index 359fb2bc67..5f798dc909 100644
--- a/core/db/migrate/20230103144439_create_option_type_translations.rb
+++ b/core/db/migrate/20230103144439_create_option_type_translations.rb
@@ -1,7 +1,10 @@
class CreateOptionTypeTranslations < ActiveRecord::Migration[6.1]
def change
if ActiveRecord::Base.connection.table_exists? 'spree_option_type_translations'
- remove_index :spree_option_type_translations, name: "index_spree_option_type_translations_on_spree_option_type_id", if_exists: true
+ # manually check for index since Rails if_exists does not always work correctly
+ if ActiveRecord::Migration.connection.index_exists?(:spree_option_type_translations, :spree_option_type_id)
+ remove_index :spree_option_type_translations, name: "index_spree_option_type_translations_on_spree_option_type_id", if_exists: true
+ end
else
create_table :spree_option_type_translations do |t|
diff --git a/core/db/migrate/20230103151034_create_option_value_translations.rb b/core/db/migrate/20230103151034_create_option_value_translations.rb
index 2acb37a311..b04a264de7 100644
--- a/core/db/migrate/20230103151034_create_option_value_translations.rb
+++ b/core/db/migrate/20230103151034_create_option_value_translations.rb
@@ -1,7 +1,10 @@
class CreateOptionValueTranslations < ActiveRecord::Migration[6.1]
def change
if ActiveRecord::Base.connection.table_exists? 'spree_option_value_translations'
- remove_index :spree_option_value_translations, name: "index_spree_option_value_translations_on_spree_option_value_id", if_exists: true
+ # manually check for index since Rails if_exists does not always work correctly
+ if ActiveRecord::Migration.connection.index_exists?(:spree_option_value_translations, :spree_option_value_id)
+ remove_index :spree_option_value_translations, column: :spree_option_value_id, if_exists: true
+ end
else
create_table :spree_option_value_translations do |t|
diff --git a/core/db/migrate/20230109084253_create_product_property_translations.rb b/core/db/migrate/20230109084253_create_product_property_translations.rb
index e100182a89..511ee40328 100644
--- a/core/db/migrate/20230109084253_create_product_property_translations.rb
+++ b/core/db/migrate/20230109084253_create_product_property_translations.rb
@@ -1,7 +1,10 @@
class CreateProductPropertyTranslations < ActiveRecord::Migration[6.1]
def change
if ActiveRecord::Base.connection.table_exists? 'spree_product_property_translations'
- remove_index :spree_product_property_translations, column: :spree_product_property_id, if_exists: true
+ # manually check for index since Rails if_exists does not always work correctly
+ if ActiveRecord::Migration.connection.index_exists?(:spree_product_property_translations, :spree_product_property_id)
+ remove_index :spree_product_property_translations, column: :spree_product_property_id, if_exists: true
+ end
else
create_table :spree_product_property_translations do |t|
# Translated attribute(s)
diff --git a/core/db/migrate/20230109105943_create_property_translations.rb b/core/db/migrate/20230109105943_create_property_translations.rb
index 5bff726be4..8dde187338 100644
--- a/core/db/migrate/20230109105943_create_property_translations.rb
+++ b/core/db/migrate/20230109105943_create_property_translations.rb
@@ -1,7 +1,10 @@
class CreatePropertyTranslations < ActiveRecord::Migration[6.1]
def change
- if ActiveRecord::Base.connection.table_exists? 'spree_property_translations'
- remove_index :spree_property_translations, column: :spree_property_id, if_exists: true
+ if ActiveRecord::Base.connection.table_exists?('spree_property_translations')
+ # manually check for index since Rails if_exists does not always work correctly
+ if ActiveRecord::Migration.connection.index_exists?(:spree_property_translations, :spree_property_id)
+ remove_index :spree_property_translations, column: :spree_property_id, if_exists: true
+ end
else
create_table :spree_property_translations do |t|
# Translated attribute(s)
diff --git a/core/db/migrate/20230117115531_create_taxonomy_translations.rb b/core/db/migrate/20230117115531_create_taxonomy_translations.rb
new file mode 100644
index 0000000000..42305b860b
--- /dev/null
+++ b/core/db/migrate/20230117115531_create_taxonomy_translations.rb
@@ -0,0 +1,24 @@
+class CreateTaxonomyTranslations < ActiveRecord::Migration[6.1]
+ def change
+ if ActiveRecord::Base.connection.table_exists?('spree_taxonomy_translations')
+ # manually check for index since Rails if_exists does not always work correctly
+ if ActiveRecord::Migration.connection.index_exists?(:spree_taxonomy_translations, :spree_taxonomy_id)
+ remove_index :spree_taxonomy_translations, column: :spree_taxonomy_id, if_exists: true
+ end
+ else
+ create_table :spree_taxonomy_translations do |t|
+ # Translated attribute(s)
+ t.string :name
+
+ t.string :locale, null: false
+ t.references :spree_taxonomy, null: false, foreign_key: true, index: false
+
+ t.timestamps null: false
+ end
+
+ add_index :spree_taxonomy_translations, :locale, name: :index_spree_taxonomy_translations_on_locale
+ end
+
+ add_index :spree_taxonomy_translations, [:spree_taxonomy_id, :locale], name: :index_spree_taxonomy_translations_on_spree_taxonomy_id_locale, unique: true
+ end
+end
diff --git a/core/db/migrate/20230117120430_allow_null_taxonomy_name.rb b/core/db/migrate/20230117120430_allow_null_taxonomy_name.rb
new file mode 100644
index 0000000000..743d40de42
--- /dev/null
+++ b/core/db/migrate/20230117120430_allow_null_taxonomy_name.rb
@@ -0,0 +1,5 @@
+class AllowNullTaxonomyName < ActiveRecord::Migration[6.1]
+ def change
+ change_column_null :spree_taxonomies, :name, true
+ end
+end
diff --git a/core/db/migrate/20230117121303_transfer_taxonomy_data_to_translatable_tables.rb b/core/db/migrate/20230117121303_transfer_taxonomy_data_to_translatable_tables.rb
new file mode 100644
index 0000000000..a3ef237123
--- /dev/null
+++ b/core/db/migrate/20230117121303_transfer_taxonomy_data_to_translatable_tables.rb
@@ -0,0 +1,11 @@
+class TransferTaxonomyDataToTranslatableTables < ActiveRecord::Migration[6.1]
+ TRANSLATION_MIGRATION = Spree::TranslationMigrations.new(Spree::Taxonomy, 'en')
+
+ def up
+ TRANSLATION_MIGRATION.transfer_translation_data
+ end
+
+ def down
+ TRANSLATION_MIGRATION.revert_translation_data_transfer
+ end
+end
diff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rb
index 31a04ed58b..c95b077704 100644
--- a/core/lib/spree/core.rb
+++ b/core/lib/spree/core.rb
@@ -117,6 +117,7 @@ require 'spree/core/version'
require 'spree/core/number_generator'
require 'spree/migrations'
+require 'spree/translation_migrations'
require 'spree/core/engine'
require 'spree/i18n'
diff --git a/core/lib/spree/translation_migrations.rb b/core/lib/spree/translation_migrations.rb
new file mode 100644
index 0000000000..19380712b2
--- /dev/null
+++ b/core/lib/spree/translation_migrations.rb
@@ -0,0 +1,40 @@
+module Spree
+ class TranslationMigrations
+ def initialize(resource_class, default_locale)
+ @resource_class = resource_class
+ @translations_table = resource_class::Translation.table_name
+ @translatable_fields = resource_class.translatable_fields.join(', ')
+ @foreign_key = "#{resource_class.table_name.singularize}_id"
+ @default_locale = default_locale
+ end
+
+ def transfer_translation_data
+ nullify_translatable_fields = @resource_class.translatable_fields.map { |f| "#{f}=null" }.join(', ')
+
+ unless @resource_class::Translation.exists?
+ ActiveRecord::Base.connection.execute("
+ INSERT INTO #{@translations_table} (#{@translatable_fields}, #{@foreign_key}, locale, created_at, updated_at)
+ SELECT #{@translatable_fields}, id, '#{@default_locale}' as locale, created_at, updated_at FROM #{@resource_class.table_name};
+ ")
+ ActiveRecord::Base.connection.execute("
+ UPDATE #{@resource_class.table_name}
+ SET #{nullify_translatable_fields};
+ ")
+ end
+ end
+
+ def revert_translation_data_transfer
+ translation_table_fields = @resource_class.translatable_fields.map { |f| "#{@translations_table}.#{f}" }.join(', ')
+ row_expression = @resource_class.translatable_fields.count == 1 ? 'ROW' : ''
+
+ ActiveRecord::Base.connection.execute("
+ UPDATE #{@resource_class.table_name}
+ SET (#{@translatable_fields}) = #{row_expression}(#{translation_table_fields})
+ FROM #{@translations_table}
+ WHERE #{@translations_table}.#{@foreign_key} = #{@resource_class.table_name}.id
+ ")
+
+ ActiveRecord::Base.connection.execute("TRUNCATE TABLE #{@translations_table}")
+ end
+ end
+end
diff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rb
index dc7078fd17..9081b32d26 100644
--- a/core/spec/models/spree/product_spec.rb
+++ b/core/spec/models/spree/product_spec.rb
@@ -352,6 +352,12 @@ describe Spree::Product, type: :model do
expect(@product.slugs.with_deleted).not_to be_empty
end
+ it 'keeps translations when product is destroyed' do
+ @product.destroy
+
+ expect(@product.name).not_to be_empty
+ end
+
it 'updates the history when the product is restored' do
@product.destroy
diff --git a/sample/db/samples/taxonomies.rb b/sample/db/samples/taxonomies.rb
index 56e245c0c7..06d29cceb4 100644
--- a/sample/db/samples/taxonomies.rb
+++ b/sample/db/samples/taxonomies.rb
@@ -8,5 +8,9 @@ taxonomies = [
]
taxonomies.each do |taxonomy_attrs|
- Spree::Taxonomy.where(taxonomy_attrs).first_or_create!
+ if Spree::Taxonomy.where(taxonomy_attrs).any?
+ Spree::Taxonomy.where(taxonomy_attrs).first
+ else
+ Spree::Taxonomy.create!(taxonomy_attrs)
+ end
end