From 867b55088015eff8059dfb4796ee78c8c19c25f1 Mon Sep 17 00:00:00 2001 From: Beber Date: Wed, 16 Nov 2022 17:34:13 +0100 Subject: [PATCH] Fix call context when a preference default is a proc Before v3.1, the proc given as the default value in a preference had its context bound to the initialized instance. That allowed, for example, referencing other preferences. From v3.1., the context is bound to the class. We are coming back to the behavior by calling the proc with `instance_eval` --- core/lib/spree/preferences/preferable_class_methods.rb | 2 +- core/spec/models/spree/preferences/preferable_spec.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/core/lib/spree/preferences/preferable_class_methods.rb b/core/lib/spree/preferences/preferable_class_methods.rb index 4da470c0c9a..9a6b99c2ab8 100644 --- a/core/lib/spree/preferences/preferable_class_methods.rb +++ b/core/lib/spree/preferences/preferable_class_methods.rb @@ -92,7 +92,7 @@ def preference(name, type, options = {}) end define_method preference_default_getter_method(name) do - default.call(*context_for_default) + instance_exec(*context_for_default, &default) end define_method preference_type_getter_method(name) do diff --git a/core/spec/models/spree/preferences/preferable_spec.rb b/core/spec/models/spree/preferences/preferable_spec.rb index 25addfa03ff..0ac82ec5ac7 100644 --- a/core/spec/models/spree/preferences/preferable_spec.rb +++ b/core/spec/models/spree/preferences/preferable_spec.rb @@ -151,6 +151,16 @@ def self.allowed_admin_form_preference_types end end + context "when a default is a proc" do + before do + A.preference(:context, :string, default: -> { self.to_s }) + end + + it "calls it from the instance" do + expect(@a.preferred_context).to eq(@a.to_s) + end + end + context "converts integer preferences to integer values" do before do A.preference :is_integer, :integer