Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix call context when a preference default is a proc #4721

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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