-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a generator to create an initializer with new defaults
Following the work done in #4064, this commits introduces a Rails generator that creates a new initializer called, by default, `new_solidus_defaults.rb`. This initializer works in a very similar way that [`new_framework_defaults.rb` does in Rails](https://guides.rubyonrails.org/upgrading_ruby_on_rails.html#configure-framework-defaults). It allows users to preview the defaults that have changed on a new Solidus version, as they are printed one by one on a commented line. Users can then keep enabling them while updating their application code. We're adding the `load_defaults` call with the old version value to the generated initializer. Users can remove the file altogether when they're done with the process. At that point, we require them to add `load_defaults` with the new version to the main initializer file (`spree.rb`). Even if there's no actual need for that, as `loaded_defaults` on the configuration class defaults to the current Solidus version, we want to enforce that so that users are on the safe side for the next version upgrade. If they don't add it, we emit a warning. For now, we're leaving this as a generator, but we could reference it from a rake task, although probably there's no need. Be aware that users need to provide the version from which they are updating. It's an option that offers more flexibility, as users can update from versions different from the latest one. It also plays well with our system's flexibility, for instance, to change defaults between a pre-release and a release. However, we can add some code to default to the latest minor version, but we should keep that information in our code, and that's a small burden for our update process.
- Loading branch information
1 parent
63501e7
commit 7dff340
Showing
22 changed files
with
453 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
core/lib/generators/solidus/update/templates/config/initializers/new_solidus_defaults.rb.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# This initializer lets you preview the defaults that have changed on the new | ||
# Solidus version. | ||
# | ||
# It allows you to enable them one by one while you adapt your application. | ||
# When you're done with all of them, you can safely remove this file and add | ||
# the updated `load_defaults` calls to the top of the config blocks in your | ||
# Solidus main initializer. | ||
|
||
Spree.config do |config| | ||
<%= @core_changes %> | ||
end | ||
|
||
<% if defined?(Spree::Frontend::Engine) -%> | ||
Spree::Frontend::Config.configure do |config| | ||
<%= @frontend_changes %> | ||
end | ||
<% end -%> | ||
|
||
<% if defined?(Spree::Backend::Engine) -%> | ||
Spree::Backend::Config.configure do |config| | ||
<%= @backend_changes %> | ||
end | ||
<% end -%> | ||
|
||
<% if defined?(Spree::Api::Engine) -%> | ||
Spree::Api::Config.configure do |config| | ||
<%= @api_changes %> | ||
end | ||
<% end -%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spree/core/preference_changes_between_solidus_versions' | ||
require 'rails/generators' | ||
|
||
module Solidus | ||
class UpdateGenerator < ::Rails::Generators::Base | ||
desc 'Generates a new initializer to preview the new defaults for current Solidus version' | ||
|
||
source_root File.expand_path('templates', __dir__) | ||
|
||
class_option :from, | ||
type: :string, | ||
banner: 'The version your are updating from. E.g. 2.11.10' | ||
|
||
class_option :initializer_basename, | ||
type: :string, | ||
default: 'new_solidus_defaults', | ||
banner: 'The name for the new initializer' | ||
|
||
class_option :to, | ||
type: :string, | ||
default: Spree.solidus_version, | ||
hide: true | ||
|
||
class_option :initializer_directory, | ||
type: :string, | ||
default: 'config/initializers/', | ||
hide: true | ||
|
||
def create_new_defaults_initializer | ||
from = options[:from] | ||
to = options[:to] | ||
@from = from | ||
@core_changes = core_changes_template(from, to) | ||
@frontend_changes = frontend_changes_template(from, to) | ||
@backend_changes = backend_changes_template(from, to) | ||
@api_changes = api_changes_template(from, to) | ||
|
||
template 'config/initializers/new_solidus_defaults.rb.tt', | ||
File.join(options[:initializer_directory], "#{options[:initializer_basename]}.rb") | ||
end | ||
|
||
private | ||
|
||
def core_changes_template(from, to) | ||
changes_template_for(Spree::AppConfiguration, from, to) | ||
end | ||
|
||
def frontend_changes_template(from, to) | ||
return '' unless defined?(Spree::Frontend::Engine) | ||
|
||
changes_template_for(Spree::FrontendConfiguration, from, to) | ||
end | ||
|
||
def backend_changes_template(from, to) | ||
return '' unless defined?(Spree::Backend::Engine) | ||
|
||
changes_template_for(Spree::BackendConfiguration, from, to) | ||
end | ||
|
||
def api_changes_template(from, to) | ||
return '' unless defined?(Spree::Api::Engine) | ||
|
||
changes_template_for(Spree::ApiConfiguration, from, to) | ||
end | ||
|
||
def changes_template_for(klass, from, to) | ||
changes = Spree::Core::PreferenceChangesBetweenSolidusVersions.new(klass).call(from: from, to: to) | ||
return '# No changes' if changes.empty? | ||
|
||
[ | ||
["config.load_defaults('#{from}')"] + | ||
changes.map do |pref_key, change| | ||
" # config.#{pref_key} = #{change[:to]}" | ||
end.flatten | ||
].join("\n") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
core/lib/spree/core/preference_changes_between_solidus_versions.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
module Core | ||
class PreferenceChangesBetweenSolidusVersions | ||
attr_reader :config_class | ||
|
||
def initialize(config_class) | ||
@config_class = config_class | ||
end | ||
|
||
def call(from:, to:) | ||
preferences_from = config_class.new.load_defaults(from) | ||
preferences_to = config_class.new.load_defaults(to) | ||
preferences_from.reduce({}) do |changes, (pref_key, value_from)| | ||
value_to = preferences_to[pref_key] | ||
if value_from == value_to | ||
changes | ||
else | ||
changes.merge( | ||
pref_key => { from: value_from, to: value_to } | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,9 +34,10 @@ module ApplicationHelper | |
|
||
# @private | ||
module DummyApp | ||
def self.setup(gem_root:, lib_name:, auto_migrate: true) | ||
def self.setup(gem_root:, lib_name:, auto_migrate: true, &block) | ||
ENV["LIB_NAME"] = lib_name | ||
DummyApp::Application.config.root = File.join(gem_root, 'spec', 'dummy') | ||
block.call if block_given? | ||
|
||
DummyApp::Application.initialize! | ||
|
||
|
@@ -127,6 +128,7 @@ class Application < ::Rails::Application | |
|
||
Spree.user_class = 'Spree::LegacyUser' | ||
Spree.config do |config| | ||
config.load_defaults(Spree.solidus_version) | ||
config.mails_from = "[email protected]" | ||
|
||
if ENV['DISABLE_ACTIVE_STORAGE'] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.