@@ -20,22 +20,41 @@ class Engine < Rails::Engine
2020 config . to_prepare do
2121 auth = Spree ::Auth ::Engine
2222
23- auth . prepare_backend if SolidusSupport . backend_available?
24- auth . prepare_frontend if SolidusSupport . frontend_available?
23+ if SolidusSupport . backend_available?
24+ auth . load_decorators_for ( 'backend' )
25+ auth . prepare_backend
26+ end
27+
28+ if SolidusSupport . frontend_available?
29+ auth . load_decorators_for ( 'frontend' )
30+ auth . prepare_frontend
31+ end
2532
26- ApplicationController . send : include, Spree ::AuthenticationHelpers
33+ ApplicationController . include Spree ::AuthenticationHelpers
2734 end
2835
29- def self . prepare_backend
30- Rails . application . config . assets . precompile += %w[
31- lib/assets/javascripts/spree/backend/solidus_auth.js
32- lib/assets/javascripts/spree/backend/solidus_auth.css
33- ]
36+ def self . load_decorators_for ( component_name )
37+ base_path = root . join ( 'lib/decorators' , component_name )
38+
39+ if Rails . respond_to? ( :autoloaders ) && Rails . autoloaders . main
40+ # Add decorators folder to the Rails autoloader. This
41+ # allows Zeitwerk to resolve decorators paths correctly,
42+ # when used.
43+ base_path . glob ( '*' ) do |decorators_folder |
44+ Rails . autoloaders . main . push_dir ( decorators_folder )
45+ end
46+ end
3447
35- Dir . glob ( File . join ( File . dirname ( __FILE__ ) , "../../controllers/backend/*/*/*_decorator*.rb" ) ) do |c |
36- Rails . configuration . cache_classes ? require ( c ) : load ( c )
48+ # Load decorator files. This is needed since they are
49+ # never explicitely referenced in the application code
50+ # and won't be loaded by default. We need them to be
51+ # executed anyway to extend exisiting classes.
52+ base_path . glob ( '**/*_decorator*.rb' ) do |decorator_path |
53+ require_dependency ( decorator_path )
3754 end
55+ end
3856
57+ def self . prepare_backend
3958 Spree ::Admin ::BaseController . unauthorized_redirect = -> do
4059 if try_spree_current_user
4160 flash [ :error ] = I18n . t ( 'spree.authorization_failure' )
@@ -48,15 +67,6 @@ def self.prepare_backend
4867 end
4968
5069 def self . prepare_frontend
51- Rails . application . config . assets . precompile += %w[
52- lib/assets/javascripts/spree/frontend/solidus_auth.js
53- lib/assets/javascripts/spree/frontend/solidus_auth.css
54- ]
55-
56- Dir . glob ( File . join ( File . dirname ( __FILE__ ) , "../../controllers/frontend/*/*_decorator*.rb" ) ) do |c |
57- Rails . configuration . cache_classes ? require ( c ) : load ( c )
58- end
59-
6070 Spree ::BaseController . unauthorized_redirect = -> do
6171 if try_spree_current_user
6272 flash [ :error ] = I18n . t ( 'spree.authorization_failure' )
0 commit comments