From e4675a94e2fbf5356d57df0a314b3ae5a353f846 Mon Sep 17 00:00:00 2001 From: George Mendoza Date: Wed, 1 Jun 2022 15:42:35 +0800 Subject: [PATCH] Add extension paths before `initialize_cache` solidus_support v0.9.0, released on May 26, 2022, included a commit that fixes the load order of adding extension paths. See https://github.com/solidusio/solidus_support/commit/202e77c2d7887ee1edb7bf352b5a942e71b5176c. The commit depends on the `initialize_dependency_mechanism` Rails initializer (https://github.com/rails/rails/blob/127dd06df66552dd272eea7832f8bb205cf6fd01/railties/lib/rails/application/bootstrap.rb#L68). However, that initiliazer was removed in Rails 7.0.3 (https://github.com/rails/rails/commit/0375657ce0fc16bda1d61e2fbf971743b4463c42). The extension paths initializer has been updated to run before `initialize_cache`. `initialize_cache` runs 1) after the Solidus engines have already loaded BUT 2) before Rails has added the paths to `$LOAD_PATH`. Normally, it would be sufficient to run the initializer before the `set_load_path` initializer. However, external gems such as Deface may also change the load paths before `set_load_path`. To ensure that our extension paths are not affected by those gems, we work around those gems by adding our paths before `initialize_cache`, which is the Rails initializer called before `set_load_path`. References ---------- * `bin/rails initializers` - Shows list of active initializers * List of Rails initializers: https://guides.rubyonrails.org/configuring.html#initializers * Engine paths configuration - See Paths section in https://api.rubyonrails.org/v7.0.3/classes/Rails/Engine.html. --- lib/solidus_support/engine_extensions.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/solidus_support/engine_extensions.rb b/lib/solidus_support/engine_extensions.rb index 9f9aa13..e24d679 100644 --- a/lib/solidus_support/engine_extensions.rb +++ b/lib/solidus_support/engine_extensions.rb @@ -82,7 +82,24 @@ def solidus_subscribers_root # # @see #load_solidus_decorators_from def enable_solidus_engine_support(engine) - initializer "#{name}_#{engine}_paths", before: :initialize_dependency_mechanism do + # In the past, view and controller paths of Solidus extensions were + # added at extension loading time. As a result, if an extension + # customizing a Solidus engine is loaded before that engine + # (for example, the extension is declared before the `solidus_frontend` + # engine in the `Gemfile`), then the extension's paths for that Solidus + # engine wouldn't be loaded. + + # The initializer below runs before `initialize_cache` because + # `initialize_cache` runs 1) after the Solidus engines have already + # loaded BUT 2) before Rails has added the paths to `$LOAD_PATH`. + # Normally, it would be sufficient to run the initializer below before + # the `set_load_path` initializer. However, external gems such as + # Deface may also change the load paths immediately before + # `set_load_path`. To ensure that our extension paths are not affected + # by those gems, we work around those gems by adding our paths before + # `initialize_cache`, which is the Rails initializer called before + # `set_load_path`. + initializer "#{name}_#{engine}_paths", before: :initialize_cache do if SolidusSupport.send(:"#{engine}_available?") paths['app/controllers'] << "lib/controllers/#{engine}" paths['app/views'] << "lib/views/#{engine}"