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.

Add translations for Option Type, Option Value, Property, & Product Property (#11831)

nciemniakec416ae1e622
diff --git a/api/app/controllers/spree/api/v2/platform/variants_controller.rb b/api/app/controllers/spree/api/v2/platform/variants_controller.rb
index 93c436b467..303752deed 100644
--- a/api/app/controllers/spree/api/v2/platform/variants_controller.rb
+++ b/api/app/controllers/spree/api/v2/platform/variants_controller.rb
@@ -16,7 +16,7 @@ module Spree
def collection
# if filtering on products, manually join on product translation to workaround mobility-ransack issue
if params.key?(:filter) && params[:filter].keys.any? { |k| k.include? 'product' }
- @collection ||= scope.join_product_translations.
+ @collection ||= scope.joins(:product).join_translation_table(Product).
ransack(params[:filter]).result
else
super
diff --git a/api/spec/integration/api/v2/platform/option_types_spec.rb b/api/spec/integration/api/v2/platform/option_types_spec.rb
index b76c9fe7f3..f98173bad2 100644
--- a/api/spec/integration/api/v2/platform/option_types_spec.rb
+++ b/api/spec/integration/api/v2/platform/option_types_spec.rb
@@ -12,7 +12,13 @@ describe 'Option Types API', swagger: true do
let(:id) { create(:option_type).id }
let(:option_type) { create(:option_type) }
let(:records_list) { create_list(:option_type, 2) }
- let(:valid_create_param_value) { build(:option_type).attributes }
+ let(:valid_create_param_value) do
+ {
+ name: 'option type name',
+ presentation: 'Option Type',
+ position: 10
+ }
+ end
let(:valid_update_param_value) do
{
name: 'Size-X'
diff --git a/api/spec/integration/api/v2/platform/option_values_spec.rb b/api/spec/integration/api/v2/platform/option_values_spec.rb
index 72dec896bf..55be093fc0 100644
--- a/api/spec/integration/api/v2/platform/option_values_spec.rb
+++ b/api/spec/integration/api/v2/platform/option_values_spec.rb
@@ -13,7 +13,14 @@ describe 'Option Values API', swagger: true do
let(:id) { create(:option_value).id }
let(:option_type) { create(:option_value) }
let(:records_list) { create_list(:option_value, 2) }
- let(:valid_create_param_value) { build(:option_value).attributes }
+ let(:valid_create_param_value) do
+ {
+ name: 'option value',
+ presentation: 'Option Value',
+ position: 10,
+ option_type_id: option_type.id
+ }
+ end
let(:valid_update_param_value) do
{
name: 'M'
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 feb1d1c5b2..99b821b5ad 100644
--- a/api/spec/requests/spree/api/v2/storefront/products_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
@@ -8,9 +8,9 @@ describe 'API V2 Storefront Products Spec', type: :request do
let(:product_with_taxon) { create(:product, taxons: [taxon], stores: [store]) }
let(:product_with_name) { create(:product, name: 'Test Product', stores: [store]) }
let(:product_with_price) { create(:product, price: 13.44, stores: [store]) }
- let!(:option_type) { create(:option_type) }
+ let!(:option_type) { create(:option_type, name: 'test option type') }
let!(:option_value) { create(:option_value, option_type: option_type) }
- let(:product_with_option) { create(:product, option_types: [option_type], stores: [store]) }
+ let(:product_with_option) { create(:product, name: 'Product with Option', option_types: [option_type], stores: [store]) }
let!(:variant) { create(:variant, product: product_with_option, option_values: [option_value]) }
let(:product) { create(:product, stores: [store]) }
let!(:deleted_product) { create(:product, deleted_at: Time.current - 1.day, stores: [store]) }
@@ -24,6 +24,7 @@ describe 'API V2 Storefront Products Spec', type: :request do
let!(:product_property) { create(:product_property, property: new_property, product: product_with_property, value: 'Some Value') }
let!(:product_property2) { create(:product_property, property: property, product: product_with_property, value: 'Some Value 2') }
+
before { Spree::Api::Config[:api_v2_per_page_limit] = 4 }
describe 'products#index' do
@@ -151,14 +152,37 @@ describe 'API V2 Storefront Products Spec', type: :request do
end
context 'with specified options' do
- before { get "/api/v2/storefront/products?filter[options][#{option_type.name}]=#{option_value.name}&include=option_types,variants.option_values" }
+ context 'with no locale set' do
+ before {
+ get "/api/v2/storefront/products?filter[options][#{option_type.name}]=#{option_value.name}&include=option_types,variants.option_values"
+ }
- it_behaves_like 'returns 200 HTTP status'
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns products with specified options' do
+ expect(json_response['data'].first).to have_id(product_with_option.id.to_s)
+ expect(json_response['included']).to include(have_type('option_type').and(have_attribute(:name).with_value(option_type.name)))
+ expect(json_response['included']).to include(have_type('option_value').and(have_attribute(:name).with_value(option_value.name)))
+ end
+ end
- it 'returns products with specified options' do
- expect(json_response['data'].first).to have_id(product_with_option.id.to_s)
- expect(json_response['included']).to include(have_type('option_type').and(have_attribute(:name).with_value(option_type.name)))
- expect(json_response['included']).to include(have_type('option_value').and(have_attribute(:name).with_value(option_value.name)))
+ context 'with locale set to polish' do
+ # create translated resources
+ let!(:option_type_pl_locale) { Mobility.with_locale(:pl) { create(:option_type) } }
+ let!(:option_value_pl_locale) { Mobility.with_locale(:pl) { create(:option_value, option_type: option_type_pl_locale) } }
+ let!(:product_pl_locale) { Mobility.with_locale(:pl) { create(:product, name: 'Produkt Superowy', option_types: [option_type_pl_locale], stores: [store]) } }
+ let!(:variant_pl_locale) { Mobility.with_locale(:pl) { create(:variant, product: product_pl_locale, option_values: [option_value_pl_locale]) } }
+
+ before do
+ store.update_column(:supported_locales, 'en,pl')
+ get "/api/v2/storefront/products?filter[options][#{option_type_pl_locale.name(locale: :pl)}]=#{option_value_pl_locale.name(locale: :pl)}&include=option_types,variants.option_values&locale=pl"
+ end
+
+ it 'returns products with specified options in polish' do
+ expect(json_response['data'].first).to have_id(product_pl_locale.id.to_s)
+ expect(json_response['included']).to include(have_type('option_type').and(have_attribute(:name).with_value(option_type_pl_locale.name(locale: :pl))))
+ expect(json_response['included']).to include(have_type('option_value').and(have_attribute(:name).with_value(option_value_pl_locale.name(locale: :pl))))
+ end
end
end
diff --git a/core/app/finders/spree/products/find.rb b/core/app/finders/spree/products/find.rb
index fc06af2bd7..116d44e430 100644
--- a/core/app/finders/spree/products/find.rb
+++ b/core/app/finders/spree/products/find.rb
@@ -148,10 +148,8 @@ module Spree
product_name = name
- # i18n scope doesn't automatically get set here (mobility gem bug?) set it explicitly
- products.i18n do
- name.matches("%#{product_name}%")
- end
+ # i18n mobility scope doesn't automatically get set for query blocks (Mobility issue #599) - set it explicitly
+ products.i18n { name.matches("%#{product_name}%") }
end
def by_options(products)
diff --git a/core/app/models/concerns/spree/product_scopes.rb b/core/app/models/concerns/spree/product_scopes.rb
index 340ad930c6..effe62f8ca 100644
--- a/core/app/models/concerns/spree/product_scopes.rb
+++ b/core/app/models/concerns/spree/product_scopes.rb
@@ -32,15 +32,16 @@ module Spree
end
def self.property_conditions(property)
- properties = Property.table_name
+ properties_table = Property.table_name
+ property_translations_table = Property.translation_table_alias
case property
- when Property then { "#{properties}.id" => property.id }
- when Integer then { "#{properties}.id" => property }
+ when Property then { "#{properties_table}.id" => property.id }
+ when Integer then { "#{properties_table}.id" => property }
else
if Property.column_for_attribute('id').type == :uuid
- ["#{properties.name} = ? OR #{properties.id} = ?", property, property]
+ ["#{property_translations_table.name} = ? OR #{properties_table.id} = ?", property, property]
else
- { "#{properties}.name" => property }
+ { "#{property_translations_table}.name" => property }
end
end
end
@@ -126,21 +127,25 @@ module Spree
# a scope that finds all products having property specified by name, object or id
add_search_scope :with_property do |property|
- joins(:properties).where(property_conditions(property))
+ joins(:properties).join_translation_table(Property).where(property_conditions(property))
end
# a simple test for product with a certain property-value pairing
# note that it can test for properties with NULL values, but not for absent values
add_search_scope :with_property_value do |property, value|
joins(:properties).
- where("#{ProductProperty.table_name}.value = ?", value).
+ join_translation_table(Property).
+ join_translation_table(ProductProperty).
+ where("#{ProductProperty.translation_table_alias}.value = ?", value).
where(property_conditions(property))
end
add_search_scope :with_property_values do |property_filter_param, property_values|
joins(product_properties: :property).
- where(Property.table_name => { filter_param: property_filter_param }).
- where(ProductProperty.table_name => { filter_param: property_values.map(&:parameterize) })
+ join_translation_table(Property).
+ join_translation_table(ProductProperty).
+ where(Property.translation_table_alias => { filter_param: property_filter_param }).
+ where(ProductProperty.translation_table_alias => { filter_param: property_values.map(&:parameterize) })
end
add_search_scope :with_option do |option|
@@ -151,7 +156,9 @@ module Spree
elsif OptionType.column_for_attribute('id').type == :uuid
joins(:option_types).where(spree_option_types: { name: option }).or(Product.joins(:option_types).where(spree_option_types: { id: option }))
else
- joins(:option_types).where(spree_option_types: { name: option })
+ joins(:option_types).
+ join_translation_table(OptionType).
+ where(OptionType.translation_table_alias => { name: option })
end
end
@@ -164,6 +171,7 @@ module Spree
OptionType.where(id: option).or(OptionType.where(name: option))&.first&.id
else
OptionType.where(name: option)&.first&.id
+ OptionType.where(name: option)&.first&.id
end
end
@@ -171,7 +179,9 @@ module Spree
group("#{Spree::Product.table_name}.id").
joins(variants_including_master: :option_values).
- where(Spree::OptionValue.table_name => { name: value, option_type_id: option_type_id })
+ join_translation_table(Spree::OptionValue).
+ where(Spree::OptionValue.translation_table_alias => { name: value },
+ Spree::OptionValue.table_name => { option_type_id: option_type_id })
end
# Finds all products which have either:
diff --git a/core/app/models/concerns/spree/translatable_resource_scopes.rb b/core/app/models/concerns/spree/translatable_resource_scopes.rb
new file mode 100644
index 0000000000..d7f2d75ed1
--- /dev/null
+++ b/core/app/models/concerns/spree/translatable_resource_scopes.rb
@@ -0,0 +1,16 @@
+module Spree
+ module TranslatableResourceScopes
+ extend ActiveSupport::Concern
+
+ class_methods do
+ # To be used when joining on the resource itself does not automatically join on its translations table
+ # This method is to be used when you've already joined on the translatable table itself
+ def join_translation_table(translatable_class)
+ translatable_class_foreign_key = "#{translatable_class.table_name.singularize}_id"
+ joins("LEFT OUTER JOIN #{translatable_class::Translation.table_name} #{translatable_class.translation_table_alias}
+ ON #{translatable_class.translation_table_alias}.#{translatable_class_foreign_key} = #{translatable_class.table_name}.id
+ AND #{translatable_class.translation_table_alias}.locale = '#{Mobility.locale}'")
+ end
+ end
+ end
+end
diff --git a/core/app/models/spree/option_type.rb b/core/app/models/spree/option_type.rb
index 84366de9ef..244aaac8c9 100644
--- a/core/app/models/spree/option_type.rb
+++ b/core/app/models/spree/option_type.rb
@@ -2,10 +2,14 @@ module Spree
class OptionType < Spree::Base
include UniqueName
include Metadata
+ include TranslatableResource
if defined?(Spree::Webhooks)
include Spree::Webhooks::HasWebhooks
end
+ TRANSLATABLE_FIELDS = %i[name presentation].freeze
+ translates(*TRANSLATABLE_FIELDS)
+
acts_as_list
auto_strip_attributes :name, :presentation
diff --git a/core/app/models/spree/option_value.rb b/core/app/models/spree/option_value.rb
index ccd0623bdf..e594d72f2f 100644
--- a/core/app/models/spree/option_value.rb
+++ b/core/app/models/spree/option_value.rb
@@ -1,10 +1,14 @@
module Spree
class OptionValue < Spree::Base
include Metadata
+ include TranslatableResource
if defined?(Spree::Webhooks)
include Spree::Webhooks::HasWebhooks
end
+ TRANSLATABLE_FIELDS = %i[name presentation].freeze
+ translates(*TRANSLATABLE_FIELDS)
+
belongs_to :option_type, class_name: 'Spree::OptionType', touch: true, inverse_of: :option_values
acts_as_list scope: :option_type
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index 8013939172..99367998d0 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 TranslatableResourceScopes
include MemoizedData
include Metadata
if defined?(Spree::Webhooks)
@@ -39,7 +40,8 @@ module Spree
TRANSLATABLE_FIELDS = %i[name description slug meta_description meta_keywords meta_title].freeze
translates(*TRANSLATABLE_FIELDS)
- Product::Translation.class_eval { acts_as_paranoid }
+
+ self::Translation.class_eval { acts_as_paranoid }
friendly_id :slug_candidates, use: [:history, :mobility]
acts_as_paranoid
@@ -310,14 +312,26 @@ module Spree
end
def property(property_name)
- product_properties.joins(:property).find_by(spree_properties: { name: property_name }).try(:value)
+ product_properties.joins(:property).
+ join_translation_table(Property).
+ find_by(Property.translation_table_alias => { name: property_name }).try(:value)
end
def set_property(property_name, property_value, property_presentation = property_name)
ApplicationRecord.transaction do
- # Works around spree_i18n #301
- property = Property.create_with(presentation: property_presentation).find_or_create_by(name: property_name)
- product_property = ProductProperty.where(product: self, property: property).first_or_initialize
+ # Manual first_or_create to work around Mobility bug
+ property = if Property.where(name: property_name).exists?
+ Property.where(name: property_name).first
+ else
+ Property.create(name: property_name, presentation: property_presentation)
+ end
+
+ product_property = if ProductProperty.where(product: self, property: property).exists?
+ ProductProperty.where(product: self, property: property).first
+ else
+ ProductProperty.create(product: self, property: property)
+ end
+
product_property.value = property_value
product_property.save!
end
diff --git a/core/app/models/spree/product_property.rb b/core/app/models/spree/product_property.rb
index 785cb7bba0..58dec2bf85 100644
--- a/core/app/models/spree/product_property.rb
+++ b/core/app/models/spree/product_property.rb
@@ -1,8 +1,15 @@
module Spree
class ProductProperty < Spree::Base
include Spree::FilterParam
+ include TranslatableResource
+ include TranslatableResourceScopes
- auto_strip_attributes :value
+ TRANSLATABLE_FIELDS = %i[value filter_param].freeze
+ translates(*TRANSLATABLE_FIELDS)
+
+ self::Translation.class_eval do
+ auto_strip_attributes :value
+ end
acts_as_list scope: :product
@@ -31,8 +38,11 @@ module Spree
`ProductProperty#property_name=` is deprecated and will be removed in Spree 5.0.
DEPRECATION
if name.present?
- # don't use `find_by :name` to workaround globalize/globalize#423 bug
- self.property = Property.where(name: name).first_or_create(presentation: name)
+ self.property = if Property.where(name: name).exists?
+ Property.where(name: name).first
+ else
+ Property.create(name: name, presentation: name)
+ end
end
end
diff --git a/core/app/models/spree/property.rb b/core/app/models/spree/property.rb
index 026522ea90..73e2f55a49 100644
--- a/core/app/models/spree/property.rb
+++ b/core/app/models/spree/property.rb
@@ -2,11 +2,17 @@ module Spree
class Property < Spree::Base
include Spree::FilterParam
include Metadata
+ include TranslatableResource
if defined?(Spree::Webhooks)
include Spree::Webhooks::HasWebhooks
end
- auto_strip_attributes :name, :presentation
+ TRANSLATABLE_FIELDS = %i[name presentation filter_param].freeze
+ translates(*TRANSLATABLE_FIELDS)
+
+ self::Translation.class_eval do
+ auto_strip_attributes :name, :presentation
+ end
has_many :property_prototypes, class_name: 'Spree::PropertyPrototype'
has_many :prototypes, through: :property_prototypes, class_name: 'Spree::Prototype'
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index 2be9a29c3b..0e2f44d846 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -5,6 +5,7 @@ module Spree
include MemoizedData
include Metadata
+ include TranslatableResourceScopes
if defined?(Spree::Webhooks)
include Spree::Webhooks::HasWebhooks
end
@@ -122,17 +123,11 @@ module Spree
self.whitelisted_ransackable_scopes = %i(product_name_or_sku_cont search_by_product_name_or_sku)
def self.product_name_or_sku_cont(query)
- join_product_translations.
+ joins(:product).join_translation_table(Product).
where("LOWER(#{Product.translation_table_alias}.name) LIKE LOWER(:query)
OR LOWER(sku) LIKE LOWER(:query)", query: "%#{query}%")
end
- def self.join_product_translations
- joins(:product).joins("LEFT OUTER JOIN #{Product::Translation.table_name} #{Product.translation_table_alias}
- ON #{Product.translation_table_alias}.spree_product_id = #{Product.table_name}.id
- AND #{Product.translation_table_alias}.locale = '#{Mobility.locale}'")
- end
-
def self.search_by_product_name_or_sku(query)
product_name_or_sku_cont(query)
end
diff --git a/core/app/sorters/spree/products/sort.rb b/core/app/sorters/spree/products/sort.rb
index 0eca2d54dd..511dabd869 100644
--- a/core/app/sorters/spree/products/sort.rb
+++ b/core/app/sorters/spree/products/sort.rb
@@ -49,7 +49,7 @@ module Spree
sort.detect { |s| s[0] == field }
end
- # Add translatable fields to SELECT statement to avoid InvalidColumnReference error (Mobility bug workaround)
+ # Add translatable fields to SELECT statement to avoid InvalidColumnReference error (workaround for Mobility issue #596)
def select_translatable_fields(scope)
translatable_fields = translatable_sortable_fields
return scope if translatable_fields.empty?
diff --git a/core/brakeman.ignore b/core/brakeman.ignore
index 24e68c5178..a072ae9c04 100644
--- a/core/brakeman.ignore
+++ b/core/brakeman.ignore
@@ -7,7 +7,7 @@
"check_name": "SQL",
"message": "Possible SQL injection",
"file": "app/models/concerns/spree/product_scopes.rb",
- "line": 63,
+ "line": 64,
"link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
"code": "where(\"#{price_table_name}.amount <= ?\", price)",
"render_path": null,
@@ -26,20 +26,43 @@
{
"warning_type": "SQL Injection",
"warning_code": 0,
- "fingerprint": "67e80bcfa8898315b0f8642bc61990c8319bc7a468453efc048757a439e53e0b",
+ "fingerprint": "1f02952550c2f54d044c9577a45e7ba7c7990c8b8a59d1dac83a96790237f507",
"check_name": "SQL",
"message": "Possible SQL injection",
- "file": "app/models/spree/variant.rb",
- "line": 131,
+ "file": "app/models/concerns/spree/product_scopes.rb",
+ "line": 139,
"link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
- "code": "joins(:product).joins(\"LEFT OUTER JOIN #{Product::Translation.table_name} #{Product.translation_table_alias}\\n ON #{Product.translation_table_alias}.spree_product_id = #{Product.table_name}.id\\n AND #{Product.translation_table_alias}.locale = '#{Mobility.locale}'\")",
+ "code": "joins(:properties).join_translation_table(Property).join_translation_table(ProductProperty).where(\"#{ProductProperty.translation_table_alias}.value = ?\", value)",
"render_path": null,
"location": {
"type": "method",
- "class": "Spree::Variant",
- "method": "Spree::Variant.join_product_translations"
+ "class": "Spree::ProductScopes",
+ "method": null
},
- "user_input": "Product.translation_table_alias",
+ "user_input": "ProductProperty.translation_table_alias",
+ "confidence": "Weak",
+ "cwe_id": [
+ 89
+ ],
+ "note": ""
+ },
+ {
+ "warning_type": "SQL Injection",
+ "warning_code": 0,
+ "fingerprint": "7928c0813a0bf084ead091b4554ef6abea9ae9c7167936f5c62da9e328b9f736",
+ "check_name": "SQL",
+ "message": "Possible SQL injection",
+ "file": "app/models/concerns/spree/product_scopes.rb",
+ "line": 139,
+ "link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
+ "code": "joins(:properties).join_translation_table(Property).join_translation_table(ProductProperty).where(\"#{ProductProperty.translation_table_alias}.value = ?\", value)",
+ "render_path": null,
+ "location": {
+ "type": "method",
+ "class": "Spree",
+ "method": null
+ },
+ "user_input": "ProductProperty.translation_table_alias",
"confidence": "Weak",
"cwe_id": [
89
@@ -53,7 +76,7 @@
"check_name": "SQL",
"message": "Possible SQL injection",
"file": "app/models/concerns/spree/product_scopes.rb",
- "line": 67,
+ "line": 68,
"link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
"code": "where(\"#{price_table_name}.amount >= ?\", price)",
"render_path": null,
@@ -76,7 +99,7 @@
"check_name": "SQL",
"message": "Possible SQL injection",
"file": "app/models/concerns/spree/product_scopes.rb",
- "line": 63,
+ "line": 64,
"link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
"code": "where(\"#{price_table_name}.amount <= ?\", price)",
"render_path": null,
@@ -95,13 +118,13 @@
{
"warning_type": "SQL Injection",
"warning_code": 0,
- "fingerprint": "e6bb08c012af19589f796e26b4dc2230ea1c1949c7cd6464ca025939513d8b3d",
+ "fingerprint": "c2bc48d98076b7c4fc3314c6a85f7bd1132efe5fcc346da4d28df7c25f93633f",
"check_name": "SQL",
"message": "Possible SQL injection",
"file": "app/models/spree/variant.rb",
- "line": 126,
+ "line": 127,
"link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
- "code": "join_product_translations.where(\"LOWER(#{Product.translation_table_alias}.name) LIKE LOWER(:query)\\n OR LOWER(sku) LIKE LOWER(:query)\", :query => (\"%#{query}%\"))",
+ "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,
"location": {
"type": "method",
@@ -115,6 +138,29 @@
],
"note": ""
},
+ {
+ "warning_type": "SQL Injection",
+ "warning_code": 0,
+ "fingerprint": "e7aa61c87f26af5f63f00e9791954d787a559dab1eadf8926861796237bd431d",
+ "check_name": "SQL",
+ "message": "Possible SQL injection",
+ "file": "app/models/concerns/spree/translatable_resource_scopes.rb",
+ "line": 10,
+ "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}'\")",
+ "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,
@@ -122,7 +168,7 @@
"check_name": "SQL",
"message": "Possible SQL injection",
"file": "app/models/concerns/spree/product_scopes.rb",
- "line": 67,
+ "line": 68,
"link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
"code": "where(\"#{price_table_name}.amount >= ?\", price)",
"render_path": null,
@@ -139,6 +185,6 @@
"note": "interpolating table name"
}
],
- "updated": "2022-12-29 09:32:26 +0100",
+ "updated": "2023-01-13 11:44:19 +0100",
"brakeman_version": "5.4.0"
}
diff --git a/core/db/migrate/20220804073928_transfer_data_to_translatable_tables.rb b/core/db/migrate/20220804073928_transfer_data_to_translatable_tables.rb
new file mode 100644
index 0000000000..e5aac575e6
--- /dev/null
+++ b/core/db/migrate/20220804073928_transfer_data_to_translatable_tables.rb
@@ -0,0 +1,66 @@
+class TransferDataToTranslatableTables < ActiveRecord::Migration[6.1]
+ DEFAULT_LOCALE = 'en'
+ PRODUCTS_TABLE = 'spree_products'
+ PRODUCT_TRANSLATIONS_TABLE = 'spree_product_translations'
+ TAXONS_TABLE = 'spree_taxons'
+ TAXON_TRANSLATIONS_TABLE = 'spree_taxon_translations'
+
+ def up
+ # Products
+ ActiveRecord::Base.connection.execute("
+ INSERT INTO #{PRODUCT_TRANSLATIONS_TABLE} (name, description, locale, spree_product_id, created_at, updated_at, meta_description, meta_keywords, meta_title, slug)
+ SELECT name, description, '#{DEFAULT_LOCALE}' as locale, id, created_at, updated_at, meta_description, meta_keywords, meta_title, slug FROM #{PRODUCTS_TABLE}
+ ")
+ ActiveRecord::Base.connection.execute("
+ UPDATE #{PRODUCTS_TABLE}
+ SET name=null, description=null, meta_description=null, meta_keywords=null, meta_title=null, slug=null;
+ ")
+ #Taxons
+ ActiveRecord::Base.connection.execute("
+ INSERT INTO #{TAXON_TRANSLATIONS_TABLE} (name, description, locale, spree_taxon_id, created_at, updated_at)
+ SELECT name, description, '#{DEFAULT_LOCALE}' as locale, id, created_at, updated_at FROM #{TAXONS_TABLE}
+ ")
+ ActiveRecord::Base.connection.execute("
+ UPDATE #{TAXONS_TABLE}
+ SET name=null, description=null
+ ")
+ end
+
+ def down
+ ActiveRecord::Base.connection.execute("
+ UPDATE #{PRODUCTS_TABLE} as products
+ SET (name,
+ description,
+ meta_description,
+ meta_keywords,
+ meta_title,
+ slug) =
+ (t_products.name,
+ t_products.description,
+ t_products.meta_description,
+ t_products.meta_keywords,
+ t_products.meta_title,
+ t_products.slug)
+ FROM #{PRODUCT_TRANSLATIONS_TABLE} AS t_products
+ WHERE t_products.spree_product_id = products.id
+ ")
+
+ ActiveRecord::Base.connection.execute("
+ TRUNCATE TABLE #{PRODUCT_TRANSLATIONS_TABLE}
+ ")
+
+ ActiveRecord::Base.connection.execute("
+ UPDATE #{TAXONS_TABLE} as taxons
+ SET (name,
+ description) =
+ (t_taxons.name,
+ t_taxons.description)
+ FROM #{TAXON_TRANSLATIONS_TABLE} AS t_taxons
+ WHERE t_taxons.spree_taxon_id = taxons.id
+ ")
+
+ ActiveRecord::Base.connection.execute("
+ TRUNCATE TABLE #{TAXON_TRANSLATIONS_TABLE}
+ ")
+ end
+end
diff --git a/core/db/migrate/20230103144439_create_option_type_translations.rb b/core/db/migrate/20230103144439_create_option_type_translations.rb
new file mode 100644
index 0000000000..359fb2bc67
--- /dev/null
+++ b/core/db/migrate/20230103144439_create_option_type_translations.rb
@@ -0,0 +1,23 @@
+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
+ else
+ create_table :spree_option_type_translations do |t|
+
+ # Translated attribute(s)
+ t.string :name
+ t.string :presentation
+
+ t.string :locale, null: false
+ t.references :spree_option_type, null: false, foreign_key: true, index: false
+
+ t.timestamps
+ end
+
+ add_index :spree_option_type_translations, :locale, name: :index_spree_option_type_translations_on_locale
+ end
+
+ add_index :spree_option_type_translations, [:spree_option_type_id, :locale], name: :unique_option_type_id_per_locale, unique: true
+ end
+end
diff --git a/core/db/migrate/20230103151034_create_option_value_translations.rb b/core/db/migrate/20230103151034_create_option_value_translations.rb
new file mode 100644
index 0000000000..2acb37a311
--- /dev/null
+++ b/core/db/migrate/20230103151034_create_option_value_translations.rb
@@ -0,0 +1,23 @@
+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
+ else
+ create_table :spree_option_value_translations do |t|
+
+ # Translated attribute(s)
+ t.string :name
+ t.string :presentation
+
+ t.string :locale, null: false
+ t.references :spree_option_value, null: false, foreign_key: true, index: false
+
+ t.timestamps
+ end
+
+ add_index :spree_option_value_translations, :locale, name: :index_spree_option_value_translations_on_locale
+ end
+
+ add_index :spree_option_value_translations, [:spree_option_value_id, :locale], name: :unique_option_value_id_per_locale, unique: true
+ end
+end
diff --git a/core/db/migrate/20230109084253_create_product_property_translations.rb b/core/db/migrate/20230109084253_create_product_property_translations.rb
new file mode 100644
index 0000000000..e100182a89
--- /dev/null
+++ b/core/db/migrate/20230109084253_create_product_property_translations.rb
@@ -0,0 +1,22 @@
+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
+ else
+ create_table :spree_product_property_translations do |t|
+ # Translated attribute(s)
+ t.string :value
+ t.string :filter_param
+
+ t.string :locale, null: false
+ t.references :spree_product_property, null: false, foreign_key: true, index: false
+
+ t.timestamps
+ end
+
+ add_index :spree_product_property_translations, :locale, name: :index_spree_product_property_translations_on_locale
+ end
+
+ add_index :spree_product_property_translations, [:spree_product_property_id, :locale], name: :unique_product_property_id_per_locale, unique: true
+ end
+end
diff --git a/core/db/migrate/20230109094907_transfer_options_data_to_translatable_tables.rb b/core/db/migrate/20230109094907_transfer_options_data_to_translatable_tables.rb
new file mode 100644
index 0000000000..ff7eb328fe
--- /dev/null
+++ b/core/db/migrate/20230109094907_transfer_options_data_to_translatable_tables.rb
@@ -0,0 +1,58 @@
+class TransferOptionsDataToTranslatableTables < ActiveRecord::Migration[6.1]
+ DEFAULT_LOCALE = 'en'
+
+ def up
+ # Only transfer data if translation tables are being newly created / no translations exist
+ # Otherwise, assume translation data is already in place from spree_globalize
+
+ # Option Types
+ if not Spree::OptionType::Translation.exists?
+ ActiveRecord::Base.connection.execute("
+ INSERT INTO spree_option_type_translations (name, presentation, locale, spree_option_type_id, created_at, updated_at)
+ SELECT name, presentation, '#{DEFAULT_LOCALE}', id, created_at, updated_at
+ FROM spree_option_types;
+ ")
+ ActiveRecord::Base.connection.execute("
+ UPDATE spree_option_types
+ SET name=null, presentation=null;
+ ")
+ end
+
+ # Option Values
+ if not Spree::OptionValue::Translation.exists?
+ ActiveRecord::Base.connection.execute("
+ INSERT INTO spree_option_value_translations (name, presentation, locale, spree_option_value_id, created_at, updated_at)
+ SELECT name, presentation, '#{DEFAULT_LOCALE}', id, created_at, updated_at
+ FROM spree_option_values;
+ ")
+ ActiveRecord::Base.connection.execute("
+ UPDATE spree_option_values
+ SET name=null, presentation=null;
+ ")
+ end
+ end
+
+ def down
+ # Option Types
+ ActiveRecord::Base.connection.execute("
+ UPDATE spree_option_types as option_types
+ SET (name, presentation) = (t_option_types.name, t_option_types.presentation)
+ FROM spree_option_type_translations AS t_option_types
+ WHERE t_option_types.spree_option_type_id = option_types.id;
+ ")
+ ActiveRecord::Base.connection.execute("
+ TRUNCATE TABLE spree_option_type_translations;
+ ")
+
+ # Option Values
+ ActiveRecord::Base.connection.execute("
+ UPDATE spree_option_values as option_values
+ SET (name, presentation) = (t_option_values.name, t_option_values.presentation)
+ FROM spree_option_value_translations AS t_option_values
+ WHERE t_option_values.spree_option_value_id = option_values.id;
+ ")
+ ActiveRecord::Base.connection.execute("
+ TRUNCATE TABLE spree_option_value_translations;
+ ")
+ end
+end
diff --git a/core/db/migrate/20230109105943_create_property_translations.rb b/core/db/migrate/20230109105943_create_property_translations.rb
new file mode 100644
index 0000000000..5bff726be4
--- /dev/null
+++ b/core/db/migrate/20230109105943_create_property_translations.rb
@@ -0,0 +1,23 @@
+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
+ else
+ create_table :spree_property_translations do |t|
+ # Translated attribute(s)
+ t.string :name
+ t.string :presentation
+ t.string :filter_param
+
+ t.string :locale, null: false
+ t.references :spree_property, null: false, foreign_key: true, index: false
+
+ t.timestamps
+ end
+
+ add_index :spree_property_translations, :locale, name: :index_spree_property_translations_on_locale
+ end
+
+ add_index :spree_property_translations, [:spree_property_id, :locale], name: :unique_property_id_per_locale, unique: true
+ end
+end
diff --git a/core/db/migrate/20230109110840_transfer_property_data_to_translatable_tables.rb b/core/db/migrate/20230109110840_transfer_property_data_to_translatable_tables.rb
new file mode 100644
index 0000000000..5fb7814dc0
--- /dev/null
+++ b/core/db/migrate/20230109110840_transfer_property_data_to_translatable_tables.rb
@@ -0,0 +1,59 @@
+class TransferPropertyDataToTranslatableTables < ActiveRecord::Migration[6.1]
+ DEFAULT_LOCALE = 'en'
+
+ def up
+ # Properties
+ change_column_null :spree_properties, :presentation, true
+
+ if not Spree::Property::Translation.exists?
+ ActiveRecord::Base.connection.execute("
+ INSERT INTO spree_property_translations (name, presentation, filter_param, locale, spree_property_id, created_at, updated_at)
+ SELECT name, presentation, filter_param, '#{DEFAULT_LOCALE}', id, created_at, updated_at
+ FROM spree_properties;
+ ")
+ ActiveRecord::Base.connection.execute("
+ UPDATE spree_properties
+ SET name=null, presentation=null, filter_param=null;
+ ")
+ end
+
+ # Product Properties
+ if not Spree::ProductProperty::Translation.exists?
+ ActiveRecord::Base.connection.execute("
+ INSERT INTO spree_product_property_translations (value, filter_param, locale, spree_product_property_id, created_at, updated_at)
+ SELECT value, filter_param, '#{DEFAULT_LOCALE}', id, created_at, updated_at
+ FROM spree_product_properties;
+ ")
+ ActiveRecord::Base.connection.execute("
+ UPDATE spree_product_properties
+ SET value=null, filter_param=null;
+ ")
+ end
+ end
+
+ def down
+ # Properties
+ change_column_null :spree_properties, :presentation, false
+
+ ActiveRecord::Base.connection.execute("
+ UPDATE spree_properties AS properties
+ SET (name, presentation, filter_param) = (t_properties.name, t_properties.presentation, t_properties.filter_param)
+ FROM spree_property_translations AS t_properties
+ WHERE t_properties.spree_property_id = properties.id;
+ ")
+ ActiveRecord::Base.connection.execute("
+ TRUNCATE TABLE spree_property_translations;
+ ")
+
+ # Product Properties
+ ActiveRecord::Base.connection.execute("
+ UPDATE spree_product_properties AS product_properties
+ SET (value, filter_param) = (t_product_properties.value, t_product_properties.filter_param)
+ FROM spree_product_property_translations AS t_product_properties
+ WHERE t_product_properties.spree_product_property_id = product_properties.id;
+ ")
+ ActiveRecord::Base.connection.execute("
+ TRUNCATE TABLE spree_product_property_translations;
+ ")
+ end
+end
diff --git a/core/lib/spree/core/product_filters.rb b/core/lib/spree/core/product_filters.rb
index de24f226d8..2c586c4c85 100644
--- a/core/lib/spree/core/product_filters.rb
+++ b/core/lib/spree/core/product_filters.rb
@@ -98,18 +98,21 @@ module Spree
conds.each do |new_scope|
scope = scope.or(new_scope)
end
- Spree::Product.with_property('brand').where(scope)
+ Product.with_property('brand').join_translation_table(ProductProperty).where(scope)
end
def self.brand_filter
brand_property = Spree::Property.find_by(name: 'brand')
brands = brand_property ? Spree::ProductProperty.where(property_id: brand_property.id).pluck(:value).uniq.map(&:to_s) : []
- pp = Spree::ProductProperty.arel_table
- conds = Hash[*brands.map { |b| [b, pp[:value].eq(b)] }.flatten]
+
+ conditions = brands.map do |brand|
+ [brand, { ProductProperty.translation_table_alias => { value: brand } }]
+ end.to_h
+
{
name: I18n.t('spree.taxonomy_brands_name'),
scope: :brand_any,
- conds: conds,
+ conds: conditions,
labels: brands.sort.map { |k| [k, k] }
}
end
diff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rb
index 455354d39d..dc7078fd17 100644
--- a/core/spec/models/spree/product_spec.rb
+++ b/core/spec/models/spree/product_spec.rb
@@ -433,7 +433,7 @@ describe Spree::Product, type: :model do
# Regression test for #2455
it "does not overwrite properties' presentation names" do
- Spree::Property.where(name: 'foo').first_or_create!(presentation: "Foo's Presentation Name")
+ Spree::Property.create!(name: 'foo', presentation: "Foo's Presentation Name")
product.set_property('foo', 'value1')
product.set_property('bar', 'value2')
expect(Spree::Property.where(name: 'foo').first.presentation).to eq("Foo's Presentation Name")
diff --git a/core/spec/models/spree/property_spec.rb b/core/spec/models/spree/property_spec.rb
index 377f21832f..ed837dc3fb 100644
--- a/core/spec/models/spree/property_spec.rb
+++ b/core/spec/models/spree/property_spec.rb
@@ -84,8 +84,10 @@ describe Spree::Property, type: :model do
let(:product_property_2) { create(:product_property, property: property, product: product_2, value: 'Test Test') }
before do
- product_property.update_column(:filter_param, nil)
- product_property.update_column(:value, 'some value')
+ product_property.translations.each do |t|
+ t.update_column(:filter_param, nil)
+ t.update_column(:value, 'some_value')
+ end
end
context 'filterable property' do
diff --git a/sample/db/samples/option_types.rb b/sample/db/samples/option_types.rb
index 919d55e8e0..33f14de05e 100644
--- a/sample/db/samples/option_types.rb
+++ b/sample/db/samples/option_types.rb
@@ -17,5 +17,7 @@ option_types_attributes = [
]
option_types_attributes.each do |attrs|
- Spree::OptionType.where(attrs).first_or_create!
+ unless Spree::OptionType.where(attrs).exists?
+ Spree::OptionType.create!(attrs)
+ end
end
diff --git a/sample/db/samples/properties.rb b/sample/db/samples/properties.rb
index cd4fd5156b..1c503d4bf5 100644
--- a/sample/db/samples/properties.rb
+++ b/sample/db/samples/properties.rb
@@ -14,7 +14,9 @@ properties = {
}
properties.each do |name, presentation|
- Spree::Property.where(name: name, presentation: presentation).first_or_create!
+ unless Spree::Property.where(name: name, presentation: presentation).exists?
+ Spree::Property.create!(name: name, presentation: presentation)
+ end
end
Spree::Property.where(name: %w[brand manufacturer]).update(filterable: true)