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 weak SQL scopes in core

Damian Legawiec57a1e061b6e2
diff --git a/core/app/finders/spree/taxons/find.rb b/core/app/finders/spree/taxons/find.rb
index fd4335aab7..1c1c823190 100644
--- a/core/app/finders/spree/taxons/find.rb
+++ b/core/app/finders/spree/taxons/find.rb
@@ -68,7 +68,7 @@ module Spree
if Spree.use_translations?
taxons.joins(:parent).
join_translation_table(Taxon, 'parents_spree_taxons').
- where(["#{Taxon.translation_table_alias}.permalink = ?", parent_permalink])
+ where(Taxon.translation_table_alias => { permalink: parent_permalink })
else
taxons.joins(:parent).where(parent: { permalink: parent_permalink })
end
diff --git a/core/app/models/concerns/spree/product_scopes.rb b/core/app/models/concerns/spree/product_scopes.rb
index c0ae78942a..b145a35922 100644
--- a/core/app/models/concerns/spree/product_scopes.rb
+++ b/core/app/models/concerns/spree/product_scopes.rb
@@ -49,11 +49,11 @@ module Spree
add_simple_scopes simple_scopes
add_search_scope :ascend_by_master_price do
- order("#{price_table_name}.amount ASC")
+ order(price_table_name => { amount: :asc })
end
add_search_scope :descend_by_master_price do
- order("#{price_table_name}.amount DESC")
+ order(price_table_name => { amount: :desc })
end
add_search_scope :price_between do |low, high|
@@ -61,11 +61,11 @@ module Spree
end
add_search_scope :master_price_lte do |price|
- where("#{price_table_name}.amount <= ?", price)
+ where(Price.table_name => { amount: ..price })
end
add_search_scope :master_price_gte do |price|
- where("#{price_table_name}.amount >= ?", price)
+ where(Price.table_name => { amount: price.. })
end
add_search_scope :in_stock do
@@ -137,11 +137,11 @@ module Spree
joins(:properties).
join_translation_table(Property).
join_translation_table(ProductProperty).
- where("#{ProductProperty.translation_table_alias}.value = ?", value).
+ where(ProductProperty.translation_table_alias => { value: value }).
where(property_conditions(property))
else
joins(:properties).
- where("#{ProductProperty.table_name}.value = ?", value).
+ where(ProductProperty.table_name => { value: value }).
where(property_conditions(property))
end
end
diff --git a/core/app/models/concerns/spree/translatable_resource.rb b/core/app/models/concerns/spree/translatable_resource.rb
index 69b2104976..d1f135909e 100644
--- a/core/app/models/concerns/spree/translatable_resource.rb
+++ b/core/app/models/concerns/spree/translatable_resource.rb
@@ -20,6 +20,10 @@ module Spree
def translation_table_alias
"#{self::Translation.table_name}_#{Mobility.normalize_locale(Mobility.locale)}"
end
+
+ def arel_table_alias
+ Arel::Table.new(translation_table_alias)
+ end
end
end
end
diff --git a/core/app/models/concerns/spree/translatable_resource_scopes.rb b/core/app/models/concerns/spree/translatable_resource_scopes.rb
index 6dc4501660..e5a4e87891 100644
--- a/core/app/models/concerns/spree/translatable_resource_scopes.rb
+++ b/core/app/models/concerns/spree/translatable_resource_scopes.rb
@@ -15,9 +15,23 @@ module Spree
end
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} = #{join_on_table_name}.id
- AND #{translatable_class.translation_table_alias}.locale = '#{Mobility.locale}'")
+ joins(
+ Arel::Nodes::OuterJoin.new(
+ Arel::Table.new(translatable_class::Translation.table_name).alias(translatable_class.translation_table_alias),
+ Arel::Nodes::On.new(
+ Arel::Nodes::And.new([
+ Arel::Nodes::Equality.new(
+ Arel::Table.new(translatable_class.translation_table_alias)[translatable_class_foreign_key],
+ Arel::Table.new(join_on_table_name)[:id]
+ ),
+ Arel::Nodes::Equality.new(
+ Arel::Table.new(translatable_class.translation_table_alias)[:locale],
+ Arel::Nodes::Quoted.new(Mobility.locale.to_s)
+ )
+ ])
+ )
+ )
+ )
end
end
end
diff --git a/core/app/models/spree/taxon.rb b/core/app/models/spree/taxon.rb
index 998f8c014b..2b3a865b7c 100644
--- a/core/app/models/spree/taxon.rb
+++ b/core/app/models/spree/taxon.rb
@@ -1,4 +1,3 @@
-# TODO: let friendly id take care of sanitizing the url
require 'stringex'
module Spree
@@ -96,9 +95,11 @@ module Spree
if Spree.use_translations?
joins(:taxonomy).
join_translation_table(Taxonomy).
- where(["LOWER(#{Taxonomy.translation_table_alias}.name) = ?", taxonomy_name.downcase.strip])
+ where(
+ Taxonomy.arel_table_alias[:name].lower.eq(taxonomy_name.downcase.strip)
+ )
else
- joins(:taxonomy).where(["LOWER(#{Spree::Taxonomy.table_name}.name) = ?", taxonomy_name.downcase.strip])
+ joins(:taxonomy).where(Spree::Taxonomy.arel_table[:name].lower.eq(taxonomy_name.downcase.strip))
end
}
diff --git a/core/lib/spree/translation_migrations.rb b/core/lib/spree/translation_migrations.rb
index 19380712b2..b18c15b110 100644
--- a/core/lib/spree/translation_migrations.rb
+++ b/core/lib/spree/translation_migrations.rb
@@ -12,14 +12,24 @@ module Spree
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};
- ")
+ # Copy data from main table to translations table
+ @resource_class.find_each do |resource|
+ translation_attrs = @resource_class.translatable_fields.each_with_object({}) do |field, attrs|
+ attrs[field] = resource[field]
+ end
+
+ @resource_class::Translation.create!(
+ translation_attrs.merge(
+ @foreign_key => resource.id,
+ locale: @default_locale,
+ created_at: resource.created_at,
+ updated_at: resource.updated_at
+ )
+ )
+ end
+
+ # Nullify translatable fields in main table
+ @resource_class.update_all(nullify_translatable_fields)
end
end
@@ -27,14 +37,16 @@ module Spree
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
- ")
+ # Update main table with translations
+ @resource_class::Translation.find_each do |translation|
+ resource = @resource_class.find(translation[@foreign_key])
+ @resource_class.translatable_fields.each do |field|
+ resource.update_column(field, translation[field])
+ end
+ end
- ActiveRecord::Base.connection.execute("TRUNCATE TABLE #{@translations_table}")
+ # Clear translations table
+ @resource_class::Translation.delete_all
end
end
end