Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better compile time error for link helper usage and actions that require params #1734

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions spec/lucky/link_helpers_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/lucky/html_builder.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "./no_required_params_action"
require "./tags/**"
require "./page_helpers/**"
require "./mount_component"
Expand Down
5 changes: 5 additions & 0 deletions src/lucky/no_required_params_action.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Lucky::NoRequiredParamsAction
def route : Lucky::RouteHelper
Lucky::RouteHelper.new(method, path_from_parts).url
end
end
4 changes: 4 additions & 0 deletions src/lucky/routable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
34 changes: 31 additions & 3 deletions src/lucky/tags/link_helpers.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down