Skip to content

Commit d20fb15

Browse files
committed
Add support for Zeitwerk
Decorator files are now correctly added to the autoloader and are also loaded on engine boot.
1 parent 281bfe3 commit d20fb15

4 files changed

Lines changed: 34 additions & 4 deletions

File tree

lib/controllers/backend/spree/admin/base_controller_decorator.rb renamed to lib/decorators/backend/controllers/spree/admin/base_controller_decorator.rb

File renamed without changes.

lib/controllers/backend/spree/admin/orders/customer_details_controller_decorator.rb renamed to lib/decorators/backend/controllers/spree/admin/orders/customer_details_controller_decorator.rb

File renamed without changes.

lib/controllers/frontend/spree/checkout_controller_decorator.rb renamed to lib/decorators/frontend/controllers/spree/checkout_controller_decorator.rb

File renamed without changes.

lib/spree/auth/engine.rb

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,23 @@ def self.prepare_backend
3232
lib/assets/javascripts/spree/backend/solidus_auth.css
3333
]
3434

35-
Dir.glob(File.join(File.dirname(__FILE__), "../../controllers/backend/*/*/*_decorator*.rb")) do |c|
36-
Rails.configuration.cache_classes ? require(c) : load(c)
35+
base_path = root.join('lib/decorators/backend')
36+
37+
if Rails.respond_to?(:autoloaders) && Rails.autoloaders.main
38+
# Add decorators folder to the Rails autoloader. This
39+
# allows Zeitwerk to resolve decorators paths correctly,
40+
# when used.
41+
Dir.glob(base_path.join('*')) do |decorators_folder|
42+
Rails.autoloaders.main.push_dir(decorators_folder)
43+
end
44+
end
45+
46+
# Load decorator files. This is needed since they are
47+
# never explicitely referenced in the application code
48+
# and won't be loaded by default. We need them to be
49+
# executed anyway to extend exisiting classes.
50+
Dir.glob(base_path.join('**/*_decorator*.rb')) do |decorator_path|
51+
require_dependency(decorator_path)
3752
end
3853

3954
Spree::Admin::BaseController.unauthorized_redirect = -> do
@@ -53,8 +68,23 @@ def self.prepare_frontend
5368
lib/assets/javascripts/spree/frontend/solidus_auth.css
5469
]
5570

56-
Dir.glob(File.join(File.dirname(__FILE__), "../../controllers/frontend/*/*_decorator*.rb")) do |c|
57-
Rails.configuration.cache_classes ? require(c) : load(c)
71+
base_path = root.join('lib/decorators/frontend')
72+
73+
if Rails.respond_to?(:autoloaders) && Rails.autoloaders.main
74+
# Add decorators folder to the Rails autoloader. This
75+
# allows Zeitwerk to resolve decorators paths correctly,
76+
# when used.
77+
Dir.glob(base_path.join('*')) do |decorators_folder|
78+
Rails.autoloaders.main.push_dir(decorators_folder)
79+
end
80+
end
81+
82+
# Load decorator files. This is needed since they are
83+
# never explicitely referenced in the application code
84+
# and won't be loaded by default. We need them to be
85+
# executed anyway to extend exisiting classes.
86+
Dir.glob(base_path.join('**/*_decorator*.rb')) do |decorator_path|
87+
require_dependency(decorator_path)
5888
end
5989

6090
Spree::BaseController.unauthorized_redirect = -> do

0 commit comments

Comments
 (0)