From 8ba1a69d95e20683bc1a732b05fd8c8ab3c72a7a Mon Sep 17 00:00:00 2001 From: Joel Drapper Date: Sun, 17 Mar 2024 18:20:02 +0000 Subject: [PATCH] Raise helpful error when calling a missing rails helper Co-Authored-By: Seth Horsley <4959521+iseth@users.noreply.github.com> --- lib/phlex/rails.rb | 1 + lib/phlex/rails/html.rb | 2 ++ lib/phlex/rails/html/method_missing.rb | 9 +++++++++ 3 files changed, 12 insertions(+) create mode 100644 lib/phlex/rails/html/method_missing.rb diff --git a/lib/phlex/rails.rb b/lib/phlex/rails.rb index c62da2f5..ae563ab6 100644 --- a/lib/phlex/rails.rb +++ b/lib/phlex/rails.rb @@ -26,6 +26,7 @@ module Rails HTML.extend Phlex::Rails::HTML::Format HTML.include Phlex::Rails::HTML::Format + HTML.include Phlex::Rails::HTML::MethodMissing Unbuffered.prepend Phlex::Rails::UnbufferedOverrides end diff --git a/lib/phlex/rails/html.rb b/lib/phlex/rails/html.rb index 6868c69a..be6228fc 100644 --- a/lib/phlex/rails/html.rb +++ b/lib/phlex/rails/html.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true module Phlex::Rails::HTML + autoload :MethodMissing, "phlex/rails/html/method_missing" + module Format def format :html diff --git a/lib/phlex/rails/html/method_missing.rb b/lib/phlex/rails/html/method_missing.rb new file mode 100644 index 00000000..51975d65 --- /dev/null +++ b/lib/phlex/rails/html/method_missing.rb @@ -0,0 +1,9 @@ +module Phlex::Rails::HTML::MethodMissing + def method_missing(name, *args, **kwargs, &block) + return super unless helpers.respond_to?(name) + const_name = name.to_s.gsub("?", "") + module_name = Phlex::Rails::Helpers.constants.find { |mod| mod.to_s.underscore.gsub("domid", "dom_id") == const_name } + return super unless module_name + raise NoMethodError, "Try including `Phlex::Rails::Helpers::#{module_name}` in your class." + end +end