22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/spree/preferences/preferable_class_methods.rb', line 22
def preference(name, type, options = {})
options.assert_valid_keys(:default, :encryption_key)
if type == :encrypted_string
preference_encryptor = preference_encryptor(options)
options[:default] = preference_encryptor.encrypt(options[:default])
end
default = begin
given = options[:default]
if ancestors.include?(Spree::Preferences::Configuration) &&
given.is_a?(Proc) &&
given.lambda? &&
given.arity.zero?
Spree::Deprecation.warn " The arity of a proc given as the default for a preference\n has changed from 0 to 1 on Solidus 3.1. The Solidus\n version for the loaded preference defaults is given as the\n proc's argument from this point on.\n\n If you don't need to return a different default value\n depending on the loaded Solidus version, you can change\n the proc so that it doesn't have lambda semantics (lambdas\n raise when extra arguments are supplied, while raw procs\n don't). E.g.:\n\n preference :foo, :string, default: proc { true }\n\n If you want to branch on the provided Solidus version, you can do like the following:\n\n preference :foo, :string, default: by_version(true, \"3.2.0\" => false)\n\n MSG\n ->(_default_context) { given.call }\n elsif given.is_a?(Proc)\n given\n else\n proc { given }\n end\n end\n\n # The defined preferences on a class are all those defined directly on\n # that class as well as those defined on ancestors.\n # We store these as a class instance variable on each class which has a\n # preference. super() collects preferences defined on ancestors.\n singleton_preferences = (@defined_singleton_preferences ||= [])\n singleton_preferences << name.to_sym\n\n define_singleton_method :defined_preferences do\n super() + singleton_preferences\n end\n\n # cache_key will be nil for new objects, then if we check if there\n # is a pending preference before going to default\n define_method preference_getter_method(name) do\n value = preferences.fetch(name) do\n instance_exec(*context_for_default, &default)\n end\n value = preference_encryptor.decrypt(value) if preference_encryptor.present?\n value\n end\n\n define_method preference_setter_method(name) do |value|\n value = convert_preference_value(value, type, preference_encryptor)\n preferences[name] = value\n\n # If this is an activerecord object, we need to inform\n # ActiveRecord::Dirty that this value has changed, since this is an\n # in-place update to the preferences hash.\n preferences_will_change! if respond_to?(:preferences_will_change!)\n end\n\n define_method preference_default_getter_method(name) do\n instance_exec(*context_for_default, &default)\n end\n\n define_method preference_type_getter_method(name) do\n type\n end\nend\n"
|