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.

Rewrote some queries to reduce number of SQL joins

Damian Legawiecccda04992eee
diff --git a/core/app/models/spree/option_value.rb b/core/app/models/spree/option_value.rb
index 5a71bc7f60..52d010b3de 100644
--- a/core/app/models/spree/option_value.rb
+++ b/core/app/models/spree/option_value.rb
@@ -48,8 +48,9 @@ module Spree
}
scope :for_products, lambda { |products|
- joins(:variants).
- where(Variant.table_name => { product_id: products.map(&:id) })
+ product_ids = products.loaded? ? products.map(&:id) : products
+
+ joins(:variants).where(Spree::Variant.table_name => { product_id: product_ids })
}
#
diff --git a/core/app/models/spree/price.rb b/core/app/models/spree/price.rb
index 9549c550e0..9ade5def36 100644
--- a/core/app/models/spree/price.rb
+++ b/core/app/models/spree/price.rb
@@ -36,7 +36,13 @@ module Spree
scope :with_currency, ->(currency) { where(currency: currency) }
scope :non_zero, -> { where.not(amount: [nil, 0]) }
scope :discounted, -> { where('compare_at_amount > amount') }
- scope :for_products, ->(products) { joins(variant: :product).where("#{Spree::Product.table_name}.id" => products) }
+ scope :for_products, ->(products, currency = nil) do
+ currency ||= Spree::Store.default.default_currency
+
+ with_currency(currency).joins(:variant).where(
+ Spree::Variant.table_name => { product_id: products }
+ )
+ end
extend DisplayMoney
money_methods :amount, :price, :compare_at_amount
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index 22789c479b..eb6e4b456a 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -227,7 +227,7 @@ module Spree
having("SUM(#{Spree::StockItem.table_name}.count_on_hand) <= 0")
}
scope :out_of_stock, lambda {
- joins(:stock_items).where("#{Spree::StockItem.table_name}.count_on_hand <= ? OR #{Spree::Variant.table_name}.track_inventory = ?", 0, false)
+ joins(:stock_items).where("#{Spree::Variant.table_name}.track_inventory = ? OR #{Spree::StockItem.table_name}.count_on_hand <= ?", false, 0)
}
scope :by_best_selling, lambda { |order_direction = :desc|
diff --git a/core/app/models/spree/product_property.rb b/core/app/models/spree/product_property.rb
index 55023a1d72..b199fc205e 100644
--- a/core/app/models/spree/product_property.rb
+++ b/core/app/models/spree/product_property.rb
@@ -31,9 +31,9 @@ module Spree
default_scope { order(:position) }
scope :filterable, -> { joins(:property).where(Property.table_name => { filterable: true }) }
- scope :for_products, ->(products) { joins(:product).merge(products) }
+ scope :for_products, ->(products) { where(product_id: products) }
scope :sort_by_property_position, -> {
- unscope(:order).joins(:property).order("spree_properties.position ASC")
+ unscope(:order).joins(:property).order(Spree::Property.table_name => { position: :asc })
}
self.whitelisted_ransackable_attributes = ['value', 'filter_param']
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index 18d3fbf154..8433bb3150 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -82,7 +82,7 @@ module Spree
after_touch :clear_in_stock_cache
- scope :in_stock, -> { left_joins(:stock_items).where("#{Spree::StockItem.table_name}.count_on_hand > ? OR #{Spree::Variant.table_name}.track_inventory = ?", 0, false) }
+ scope :in_stock, -> { left_joins(:stock_items).where("#{Spree::Variant.table_name}.track_inventory = ? OR #{Spree::StockItem.table_name}.count_on_hand > ?", false, 0) }
scope :backorderable, -> { left_joins(:stock_items).where(spree_stock_items: { backorderable: true }) }
scope :in_stock_or_backorderable, -> { in_stock.or(backorderable) }