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

Support code reloading when configuring static preferences sources #4449

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
43 changes: 41 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

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

**Other important changes**
### Other important changes

#### No more autoload of decorators in fresh applications

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

### Changes to the promotion system
#### Changes to the promotion system

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

#### Static preference sources configured within `.to_prepare` blocks

[Rails 7 no longer supports referring autoloadable classes within an
initializer](https://guides.rubyonrails.org/autoloading_and_reloading_constants.html#autoload-on-boot-and-on-each-reload).

Because of that, we need to change the way we configure static preference sources.

Before:

```ruby
# config/initializers/spree.rb
Spree.config do |config|
config.static_model_preferences.add(
AmazingStore::AmazingPaymentMethod,
'amazing_payment_method_credentials',
credentials: ENV['AMAZING_PAYMENT_METHOD_CREDENTIALS'],
server: Rails.env.production? ? 'production' : 'test',
test_mode: !Rails.env.production?
)
end
```

Now:

```ruby
# config/initializers/spree.rb
Rails.application.config.to_prepare do
Spree::Config.static_model_preferences.add(
AmazingStore::AmazingPaymentMethod,
'amazing_payment_method_credentials',
credentials: ENV['AMAZING_PAYMENT_METHOD_CREDENTIALS'],
server: Rails.env.production? ? 'production' : 'test',
test_mode: !Rails.env.production?
)
end
```

### Core

- Add configuration option for `migration_path` [#4190](https://github.com/solidusio/solidus/pull/4190) ([SuperGoodSoft](https://github.com/supergoodsoft/))
Expand Down
2 changes: 0 additions & 2 deletions core/lib/spree/preferences/static_model_preferences.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ def initialize
end

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ module Spree
expect(definitions).to have_key('my_definition')
end

it "can replace preferences" do
subject.add(preference_class, 'my_definition', { color: "red" })

subject.add(preference_class, 'my_definition', { color: "blue" })

expect(definitions['my_definition'].fetch(:color)).to eq("blue")
end

it "errors assigning invalid preferences" do
expect {
subject.add(preference_class, 'my_definition', { ice_cream: 'chocolate' })
Expand Down