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
Load a request's integrations once, on Spree::Current
Damian Legawiec94d65339de1f
diff --git a/spree/core/app/models/concerns/spree/integration_backed_provider.rb b/spree/core/app/models/concerns/spree/integration_backed_provider.rbindex c89e4be2c4..88d27be75b 100644--- a/spree/core/app/models/concerns/spree/integration_backed_provider.rb+++ b/spree/core/app/models/concerns/spree/integration_backed_provider.rb@@ -36,7 +36,20 @@ module Spreedef available_for_store?(store)return true if integration_class.blank?- store.present? && store.integrations.active.exists?(type: integration_class)+ active_integrations(store).any? { |integration| integration.type == integration_class }+ end++ # The store's active integrations — through {Spree::Current} for the+ # request's own store, so a whole registry's availability checks and+ # every provider's credential lookup share one query per request.+ #+ # @param store [Spree::Store, nil]+ # @return [Array<Spree::Integration>]+ def active_integrations(store)+ return [] if store.nil?+ return Spree::Current.integrations if store.id == Spree::Current.store&.id++ store.integrations.active.to_aend# The name a merchant sees when choosing a provider.@@ -62,18 +75,15 @@ module Spreeend# The connected, active integration carrying this provider's credentials.- #- # No memoization: provider instances are built fresh per resolution, so an- # instance-level cache never gets a second hit. A provider consulting this- # on the catalog read path should do so inside +price_for+ (whose answers- # {Spree::Pricing::PriceResolution} caches by +cache_ttl+), not +handles?+.+ # Request-cached via {Spree::Current.integrations}, so a provider asking+ # per catalog row costs one query per request, not one per row.## @param store [Spree::Store, nil]# @return [Spree::Integration, nil]def integration_for(store)return if self.class.integration_class.blank?- store&.integrations&.active&.find_by(type: self.class.integration_class)+ self.class.active_integrations(store).find { |integration| integration.type == self.class.integration_class }endendenddiff --git a/spree/core/app/models/concerns/spree/integrations_concern.rb b/spree/core/app/models/concerns/spree/integrations_concern.rbindex 10212591e4..2c2ac8141c 100644--- a/spree/core/app/models/concerns/spree/integrations_concern.rb+++ b/spree/core/app/models/concerns/spree/integrations_concern.rb@@ -1,11 +1,24 @@module Spree+ # @deprecated Use {Spree::Current.integrations}; removed in Spree 6.1.+ #+ # Nothing in Spree calls these — the per-request integration set lives on+ # {Spree::Current} — but the concern sits on Spree::Base, so every model has+ # carried the methods as public surface since they shipped.module IntegrationsConcerndef store_integrations- @store_integrations ||= Spree::Store.current.integrations.active.to_a+ Spree::Deprecation.warn(+ 'Spree::IntegrationsConcern#store_integrations is deprecated and will be removed in Spree 6.1. ' 'Use Spree::Current.integrations instead.'+ )++ Spree::Current.integrationsenddef store_integration(name)- store_integrations.find { |integration| integration.type.to_s.demodulize.underscore == name }+ Spree::Deprecation.warn(+ 'Spree::IntegrationsConcern#store_integration is deprecated and will be removed in Spree 6.1. ' 'Use Spree::Current.integrations.find { |integration| integration.type.to_s.demodulize.underscore == name } instead.'+ )++ Spree::Current.integrations.find { |integration| integration.type.to_s.demodulize.underscore == name }endendenddiff --git a/spree/core/app/models/spree/current.rb b/spree/core/app/models/spree/current.rbindex 196242545c..1748eed68c 100644--- a/spree/core/app/models/spree/current.rb+++ b/spree/core/app/models/spree/current.rb@@ -4,7 +4,7 @@ module Spree# All attributes are automatically reset between requests by Rails.# Fallback chains ensure sensible defaults when attributes are not explicitly set.class Current < ::ActiveSupport::CurrentAttributes- attribute :store, :channel, :market, :currency, :locale, :content_locale, :tax_country, :price_lists, :global_pricing_context, :provider_cache+ attribute :store, :channel, :market, :currency, :locale, :content_locale, :tax_country, :price_lists, :global_pricing_context, :provider_cache, :integrations# Scratch space for provider strategies to memoize a call across the# request — part of the delivery rate provider contract (nothing in core@@ -71,6 +71,16 @@ module Spreesuper || (self.tax_country = market&.default_country || store&.default_country)end+ # The current store's active integrations, loaded once per request.+ # Lazy — a request that never asks pays nothing — and assigned on first+ # read the way #tax_country is, because several consumers ask per request:+ # every integration-backed provider resolving credentials, and the+ # settings page checking availability across the whole registry.+ # @return [Array<Spree::Integration>]+ def integrations+ super || (self.integrations = store ? store.integrations.active.to_a : [])+ end+# Returns the current price lists for the global pricing context.# @return [ActiveRecord::Relation<Spree::PriceList>]def price_listsdiff --git a/spree/core/app/models/spree/integration.rb b/spree/core/app/models/spree/integration.rbindex 04f724ef27..d797f31d88 100644--- a/spree/core/app/models/spree/integration.rb+++ b/spree/core/app/models/spree/integration.rb@@ -11,6 +11,11 @@ module Spreeinclude Spree::SingleStoreResourceinclude Spree::PreferenceSchema+ # Spree::Current.integrations snapshots the active set for the request;+ # connecting or deactivating one mid-request (the activate-and-verify+ # flow does) must not leave later reads serving the stale snapshot.+ after_commit -> { Spree::Current.integrations = nil }+registers_subclasses_via { registered_classes }# Integration classes registered via +Spree.integrations+. Entries may bediff --git a/spree/core/spec/models/spree/current_spec.rb b/spree/core/spec/models/spree/current_spec.rbindex 0744a68131..a3ac256b9f 100644--- a/spree/core/spec/models/spree/current_spec.rb+++ b/spree/core/spec/models/spree/current_spec.rb@@ -305,4 +305,23 @@ RSpec.describe Spree::Current doexpect(described_class.provider_cache).to eq({})endend++ describe '#integrations' do+ it 'loads the current store\'s active integrations once and serves the snapshot' do+ active = create(:integration, store: @default_store, active: true)++ expect(described_class.integrations).to eq([active])+ expect(described_class.integrations.object_id).to eq(described_class.integrations.object_id)+ end++ # The activate-and-verify flow writes an integration mid-request; later+ # reads must see it rather than the snapshot taken before.+ it 'drops the snapshot when an integration is written' do+ expect(described_class.integrations).to eq([])++ connected = create(:integration, store: @default_store, active: true)++ expect(described_class.integrations).to eq([connected])+ end+ endend