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

Adding new path_without_query_params method #1572

Merged
merged 1 commit into from
Sep 2, 2021
Merged
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
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