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.

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.rb
index 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' do
Spree.api.storefront_coupon_handler = MyCustomCouponHandler
expect(Spree.api.storefront_coupon_handler).to eq MyCustomCouponHandler
end
+
+ 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')
+ end
end
describe 'Spree.api.dependencies' do
diff --git a/core/lib/spree/core/dependencies_helper.rb b/core/lib/spree/core/dependencies_helper.rb
index 491a95e3c7..51909a9d36 100644
--- a/core/lib/spree/core/dependencies_helper.rb
+++ b/core/lib/spree/core/dependencies_helper.rb
@@ -2,6 +2,12 @@ module Spree
class DependencyError < StandardError; end
module 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.freeze
base.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 Spree
instance_variable_set("@#{injection_point}", value)
end
end
+
+ # 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
+ end
end
end
diff --git a/core/spec/models/spree/app_dependencies_spec.rb b/core/spec/models/spree/app_dependencies_spec.rb
index 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' do
Spree.cart_add_item_service = MyCustomAddItemService
expect(Spree.cart_add_item_service).to eq MyCustomAddItemService
end
+
+ 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')
+ end
end
end