From 146d55d1161f59a6822a778514468e4d48d65462 Mon Sep 17 00:00:00 2001 From: stephann <3025661+stephannv@users.noreply.github.com> Date: Sun, 29 Sep 2024 10:53:08 -0300 Subject: [PATCH] feat: Improve undefined method msg for style builder (#6) --- lib/phlex/variants.rb | 5 +++++ spec/phlex/variants_spec.rb | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/phlex/variants.rb b/lib/phlex/variants.rb index b5c2f5b..8169b71 100644 --- a/lib/phlex/variants.rb +++ b/lib/phlex/variants.rb @@ -82,6 +82,11 @@ def variants(&) def defaults(**variants) view_class::STYLE_DEFAULTS.merge!(variants) end + + def method_missing(method, *args, &) # standard:disable Style/MissingRespondToMissing + message = "undefined method '#{method}' for an instance of Phlex::Variants::StyleBuilder. The available methods are: 'base', 'variants' and 'defaults'" + raise NoMethodError, message + end end # @api private diff --git a/spec/phlex/variants_spec.rb b/spec/phlex/variants_spec.rb index c7f1317..67f57cf 100644 --- a/spec/phlex/variants_spec.rb +++ b/spec/phlex/variants_spec.rb @@ -245,6 +245,20 @@ def view_template expect(actual_classes).to eq expected_classes end + it "explains propery when calling undefined method from StyleBuilder" do + msg = "undefined method 'color' for an instance of Phlex::Variants::StyleBuilder. The available methods are: 'base', 'variants' and 'defaults'" + + expect do + phlex_class do + style do + color do + red "bg-red-200 hover:bg-red-400" + end + end + end + end.to raise_error(NoMethodError, msg) + end + def phlex_class(&) Class.new(Phlex::HTML) do include Phlex::Variants