Skip to content

Commit

Permalink
Adding new path_without_query_params method. Fixes #1571
Browse files Browse the repository at this point in the history
  • Loading branch information
jwoertink committed Sep 2, 2021
1 parent 1f28e05 commit 4592817
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
22 changes: 22 additions & 0 deletions spec/lucky/action_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,28 @@ describe Lucky::Action do
end
end

describe ".path_without_query_params" do
it "returns path without declared non-nil query params" do
Lucky::RouteHelper.temp_config(base_uri: "example.com") do
RequiredParams::Index.path_without_query_params.should eq "/required_params"
end
end

it "returns path with (required) path params" do
Lucky::RouteHelper.temp_config(base_uri: "example.com") do
Tests::Edit.path_without_query_params(1).should eq "/tests/1/edit"
end
end

it "returns path with optional path params" do
Lucky::RouteHelper.temp_config(base_uri: "example.com") do
OptionalRouteParams::Index.path_without_query_params(1).should eq "/complex_posts/1"
OptionalRouteParams::Index.path_without_query_params(1, 2).should eq "/complex_posts/1/2"
OptionalRouteParams::Index.path_without_query_params(1, 2, 3).should eq "/complex_posts/1/2/3"
end
end
end

describe "routing" do
it "creates URL helpers for the resourceful actions" do
Tests::Index.path.should eq "/tests"
Expand Down
19 changes: 19 additions & 0 deletions src/lucky/routable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,25 @@ module Lucky::Routable
Lucky::RouteHelper.new({{ method }}, path).url
end

def self.path_without_query_params(
{% for param in path_params %}
{{ param.gsub(/:/, "").id }},
{% end %}
{% for param in optional_path_params %}
{{ param.gsub(/^\?:/, "").id }} = nil,
{% end %}
)
path = path_from_parts(
{% for param in path_params %}
{{ param.gsub(/:/, "").id }},
{% end %}
{% for param in optional_path_params %}
{{ param.gsub(/^\?:/, "").id }},
{% end %}
)
Lucky::RouteHelper.new({{ method }}, path).path
end

{% params_with_defaults = PARAM_DECLARATIONS.select do |decl|
!decl.value.is_a?(Nop) || decl.type.is_a?(Union) && decl.type.types.last.id == Nil.id
end %}
Expand Down

0 comments on commit 4592817

Please sign in to comment.