Skip to content

Commit

Permalink
Inline render_html_page to change compilation error (#1373)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmcgarvey authored Dec 28, 2020
1 parent 0b9be66 commit cff93b3
Showing 1 changed file with 12 additions and 25 deletions.
37 changes: 12 additions & 25 deletions src/lucky/renderable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,9 @@ module Lucky::Renderable
# end
# ```
macro html(page_class = nil, **assigns)
{% page_class = page_class || "#{@type.name}Page".id %}
validate_page_class!({{ page_class }})

render_html_page(
{{ page_class = page_class || "#{@type.name}Page".id }},
{% if assigns.empty? %}
{} of String => String
{% else %}
{{ assigns }}
{% end %}
)
end

# :nodoc:
macro validate_page_class!(page_class)
{% if page_class && page_class.resolve? %}
{% 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 %}
end

# :nodoc:
macro render_html_page(page_class, assigns)
# Found in {{ @type.name }}
view = {{ page_class }}.new(
context: context,
Expand All @@ -90,6 +66,17 @@ module Lucky::Renderable
)
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 cff93b3

Please sign in to comment.