Skip to content

Commit

Permalink
feat: simpler renderable html macro (#1836)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdwagner committed Oct 9, 2023
1 parent 2c95e34 commit 10752ad
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions src/lucky/renderable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ module Lucky::Renderable
# end
# ```
macro html(page_class = nil, _with_status_code = 200, **assigns)
{% page_class = page_class || "#{@type.name}Page".id %}
validate_page_class!({{ page_class }})
{% page_class = page_class || parse_type("#{@type.name}Page") %}
{% ancestors = page_class.resolve.ancestors %}
{% if ancestors.includes?(Lucky::Action) %}
{% page_class.raise "You accidentally rendered an action (#{page_class}) instead of an HTMLPage in the #{@type.name} action. Did you mean #{page_class}Page?" %}
{% elsif !ancestors.includes?(Lucky::HTMLPage) %}
{% page_class.raise "Couldn't render #{page_class} in #{@type.name} because it is not an HTMLPage" %}
{% end %}

# Found in {{ @type.name }}
view = {{ page_class }}.new(
Expand Down Expand Up @@ -91,17 +96,6 @@ module Lucky::Renderable
{% end %}
end

# :nodoc:
macro validate_page_class!(page_class)
{% ancestors = page_class.resolve.ancestors %}

{% if ancestors.includes?(Lucky::Action) %}
{% page_class.raise "You accidentally rendered an action (#{page_class}) instead of an HTMLPage in the #{@type.name} action. Did you mean #{page_class}Page?" %}
{% elsif !ancestors.includes?(Lucky::HTMLPage) %}
{% page_class.raise "Couldn't render #{page_class} in #{@type.name} because it is not an HTMLPage" %}
{% end %}
end

# Disable cookies
#
# When `disable_cookies` is used, no `Set-Cookie` header will be written to
Expand Down

0 comments on commit 10752ad

Please sign in to comment.