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.

Keep a preference schema whole when a declaration predates internal preferences

Damian Legawiec78cc94857e64
diff --git a/spree/core/lib/spree/core/preferences/preferable.rb b/spree/core/lib/spree/core/preferences/preferable.rb
index 85191ceef6..ac88bf8118 100644
--- a/spree/core/lib/spree/core/preferences/preferable.rb
+++ b/spree/core/lib/spree/core/preferences/preferable.rb
@@ -72,7 +72,13 @@ module Spree::Preferences::Preferable
# schema, so nothing offers it as a field to fill in.
def preference_internal(name)
has_preference! name
- send(self.class.preference_internal_getter_method(name))
+ getter = self.class.preference_internal_getter_method(name)
+ # A preference declared before this option existed has no such reader.
+ # Treated as not internal rather than raising, so one old declaration
+ # cannot take a whole class's preference schema down with it.
+ return false unless respond_to?(getter)
+
+ send(getter)
end
def has_preference!(name)
diff --git a/spree/core/spec/models/spree/payment_method_spec.rb b/spree/core/spec/models/spree/payment_method_spec.rb
index 975605a118..38e705db71 100644
--- a/spree/core/spec/models/spree/payment_method_spec.rb
+++ b/spree/core/spec/models/spree/payment_method_spec.rb
@@ -369,5 +369,18 @@ describe Spree::PaymentMethod, type: :model do
expect(gateway.preference_internal(:issued_secret)).to be(true)
expect(gateway.preference_internal(:api_key)).to be_nil
end
+
+ # A gateway loaded from a build that predates this option has no such
+ # reader. Schema computation rescues everything into an empty list, so
+ # without a guard one old declaration would strip every field from the
+ # class — including the password ones the admin form relies on.
+ it 'still describes a class whose declarations predate the option' do
+ gateway_class.send(:undef_method, :preferred_issued_secret_internal)
+ gateway_class.instance_variable_set(:@preference_schema, nil)
+
+ keys = gateway_class.preference_schema.map { |field| field[:key] }
+
+ expect(keys).to include(:api_key, :issued_secret)
+ end
end
end