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
Introduce `Adjustment#cached_source`
Damian Legawiec4ef1958e30ab
diff --git a/core/app/models/concerns/spree/adjustment_source.rb b/core/app/models/concerns/spree/adjustment_source.rbindex 1bde610b10..45fa329d45 100644--- a/core/app/models/concerns/spree/adjustment_source.rb+++ b/core/app/models/concerns/spree/adjustment_source.rb@@ -5,6 +5,7 @@ module Spreeincluded dohas_many :adjustments, as: :sourcebefore_destroy :deals_with_adjustments_for_deleted_source+ after_commit :clear_adjustment_source_cacheendprotected@@ -54,5 +55,12 @@ module Spree# This would mean that the order's total is not altered at all.adjustments.for_complete_order.update_all(source_id: nil, updated_at: Time.current)end++ def clear_adjustment_source_cache+ # Use base_class.name to match the polymorphic_name stored in source_type+ # For STI models like PromotionAction subclasses, this ensures cache key consistency+ cache_class_name = self.class.polymorphic_name+ Rails.cache.delete("spree/adjustment_source/#{cache_class_name}/#{id}")+ endendenddiff --git a/core/app/models/spree/adjustable/promotion_accumulator.rb b/core/app/models/spree/adjustable/promotion_accumulator.rbindex fd00697e6b..c4517317a8 100644--- a/core/app/models/spree/adjustable/promotion_accumulator.rb+++ b/core/app/models/spree/adjustable/promotion_accumulator.rb@@ -25,7 +25,7 @@ module Spreedef add_adjustment(adjustment, opts = {})return unless adjustment.promotion?- source = opts[:source] || adjustment.source+ source = opts[:source] || adjustment.cached_sourcepromotion = opts[:promotion] || source.promotionadd(adjustments, adjustment, adjustment.id)diff --git a/core/app/models/spree/adjustment.rb b/core/app/models/spree/adjustment.rbindex d7c837dd7f..a2d1abf5ae 100644--- a/core/app/models/spree/adjustment.rb+++ b/core/app/models/spree/adjustment.rb@@ -96,15 +96,35 @@ module Spree!included?end+ # Returns the source using Rails.cache to avoid repeated database lookups.+ # Sources are cached by their type and ID combination.+ # Cache is automatically invalidated when the source is saved (see AdjustmentSource concern).+ #+ # @return [Object, nil] The source object (TaxRate, PromotionAction, etc.)+ def cached_source+ return nil if source_type.blank? || source_id.blank?++ Rails.cache.fetch(source_cache_key) { source }+ rescue TypeError+ # Handle objects that can't be serialized (e.g., mock objects in tests)+ source+ end++ # Cache key for the source object+ def source_cache_key+ "spree/adjustment_source/#{source_type}/#{source_id}"+ end+# Passing a target here would always be recommended as it would avoid# hitting the database again and would ensure you're compute values over# the specific object amount passed here.def update!(target = adjustable)- return amount if closed? || source.blank?+ src = cached_source+ return amount if closed? || src.blank?- amount = source.compute_amount(target)+ amount = src.compute_amount(target)attributes = { amount: amount, updated_at: Time.current }- attributes[:eligible] = source.promotion.eligible?(target) if promotion?+ attributes[:eligible] = src.promotion.eligible?(target) if promotion?update_columns(attributes)diff --git a/core/app/models/spree/promotion_handler/coupon.rb b/core/app/models/spree/promotion_handler/coupon.rbindex 38644c0cab..215e2b5b7f 100644--- a/core/app/models/spree/promotion_handler/coupon.rb+++ b/core/app/models/spree/promotion_handler/coupon.rb@@ -177,8 +177,8 @@ module Spree# Check for applied adjustments.discount = order.all_adjustments.promotion.eligible.detect do |p|- p.source.promotion.code.try(:downcase) == coupon_code ||- p.source.promotion.coupon_codes.unused.where(code: coupon_code).exists?+ p.cached_source.promotion.code.try(:downcase) == coupon_code ||+ Spree::CouponCode.unused.where(promotion_id: p.cached_source.promotion_id, code: coupon_code).exists?end# Check for applied line items.diff --git a/core/spec/models/spree/adjustment_spec.rb b/core/spec/models/spree/adjustment_spec.rbindex 84ac143722..6f4e300349 100644--- a/core/spec/models/spree/adjustment_spec.rb+++ b/core/spec/models/spree/adjustment_spec.rb@@ -220,4 +220,39 @@ describe Spree::Adjustment, type: :model doendendend++ describe '#cached_source' do+ let(:order) { create(:order) }+ let(:tax_rate) { create(:tax_rate) }+ let(:adjustment) { create(:adjustment, order: order, source: tax_rate) }++ it 'returns the source' do+ expect(adjustment.cached_source).to eq(tax_rate)+ end++ it 'uses Rails.cache.fetch to cache the source' do+ expect(Rails.cache).to receive(:fetch).with(adjustment.source_cache_key).and_call_original+ adjustment.cached_source+ end++ it 'returns nil when source_type is blank' do+ adjustment.update_columns(source_type: nil, source_id: nil)+ expect(adjustment.cached_source).to be_nil+ end++ it 'returns nil when source_id is blank' do+ adjustment.update_columns(source_id: nil)+ expect(adjustment.cached_source).to be_nil+ end+ end++ describe '#source_cache_key' do+ let(:order) { create(:order) }+ let(:tax_rate) { create(:tax_rate) }+ let(:adjustment) { create(:adjustment, order: order, source: tax_rate) }++ it 'returns a cache key based on source type and id' do+ expect(adjustment.source_cache_key).to eq("spree/adjustment_source/Spree::TaxRate/#{tax_rate.id}")+ end+ endend