Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: solidusio/solidus
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: fc02e8890d2071bf6c570550e78c57cda1dfbabb
Choose a base ref
..
head repository: solidusio/solidus
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: ea53bc9ea1f6d81d471a15e43a4cee23eb8b7802
Choose a head ref
5 changes: 5 additions & 0 deletions core/lib/spree/preferences/preferable.rb
Original file line number Diff line number Diff line change
@@ -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 momoize #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:`
@@ -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|
2 changes: 1 addition & 1 deletion core/lib/spree/preferences/preferable_class_methods.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 9 additions & 3 deletions core/spec/models/spree/preferences/preferable_spec.rb
Original file line number Diff line number Diff line change
@@ -10,10 +10,15 @@ class A

def initialize
@id = rand(999)
initialize_preference_defaults
end

def preferences
@preferences ||= default_preferences
@preferences ||= {}
end

def initialize_preference_defaults
@preferences = default_preferences.merge(preferences)
end

preference :color, :string, default: 'green'
@@ -153,11 +158,11 @@ def self.allowed_admin_form_preference_types

context "when a default is a proc" do
before do
A.preference(:context, :string, default: -> { self.to_s })
A.preference(:context, :string, default: -> { preferred_color })
end

it "calls it from the instance" do
expect(@a.preferred_context).to eq(@a.to_s)
expect(@a.preferred_context).to eq("green")
end
end

@@ -311,6 +316,7 @@ def self.allowed_admin_form_preference_types
default: 'my_default_secret',
encryption_key: 'VkYp3s6v9y$B?E(H+MbQeThWmZq4t7w!'

@a = A.new
expect(@a.get_preference(:secret)).to eq('my_default_secret')
expect(@a.preferences[:secret]).not_to eq('my_default_secret')
end