Skip to content

Commit

Permalink
Add Lucky::Routable.url_without_query_params
Browse files Browse the repository at this point in the history
Closes #407
  • Loading branch information
hibachrach committed Jan 1, 2019
1 parent c326697 commit d8f63d5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
14 changes: 14 additions & 0 deletions spec/lucky/action_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,20 @@ describe Lucky::Action do
end
end

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

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

describe "routing" do
it "creates URL helpers for the resourceful actions" do
Tests::Index.path.should eq "/tests"
Expand Down
52 changes: 38 additions & 14 deletions src/lucky/routeable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ module Lucky::Routeable
route(*args, **named_args).url
end

def self.url_without_query_params(
{% for param in path_params %}
{{ param.gsub(/:/, "").id }},
{% end %}
)
path = path_from_parts(
{% for param in path_params %}
{{ param.gsub(/:/, "").id }},
{% end %}
)
Lucky::RouteHelper.new({{ method }}, path).url
end

def self.route(
{% for param in path_params %}
{{ param.gsub(/:/, "").id }},
Expand All @@ -193,20 +206,11 @@ module Lucky::Routeable
{% end %}
anchor : String? = nil
)
path = String.build do |path|
{% for part in path_parts %}
path << "/"
{% if part.starts_with?(":") %}
path << URI.escape({{ part.gsub(/:/, "").id }}.to_param)
{% else %}
path << URI.escape({{ part }})
{% end %}
path = path_from_parts(
{% for param in path_params %}
{{ param.gsub(/:/, "").id }},
{% end %}
end

is_root_path = path == ""
path = "/" if is_root_path

)
query_params = {} of String => String
{% for param in PARAM_DECLARATIONS %}
{% if param.value == false %}
Expand All @@ -219,7 +223,6 @@ module Lucky::Routeable
query_params["{{ param.var }}"] = {{ param.var }}.to_s
end
{% end %}

unless query_params.empty?
path += "?#{HTTP::Params.encode(query_params)}"
end
Expand All @@ -239,6 +242,27 @@ module Lucky::Routeable
def self.with
\{% raise "Use `route` instead of `with` if the route doesn't need params" %}
end

private def self.path_from_parts(
{% for param in path_params %}
{{ param.gsub(/:/, "").id }},
{% end %}
)
path = String.build do |path|
{% for part in path_parts %}
path << "/"
{% if part.starts_with?(":") %}
path << URI.escape({{ part.gsub(/:/, "").id }}.to_param)
{% else %}
path << URI.escape({{ part }})
{% end %}
{% end %}
end

is_root_path = path == ""
path = "/" if is_root_path
path
end
end

macro included
Expand Down

0 comments on commit d8f63d5

Please sign in to comment.