Skip to content

Commit

Permalink
Added more type options for status in the html_with_status macro (#1568)
Browse files Browse the repository at this point in the history
* The `html_with_status` macro now also accepts Symbols and `HTTP::Status` as the status argument.

* Ensure that status is a HTTP::Status in case status is an Enum
  • Loading branch information
bnjamin authored Aug 14, 2021
1 parent f1c83e1 commit cb341cc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
22 changes: 20 additions & 2 deletions spec/lucky/action_rendering_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,24 @@ class Rendering::Index < TestAction
end
end

class Rendering::Show < TestAction
class Rendering::Show::WithStatus < TestAction
get "/rendering/:nothing" do
html_with_status IndexPage, 419, title: "Closing Time", arg2: "You don't have to go home but you can't stay here"
end
end

class Rendering::Show::WithSymbolStatus < TestAction
get "/rendering1/:nothing" do
html_with_status IndexPage, :unauthorized, title: "Closing Time", arg2: "You don't have to go home but you can't stay here"
end
end

class Rendering::Show::WithEnumStatus < TestAction
get "/rendering2/:nothing" do
html_with_status IndexPage, HTTP::Status::UNPROCESSABLE_ENTITY, title: "Closing Time", arg2: "You don't have to go home but you can't stay here"
end
end

class Namespaced::Rendering::Index < TestAction
get "/namespaced/rendering" do
html ::Rendering::IndexPage, title: "Anything", arg2: "testing multiple args"
Expand Down Expand Up @@ -189,11 +201,17 @@ describe Lucky::Action do
end

it "renders with a different status code" do
response = Rendering::Show.new(build_context, params).call
response = Rendering::Show::WithStatus.new(build_context, params).call

response.body.to_s.should contain "Closing Time"
response.debug_message.to_s.should contain "Rendering::IndexPage"
response.status.should eq 419

status = Rendering::Show::WithSymbolStatus.new(build_context, params).call.status
status.should eq 401

status = Rendering::Show::WithEnumStatus.new(build_context, params).call.status
status.should eq 422
end
end

Expand Down
20 changes: 17 additions & 3 deletions src/lucky/renderable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,43 @@ module Lucky::Renderable

# Render an HTMLPage with a status other than 200
#
# The status can either be a Number, a HTTP::Status, or a Symbol that corresponds to the HTTP::Status.
#
# ```
# class SecretAgents::Index < BrowserAction
# get "/shhhh" do
# html_with_status IndexPage, 472, message: "This page can only be seen with special goggles"
# end
# end
# ```
# See Crystal's
# [HTTP::Status](https://crystal-lang.org/api/latest/HTTP/Status.html)
# enum for more available http status codes.
macro html_with_status(page_class, status, **assigns)
{% if !status.is_a?(NumberLiteral) %}
{% if status.is_a?(NumberLiteral) %}
html {{ page_class }}, _with_status_code: {{ status }}, {{ **assigns }}
{% elsif status.is_a?(SymbolLiteral) %}
html {{ page_class }}, _with_status_code: HTTP::Status::{{ status.upcase.id }}.value, {{ **assigns }}
{% elsif status.is_a?(Path) && status.names.join("::").starts_with?("HTTP::Status::") %}
html {{ page_class }}, _with_status_code: {{ status.resolve }}, {{ **assigns }}
{% else %}
{% raise <<-ERROR
#{@type.name} called `html_with_status #{page_class}` with status '#{status}'.
The `status` value should be a Number, or use `html` to render a Page with a 200 status
The `status` value should either be a Number, a HTTP::Status or a Symbol that corresponds to the HTTP::Status.
If you want to render a page with status code 200 you can also use `html`
Try this...
▸ html_with_status #{page_class}, 419, arg1: "...", arg2: "..."
▸ html_with_status #{page_class}, HTTP::Status::FORBIDDEN, arg1: "...", arg2: "..."
▸ html_with_status #{page_class}, :unprocessable_entity, arg1: "...", arg2: "..."
▸ html #{page_class}, arg1: "...", arg2: "..."
ERROR
%}
{% end %}
html {{ page_class }}, _with_status_code: {{ status }}, {{ **assigns }}
end

# :nodoc:
Expand Down

0 comments on commit cb341cc

Please sign in to comment.