From 336a0c1210fd2b62ff282d6292ad253c03f52a60 Mon Sep 17 00:00:00 2001 From: Elia Schito Date: Wed, 11 Oct 2023 12:47:08 +0200 Subject: [PATCH] Don't capture NameError if its not a missing component Instead of rescuing NameError we proactively check if the requested component is available. This is made possible by Zeitwerk using `autoload` under the hood and having all loadable constants respond `true` when asked if they are `const_defined?`. --- admin/lib/solidus_admin/configuration.rb | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/admin/lib/solidus_admin/configuration.rb b/admin/lib/solidus_admin/configuration.rb index cdb8fc9ad4b..e2973ef9bfb 100644 --- a/admin/lib/solidus_admin/configuration.rb +++ b/admin/lib/solidus_admin/configuration.rb @@ -188,14 +188,22 @@ def menu_items def components @components ||= Hash.new do |_h, k| - "solidus_admin/#{k}/component".classify.constantize - rescue NameError - prefix = "#{ENGINE_ROOT}/app/components/solidus_admin/" - suffix = "/component.rb" - dictionary = Dir["#{prefix}**#{suffix}"].map { _1.delete_prefix(prefix).delete_suffix(suffix) } - corrections = DidYouMean::SpellChecker.new(dictionary: dictionary).correct(k.to_s) - - raise ComponentNotFoundError, "Unknown component #{k}#{DidYouMean.formatter.message_for(corrections)}" + const_name = "solidus_admin/#{k}/component".classify + + unless Object.const_defined?(const_name) + prefix = "#{ENGINE_ROOT}/app/components/solidus_admin/" + suffix = "/component.rb" + dictionary = Dir["#{prefix}**#{suffix}"].map { _1.delete_prefix(prefix).delete_suffix(suffix) } + corrections = DidYouMean::SpellChecker.new(dictionary: dictionary).correct(k.to_s) + + raise ComponentNotFoundError.new( + "Unknown component #{k}#{DidYouMean.formatter.message_for(corrections)}", + k.classify, + receiver: ::SolidusAdmin + ) + end + + const_name.constantize end end