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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
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.rbindex 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 Spreeprivatedef scope- current_store.checkouts.accessible_by(current_ability, :index)+ current_store.checkouts.accessible_by(current_ability, :index).includes(collection_includes)enddef collection_default_sort@@ -25,7 +25,7 @@ module Spreeenddef collection_includes- { user: [], shipments: :stock_location }+ { user: [] }enddef edit_object_url(object, options = {})diff --git a/admin/app/controllers/spree/admin/orders_controller.rb b/admin/app/controllers/spree/admin/orders_controller.rbindex 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 Spreebase_scope.completeelsebase_scope- end+ end.includes(collection_includes)enddef collection_default_sort@@ -93,7 +93,7 @@ module Spreeenddef collection_includes- { user: [], shipments: :stock_location }+ { user: [], payments: [], refunds: [], shipments: :stock_location }enddef order_paramsdiff --git a/admin/app/controllers/spree/admin/products_controller.rb b/admin/app/controllers/spree/admin/products_controller.rbindex 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 Spreeparams[:product] = params_service.callend+ # 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 Spreeclear_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_availabilitynew_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.rbindex 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.rbindex 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 Spreeenddef 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' doSpree.t('admin.not_tracking_inventory')end@@ -20,8 +20,8 @@ module Spreeelsecontent_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}"endstockenddiff --git a/api/app/controllers/concerns/spree/api/v2/product_list_includes.rb b/api/app/controllers/concerns/spree/api/v2/product_list_includes.rbindex 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 Spreemodule Apimodule V2+ # These includes are not picked automatically by ar_lazy_preload gem so we need to specify them manually.module ProductListIncludesdef 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_includesendendenddiff --git a/api/app/controllers/spree/api/v2/resource_controller.rb b/api/app/controllers/spree/api/v2/resource_controller.rbindex 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 Spreeenddef 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+ endenddef finder_paramsdiff --git a/api/app/controllers/spree/api/v2/storefront/products_controller.rb b/api/app/controllers/spree/api/v2/storefront/products_controller.rbindex 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 Spreeenddef variant_includes- [- :prices,- { option_values: [:option_type] }- ]+ [:images, :prices, { stock_items: :stock_location }]enddef collection_sorterdiff --git a/api/app/serializers/spree/v2/storefront/product_serializer.rb b/api/app/serializers/spree/v2/storefront/product_serializer.rbindex 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 Spreehas_many :product_properties, serializer: Spree.api.storefront_product_property_serializerhas_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 variantsdiff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rbindex c44d860d11..a30789daba 100644--- a/core/app/models/spree/order.rb+++ b/core/app/models/spree/order.rb@@ -271,15 +271,26 @@ module Spreecompleted_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.absend+ 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.absend# 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.rbindex 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)+ endend# Returns true if the order is using store credit.diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rbindex 5cdea7425a..0c4e22ca5a 100644--- a/core/app/models/spree/product.rb+++ b/core/app/models/spree/product.rb@@ -649,7 +649,7 @@ module Spreereturn 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)endenddiff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rbindex 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.rbindex 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 doexpect(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) }enddescribe '#any_variant_in_stock_or_backorderable?' dodiff --git a/core/spree_core.gemspec b/core/spree_core.gemspecindex 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'enddiff --git a/storefront/lib/spree/storefront.rb b/storefront/lib/spree/storefront.rbindex 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.gemspecindex 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