Skip to content

Commit

Permalink
Don't capture NameError if its not a missing component
Browse files Browse the repository at this point in the history
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?`.
  • Loading branch information
elia committed Oct 12, 2023
1 parent afc630e commit 336a0c1
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions admin/lib/solidus_admin/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 336a0c1

Please sign in to comment.