Skip to content

Commit

Permalink
Merge pull request #4721 from Roddoric/fix-context-for-proc-in-prefer…
Browse files Browse the repository at this point in the history
…ences

Fix call context when a preference default is a proc
  • Loading branch information
waiting-for-dev authored Dec 7, 2022
2 parents 779b648 + 67bd03d commit aa3bbed
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 99 deletions.
5 changes: 5 additions & 0 deletions core/lib/spree/preferences/preferable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module Preferences
#
# A class including Preferable must implement #preferences which should return
# an object responding to .fetch(key), []=(key, val), and .delete(key).
# If #preferences is initialized with `default_preferences` and one of the
# preferences is another preference, it will cause a stack level too deep error.
# To avoid it do not memoize #preferences.
#
# It may also define a `#context_for_default` method. It should return an
# array with the arguments to be provided to a proc used as the `default:`
Expand Down Expand Up @@ -111,6 +114,8 @@ def defined_preferences
end

# @return [Hash{Symbol => Object}] Default for all preferences defined on this class
# This may raise an infinite loop error if any of the defaults are
# dependent on other preferences defaults.
def default_preferences
Hash[
defined_preferences.map do |preference|
Expand Down
4 changes: 2 additions & 2 deletions core/lib/spree/preferences/preferable_class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def preference(name, type, options = {})
# is a pending preference before going to default
define_method preference_getter_method(name) do
value = preferences.fetch(name) do
default.call(*context_for_default)
instance_exec(*context_for_default, &default)
end
value = preference_encryptor.decrypt(value) if preference_encryptor.present?
value
Expand All @@ -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
Expand Down
Loading

0 comments on commit aa3bbed

Please sign in to comment.