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
Fixed Bug: Override source tracking records internal code location, not caller
Damian Legawiec906a8dec7848
diff --git a/api/spec/models/spree/api_dependencies_spec.rb b/api/spec/models/spree/api_dependencies_spec.rbindex d8858bccb7..55a70e21bc 100644--- a/api/spec/models/spree/api_dependencies_spec.rb+++ b/api/spec/models/spree/api_dependencies_spec.rb@@ -111,6 +111,15 @@ describe 'Spree.api accessor' doSpree.api.storefront_coupon_handler = MyCustomCouponHandlerexpect(Spree.api.storefront_coupon_handler).to eq MyCustomCouponHandlerend++ it 'tracks override source correctly (not internal routing code)' do+ Spree.api.storefront_coupon_handler = MyCustomCouponHandler+ info = Spree::Api::Dependencies.override_info(:storefront_coupon_handler)++ # Should point to this spec file, not api.rb method_missing+ expect(info[:source]).to include('api_dependencies_spec.rb')+ expect(info[:source]).not_to include('lib/spree/api.rb')+ endenddescribe 'Spree.api.dependencies' dodiff --git a/core/lib/spree/core/dependencies_helper.rb b/core/lib/spree/core/dependencies_helper.rbindex 491a95e3c7..51909a9d36 100644--- a/core/lib/spree/core/dependencies_helper.rb+++ b/core/lib/spree/core/dependencies_helper.rb@@ -2,6 +2,12 @@ module Spreeclass DependencyError < StandardError; endmodule DependenciesHelper+ # Patterns to skip when finding the actual caller (internal routing code)+ INTERNAL_CALLER_PATTERNS = [+ %r{lib/spree/core\.rb.*method_missing},+ %r{lib/spree/api\.rb.*method_missing}+ ].freeze+def self.included(base)injection_points = base::INJECTION_POINTS_WITH_DEFAULTS.keys.freezebase.const_set(:INJECTION_POINTS, injection_points)@@ -17,7 +23,7 @@ module Spree@overrides ||= {}@overrides[point] = {value: value,- source: caller_locations(1, 1).first.to_s,+ source: find_caller_source,set_at: Time.current}# Clear memoized class when value changes@@ -100,5 +106,18 @@ module Spreeinstance_variable_set("@#{injection_point}", value)endend++ # Find the actual caller source, skipping internal DSL routing code+ # This ensures Spree.foo = X reports the user's code location, not method_missing+ def find_caller_source+ caller_locations(2, 10).each do |location|+ location_str = location.to_s+ next if INTERNAL_CALLER_PATTERNS.any? { |pattern| location_str.match?(pattern) }++ return location_str+ end+ # Fallback to immediate caller if no external caller found+ caller_locations(2, 1).first.to_s+ endendenddiff --git a/core/spec/models/spree/app_dependencies_spec.rb b/core/spec/models/spree/app_dependencies_spec.rbindex ea490393e9..682f188b79 100644--- a/core/spec/models/spree/app_dependencies_spec.rb+++ b/core/spec/models/spree/app_dependencies_spec.rb@@ -176,5 +176,14 @@ describe 'Spree module dependency accessors' doSpree.cart_add_item_service = MyCustomAddItemServiceexpect(Spree.cart_add_item_service).to eq MyCustomAddItemServiceend++ it 'tracks override source correctly (not internal routing code)' do+ Spree.cart_add_item_service = MyCustomAddItemService+ info = Spree::Dependencies.override_info(:cart_add_item_service)++ # Should point to this spec file, not core.rb method_missing+ expect(info[:source]).to include('app_dependencies_spec.rb')+ expect(info[:source]).not_to include('lib/spree/core.rb')+ endendend