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 `ar_lazy_preload` to API and admin as well (#13479)

Damian Legawiecd62abb602005
diff --git a/admin/app/controllers/spree/admin/checkouts_controller.rb b/admin/app/controllers/spree/admin/checkouts_controller.rb
index 08f9d7e4c2..9946b535fa 100644
--- a/admin/app/controllers/spree/admin/checkouts_controller.rb
+++ b/admin/app/controllers/spree/admin/checkouts_controller.rb
@@ -17,7 +17,7 @@ module Spree
private
def scope
- current_store.checkouts.accessible_by(current_ability, :index)
+ current_store.checkouts.accessible_by(current_ability, :index).includes(collection_includes)
end
def collection_default_sort
@@ -25,7 +25,7 @@ module Spree
end
def collection_includes
- { user: [], shipments: :stock_location }
+ { user: [] }
end
def edit_object_url(object, options = {})
diff --git a/admin/app/controllers/spree/admin/orders_controller.rb b/admin/app/controllers/spree/admin/orders_controller.rb
index 77e8502fda..19dce40641 100644
--- a/admin/app/controllers/spree/admin/orders_controller.rb
+++ b/admin/app/controllers/spree/admin/orders_controller.rb
@@ -85,7 +85,7 @@ module Spree
base_scope.complete
else
base_scope
- end
+ end.includes(collection_includes)
end
def collection_default_sort
@@ -93,7 +93,7 @@ module Spree
end
def collection_includes
- { user: [], shipments: :stock_location }
+ { user: [], payments: [], refunds: [], shipments: :stock_location }
end
def order_params
diff --git a/admin/app/controllers/spree/admin/products_controller.rb b/admin/app/controllers/spree/admin/products_controller.rb
index 156bc567ee..cff7adb8d4 100644
--- a/admin/app/controllers/spree/admin/products_controller.rb
+++ b/admin/app/controllers/spree/admin/products_controller.rb
@@ -218,12 +218,12 @@ module Spree
params[:product] = params_service.call
end
+ # These includes are not picked automatically by ar_lazy_preload gem so we need to specify them manually.
def collection_includes
{
stock_items: [],
- variants_including_master: [],
- master: [:prices, :images, :stock_items],
- variants: [:prices, :images, :stock_items],
+ master: [:images, :prices, :stock_items],
+ variants: [:images, :prices, :stock_items]
}
end
@@ -293,17 +293,6 @@ module Spree
clear_session_for_uploaded_assets('Spree::Variant')
end
- def product_includes
- {
- tax_category: [],
- stock_items: [:stock_location],
- variants_including_master: [],
- shipping_category: [],
- master: [:prices, :images, :stock_items, :stock_locations],
- variants: [:prices, :images, :stock_items, :stock_locations],
- }
- end
-
def check_slug_availability
new_slug = permitted_resource_params[:slug]
permitted_resource_params[:slug] = @product.ensure_slug_is_unique(new_slug)
diff --git a/admin/app/controllers/spree/admin/resource_controller.rb b/admin/app/controllers/spree/admin/resource_controller.rb
index d419c13907..b1a9573db5 100644
--- a/admin/app/controllers/spree/admin/resource_controller.rb
+++ b/admin/app/controllers/spree/admin/resource_controller.rb
@@ -280,7 +280,7 @@ class Spree::Admin::ResourceController < Spree::Admin::BaseController
# @return [ActiveRecord::Relation]
def collection
@collection ||= begin
- result = search_collection.result(distinct: true)
+ result = search_collection.result(distinct: true).preload_associations_lazily
# Apply custom sort scope if configured (e.g., for price sorting)
result = apply_table_sort(result) if table_registered? && custom_sort_active?
diff --git a/admin/app/helpers/spree/admin/products_helper.rb b/admin/app/helpers/spree/admin/products_helper.rb
index b5c75c597a..9bcc050120 100644
--- a/admin/app/helpers/spree/admin/products_helper.rb
+++ b/admin/app/helpers/spree/admin/products_helper.rb
@@ -10,7 +10,7 @@ module Spree
end
def display_inventory(product_or_variant)
- if product_or_variant.total_on_hand == BigDecimal::INFINITY
+ unless product_or_variant.track_inventory?
content_tag :span, class: 'text-gray-600' do
Spree.t('admin.not_tracking_inventory')
end
@@ -20,8 +20,8 @@ module Spree
else
content_tag(:span, "0 #{Spree.t(:in_stock).downcase}", class: 'text-danger')
end
- if product_or_variant.respond_to?(:variants) && product_or_variant.variants.size.positive?
- stock += " - #{product_or_variant.variants.size} #{Spree.t(:variants).downcase}"
+ if product_or_variant.respond_to?(:variants) && product_or_variant.variant_count.positive?
+ stock += " - #{product_or_variant.variant_count} #{Spree.t(:variants).downcase}"
end
stock
end
diff --git a/api/app/controllers/concerns/spree/api/v2/product_list_includes.rb b/api/app/controllers/concerns/spree/api/v2/product_list_includes.rb
index d97eccf2b3..ad287b3bbf 100644
--- a/api/app/controllers/concerns/spree/api/v2/product_list_includes.rb
+++ b/api/app/controllers/concerns/spree/api/v2/product_list_includes.rb
@@ -1,31 +1,19 @@
module Spree
module Api
module V2
+ # These includes are not picked automatically by ar_lazy_preload gem so we need to specify them manually.
module ProductListIncludes
def product_list_includes
- @product_list_includes ||= {
- taggings: [:tag],
- variants: [],
- master: [:prices]
+ {
+ option_types: [],
+ product_properties: [],
+ metafields: [],
+ variant_images: [],
+ tags: [],
+ taxons: [:taxonomy],
+ master: [:prices, :images, { stock_items: :stock_location }, metafields: [], option_values: []],
+ variants: [:prices, :images, { stock_items: :stock_location }, metafields: [], option_values: []]
}
-
- @product_list_includes[:variant_images] = [] if params[:include]&.match('images')
- @product_list_includes[:option_types] = [] if params[:include]&.match('option_types')
- @product_list_includes[:product_properties] = [:property] if params[:include]&.match('product_properties')
- @product_list_includes[:master] = variant_includes if params[:include]&.match(/master|default_variant/)
- @product_list_includes[:variants] = variant_includes if params[:include]&.match(/variants|default_variant/)
- @product_list_includes[:taxons] = [:taxonomy, :icon, :store, :rich_text_translations, image_attachment: :blob] if params[:include]&.match('taxons')
- @product_list_includes
- end
-
- def variant_includes
- variant_includes = {
- prices: [],
- option_values: :option_type,
- stock_items: :stock_location
- }
- variant_includes[:images] = [] if params[:include]&.match(/images/)
- variant_includes
end
end
end
diff --git a/api/app/controllers/spree/api/v2/resource_controller.rb b/api/app/controllers/spree/api/v2/resource_controller.rb
index f93252d2a8..598afc4cf2 100644
--- a/api/app/controllers/spree/api/v2/resource_controller.rb
+++ b/api/app/controllers/spree/api/v2/resource_controller.rb
@@ -51,11 +51,14 @@ module Spree
end
def collection
- @collection ||= if defined?(collection_finder)
- collection_finder.new(scope: scope, params: finder_params).execute
- else
- scope
- end
+ @collection ||= begin
+ result = if defined?(collection_finder)
+ collection_finder.new(scope: scope, params: finder_params).execute
+ else
+ scope
+ end
+ result.preload_associations_lazily
+ end
end
def finder_params
diff --git a/api/app/controllers/spree/api/v2/storefront/products_controller.rb b/api/app/controllers/spree/api/v2/storefront/products_controller.rb
index 420ede01d5..8ecc3f7649 100644
--- a/api/app/controllers/spree/api/v2/storefront/products_controller.rb
+++ b/api/app/controllers/spree/api/v2/storefront/products_controller.rb
@@ -21,10 +21,7 @@ module Spree
end
def variant_includes
- [
- :prices,
- { option_values: [:option_type] }
- ]
+ [:images, :prices, { stock_items: :stock_location }]
end
def collection_sorter
diff --git a/api/app/serializers/spree/v2/storefront/product_serializer.rb b/api/app/serializers/spree/v2/storefront/product_serializer.rb
index 50bf9362a7..aaaece4d42 100644
--- a/api/app/serializers/spree/v2/storefront/product_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/product_serializer.rb
@@ -65,7 +65,7 @@ module Spree
has_many :product_properties, serializer: Spree.api.storefront_product_property_serializer
has_many :taxons, serializer: Spree.api.storefront_taxon_serializer, record_type: :taxon do |object, params|
- object.taxons_for_store(params[:store])&.order(:id)
+ object.taxons_for_store(params[:store])
end
# all images from all variants
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb
index c44d860d11..a30789daba 100644
--- a/core/app/models/spree/order.rb
+++ b/core/app/models/spree/order.rb
@@ -271,15 +271,26 @@ module Spree
completed_at.present?
end
+ # Checks if the order is fully refunded
+ # @return [Boolean]
def order_refunded?
- (payment_state.in?(%w[void failed]) && refunds.sum(:amount).positive?) ||
- refunds.sum(:amount) == total_minus_store_credits - additional_tax_total.abs
+ return false if item_count.zero?
+
+ (payment_state.in?(%w[void failed]) && refunds_total.positive?) ||
+ refunds_total == total_minus_store_credits - additional_tax_total.abs
end
+ def refunds_total
+ refunds.loaded? ? refunds.sum(&:amount) : refunds.sum(:amount)
+ end
+
+ # Checks if the order is partially refunded
+ # @return [Boolean]
def partially_refunded?
- return false if refunds.empty? || payment_state.in?(%w[void failed])
+ return false if item_count.zero?
+ return false if payment_state.in?(%w[void failed]) || refunds.empty?
- refunds.sum(:amount) < total_minus_store_credits - additional_tax_total.abs
+ refunds_total < total_minus_store_credits - additional_tax_total.abs
end
# Indicates whether or not the user is allowed to proceed to checkout.
diff --git a/core/app/models/spree/order/store_credit.rb b/core/app/models/spree/order/store_credit.rb
index 1a894b73b6..328160023e 100644
--- a/core/app/models/spree/order/store_credit.rb
+++ b/core/app/models/spree/order/store_credit.rb
@@ -65,7 +65,11 @@ module Spree
#
# @return [BigDecimal] The total amount of store credits applied to the order.
def total_applied_store_credit
- payments.store_credits.valid.sum(:amount)
+ if payments.loaded?
+ payments.find_all(&:store_credit?).find_all(&:valid?).sum(&:amount) || BigDecimal::ZERO
+ else
+ payments.store_credits.valid.sum(:amount)
+ end
end
# Returns true if the order is using store credit.
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index 5cdea7425a..0c4e22ca5a 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -649,7 +649,7 @@ module Spree
return if classification_count.zero?
Rails.cache.fetch("#{cache_key_with_version}/taxons-per-store/#{store.id}") do
- taxons.for_store(store)
+ taxons.loaded? ? taxons.find_all { |taxon| taxon.taxonomy.store_id == store.id } : taxons.for_store(store)
end
end
diff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rb
index 7eaea1c612..9797afedc9 100644
--- a/core/lib/spree/core.rb
+++ b/core/lib/spree/core.rb
@@ -30,6 +30,7 @@ require 'wannabe_bool'
require 'geocoder'
require 'oembed'
require 'safely_block'
+require 'ar_lazy_preload'
# This is required because ActiveModel::Validations#invalid? conflicts with the
# invalid state of a Payment. In the future this should be removed.
diff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rb
index c254e89614..b2e31e7804 100644
--- a/core/spec/models/spree/product_spec.rb
+++ b/core/spec/models/spree/product_spec.rb
@@ -1192,8 +1192,6 @@ describe Spree::Product, type: :model do
expect(product.taxons_for_store(store)).to eq([taxon])
expect(product.taxons_for_store(store_2)).to eq([taxon_2])
end
-
- it { expect(product.taxons_for_store(store)).to be_a(ActiveRecord::Relation) }
end
describe '#any_variant_in_stock_or_backorderable?' do
diff --git a/core/spree_core.gemspec b/core/spree_core.gemspec
index b4133dfdff..8a27bca512 100644
--- a/core/spree_core.gemspec
+++ b/core/spree_core.gemspec
@@ -64,4 +64,5 @@ Gem::Specification.new do |s|
s.add_dependency 'any_ascii', '~> 0.3.2'
s.add_dependency 'safely_block', '~> 0.4'
s.add_dependency 'phonelib', '~> 0.10'
+ s.add_dependency 'ar_lazy_preload', '~> 2.0'
end
diff --git a/storefront/lib/spree/storefront.rb b/storefront/lib/spree/storefront.rb
index 0a846321a5..439567c37d 100644
--- a/storefront/lib/spree/storefront.rb
+++ b/storefront/lib/spree/storefront.rb
@@ -11,7 +11,6 @@ require 'tailwindcss-rails'
require 'turbo-rails'
require 'inline_svg'
require 'pagy'
-require 'ar_lazy_preload'
require 'spree/storefront/engine'
require 'spree/core/partials'
diff --git a/storefront/spree_storefront.gemspec b/storefront/spree_storefront.gemspec
index ff276d6df0..01c7c000f6 100644
--- a/storefront/spree_storefront.gemspec
+++ b/storefront/spree_storefront.gemspec
@@ -37,5 +37,4 @@ Gem::Specification.new do |s|
s.add_dependency 'tailwindcss-ruby'
s.add_dependency 'turbo-rails'
s.add_dependency 'pagy', '~> 43.0'
- s.add_dependency 'ar_lazy_preload', '~> 2.0'
end