Skip to content

Commit adbc70b

Browse files
Merge pull request #4449 from nebulab/waiting-for-dev/accept_static_class_name_as_string
Support code reloading when configuring static preferences sources
2 parents fee8a86 + c9562c6 commit adbc70b

3 files changed

Lines changed: 49 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
- Fix CSRF forgery protection bypass for Spree::OrdersController#populate [GHSA-h3fg-h5v3-vf8m](https://github.com/solidusio/solidus/security/advisories/GHSA-h3fg-h5v3-vf8m)
77

8-
**Other important changes**
8+
### Other important changes
9+
10+
#### No more autoload of decorators in fresh applications
911

1012
New Solidus applications won't autoload files matching `app/**/*_decorator*.rb`
1113
pattern anymore. For previous Solidus applications, it's something that will
@@ -43,7 +45,7 @@ of Solidus recommendations (that files are monkey patches; they don't use the
4345
you can place those files in `app/overrides/` and remove the `decorator`
4446
suffix.
4547

46-
### Changes to the promotion system
48+
#### Changes to the promotion system
4749

4850
Promotions with a `match_policy` of `any` are deprecated. If you have promotions
4951
with such a match policy, try running the following rake task:
@@ -64,6 +66,43 @@ set a temporary flag in your `config/initializers/spree.rb` file:
6466
config.allow_promotions_any_match_policy = true
6567
```
6668

69+
#### Static preference sources configured within `.to_prepare` blocks
70+
71+
[Rails 7 no longer supports referring autoloadable classes within an
72+
initializer](https://guides.rubyonrails.org/autoloading_and_reloading_constants.html#autoload-on-boot-and-on-each-reload).
73+
74+
Because of that, we need to change the way we configure static preference sources.
75+
76+
Before:
77+
78+
```ruby
79+
# config/initializers/spree.rb
80+
Spree.config do |config|
81+
config.static_model_preferences.add(
82+
AmazingStore::AmazingPaymentMethod,
83+
'amazing_payment_method_credentials',
84+
credentials: ENV['AMAZING_PAYMENT_METHOD_CREDENTIALS'],
85+
server: Rails.env.production? ? 'production' : 'test',
86+
test_mode: !Rails.env.production?
87+
)
88+
end
89+
```
90+
91+
Now:
92+
93+
```ruby
94+
# config/initializers/spree.rb
95+
Rails.application.config.to_prepare do
96+
Spree::Config.static_model_preferences.add(
97+
AmazingStore::AmazingPaymentMethod,
98+
'amazing_payment_method_credentials',
99+
credentials: ENV['AMAZING_PAYMENT_METHOD_CREDENTIALS'],
100+
server: Rails.env.production? ? 'production' : 'test',
101+
test_mode: !Rails.env.production?
102+
)
103+
end
104+
```
105+
67106
### Core
68107

69108
- Add configuration option for `migration_path` [#4190](https://github.com/solidusio/solidus/pull/4190) ([SuperGoodSoft](https://github.com/supergoodsoft/))

core/lib/spree/preferences/static_model_preferences.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ def initialize
3636
end
3737

3838
def add(klass, name, preferences)
39-
# We use class name instead of class to allow reloading in dev
40-
raise "Static model preference '#{name}' on #{klass} is already defined" if @store[klass.to_s][name]
4139
@store[klass.to_s][name] = Definition.new(klass, preferences)
4240
end
4341

core/spec/models/spree/preferences/static_model_preferences_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ module Spree
2323
expect(definitions).to have_key('my_definition')
2424
end
2525

26+
it "can replace preferences" do
27+
subject.add(preference_class, 'my_definition', { color: "red" })
28+
29+
subject.add(preference_class, 'my_definition', { color: "blue" })
30+
31+
expect(definitions['my_definition'].fetch(:color)).to eq("blue")
32+
end
33+
2634
it "errors assigning invalid preferences" do
2735
expect {
2836
subject.add(preference_class, 'my_definition', { ice_cream: 'chocolate' })

0 commit comments

Comments
 (0)