From d49e48ea9e3ba63f084bc0a624a9a744ecd32ba7 Mon Sep 17 00:00:00 2001 From: Matthew McGarvey Date: Tue, 13 Sep 2022 01:00:45 -0500 Subject: [PATCH] Better compile time error for link helper usage --- spec/lucky/link_helpers_spec.cr | 4 +++ src/lucky/html_builder.cr | 1 + src/lucky/no_required_params_action.cr | 5 ++++ src/lucky/routable.cr | 4 +++ src/lucky/tags/link_helpers.cr | 34 +++++++++++++++++++++++--- 5 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 src/lucky/no_required_params_action.cr diff --git a/spec/lucky/link_helpers_spec.cr b/spec/lucky/link_helpers_spec.cr index 6c76423ec..87de6d0ce 100644 --- a/spec/lucky/link_helpers_spec.cr +++ b/spec/lucky/link_helpers_spec.cr @@ -8,6 +8,10 @@ class LinkHelpers::Create < TestAction post "/link_helpers" { plain_text "foo" } end +class LinkHelpers::Show < TestAction + get "/link_helpers/:id" { plain_text "foo" } +end + private class TestPage include Lucky::HTMLPage diff --git a/src/lucky/html_builder.cr b/src/lucky/html_builder.cr index 5e22c1f73..0d75779b9 100644 --- a/src/lucky/html_builder.cr +++ b/src/lucky/html_builder.cr @@ -1,3 +1,4 @@ +require "./no_required_params_action" require "./tags/**" require "./page_helpers/**" require "./mount_component" diff --git a/src/lucky/no_required_params_action.cr b/src/lucky/no_required_params_action.cr new file mode 100644 index 000000000..d2e023269 --- /dev/null +++ b/src/lucky/no_required_params_action.cr @@ -0,0 +1,5 @@ +module Lucky::NoRequiredParamsAction + def route : Lucky::RouteHelper + Lucky::RouteHelper.new(method, path_from_parts).url + end +end diff --git a/src/lucky/routable.cr b/src/lucky/routable.cr index 1fdeab519..f066edabf 100644 --- a/src/lucky/routable.cr +++ b/src/lucky/routable.cr @@ -267,6 +267,10 @@ module Lucky::Routable params_with_defaults.includes? decl end %} + {% if path_params.empty? && params_without_defaults.empty? %} + extend Lucky::NoRequiredParamsAction + {% end %} + def self.route( # required path variables {% for param in path_params %} diff --git a/src/lucky/tags/link_helpers.cr b/src/lucky/tags/link_helpers.cr index 7bdc3e893..9a2c9ea2c 100644 --- a/src/lucky/tags/link_helpers.cr +++ b/src/lucky/tags/link_helpers.cr @@ -34,17 +34,17 @@ module Lucky::LinkHelpers end end - def link(text, to : Lucky::Action.class, attrs : Array(Symbol) = [] of Symbol, **html_options) : Nil + def link(text, to : Lucky::NoRequiredParamsAction, attrs : Array(Symbol) = [] of Symbol, **html_options) : Nil link(**html_options, to: to, attrs: attrs) do text text end end - def link(to : Lucky::Action.class, attrs : Array(Symbol) = [] of Symbol, **html_options) : Nil + def link(to : Lucky::NoRequiredParamsAction, attrs : Array(Symbol) = [] of Symbol, **html_options) : Nil link(**html_options, to: to, attrs: attrs) { } end - def link(to : Lucky::Action.class, attrs : Array(Symbol) = [] of Symbol, **html_options) : Nil + def link(to : Lucky::NoRequiredParamsAction, attrs : Array(Symbol) = [] of Symbol, **html_options) : Nil link(**html_options, to: to.route, attrs: attrs) do yield end @@ -58,6 +58,34 @@ module Lucky::LinkHelpers end end + def link(text, to : Lucky::Action.class, attrs : Array(Symbol) = [] of Symbol, **html_options) : Nil + {% + raise <<-ERROR + Looks like you are trying to pass in an action that needs params. Do better. + + ERROR + %} + end + + def link(to : Lucky::Action.class, attrs : Array(Symbol) = [] of Symbol, **html_options) : Nil + {% + raise <<-ERROR + Looks like you are trying to pass in an action that needs params. Do better. + + ERROR + %} + end + + def link(to : Lucky::Action.class, attrs : Array(Symbol) = [] of Symbol, **html_options) : Nil + {% + raise <<-ERROR + Looks like you are trying to pass in an action that needs params. Do better. + + ERROR + %} + yield + end + def link(text, to : String, attrs : Array(Symbol) = [] of Symbol, **html_options) {% raise <<-ERROR