Skip to content

Commit 8b087a7

Browse files
committed
Remove duplication loading subcomponents decorators
1 parent 9eef0a7 commit 8b087a7

1 file changed

Lines changed: 15 additions & 25 deletions

File tree

lib/spree/auth/engine.rb

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,27 @@ 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

2633
ApplicationController.include Spree::AuthenticationHelpers
2734
end
2835

29-
def self.prepare_backend
30-
base_path = root.join('lib/decorators/backend')
36+
def self.load_decorators_for(component_name)
37+
base_path = root.join('lib/decorators', component_name)
3138

3239
if Rails.respond_to?(:autoloaders) && Rails.autoloaders.main
3340
# Add decorators folder to the Rails autoloader. This
3441
# allows Zeitwerk to resolve decorators paths correctly,
3542
# when used.
36-
Dir.glob(base_path.join('*')) do |decorators_folder|
43+
base_path.glob('*') do |decorators_folder|
3744
Rails.autoloaders.main.push_dir(decorators_folder)
3845
end
3946
end
@@ -42,10 +49,12 @@ def self.prepare_backend
4249
# never explicitely referenced in the application code
4350
# and won't be loaded by default. We need them to be
4451
# executed anyway to extend exisiting classes.
45-
Dir.glob(base_path.join('**/*_decorator*.rb')) do |decorator_path|
52+
base_path.glob('**/*_decorator*.rb') do |decorator_path|
4653
require_dependency(decorator_path)
4754
end
55+
end
4856

57+
def self.prepare_backend
4958
Spree::Admin::BaseController.unauthorized_redirect = -> do
5059
if try_spree_current_user
5160
flash[:error] = I18n.t('spree.authorization_failure')
@@ -58,25 +67,6 @@ def self.prepare_backend
5867
end
5968

6069
def self.prepare_frontend
61-
base_path = root.join('lib/decorators/frontend')
62-
63-
if Rails.respond_to?(:autoloaders) && Rails.autoloaders.main
64-
# Add decorators folder to the Rails autoloader. This
65-
# allows Zeitwerk to resolve decorators paths correctly,
66-
# when used.
67-
Dir.glob(base_path.join('*')) do |decorators_folder|
68-
Rails.autoloaders.main.push_dir(decorators_folder)
69-
end
70-
end
71-
72-
# Load decorator files. This is needed since they are
73-
# never explicitely referenced in the application code
74-
# and won't be loaded by default. We need them to be
75-
# executed anyway to extend exisiting classes.
76-
Dir.glob(base_path.join('**/*_decorator*.rb')) do |decorator_path|
77-
require_dependency(decorator_path)
78-
end
79-
8070
Spree::BaseController.unauthorized_redirect = -> do
8171
if try_spree_current_user
8272
flash[:error] = I18n.t('spree.authorization_failure')

0 commit comments

Comments
 (0)