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

Added more type options for status in the html_with_status macro #1568

Merged

Conversation

bnjamin
Copy link
Contributor

@bnjamin bnjamin commented Aug 13, 2021

The html_with_status macro now also accepts Symbols and HTTP::Status as the status argument.

Purpose

Fixes #1558

Description

I also added the option to use symbols, no worries they are typesafe, so you will get a compile time error in case of typos.
I think it reads better with a symbol, maybe i'm weird like that 🤷

html_with_status EditPage, :unprocessable_entity, operation: operation, post: post

Checklist

  • - An issue already exists detailing the issue/or feature request that this PR fixes
  • - All specs are formatted with crystal tool format spec src
  • - Inline documentation has been added and/or updated
  • - Lucky builds on docker with ./script/setup
  • - All builds and specs pass on docker with ./script/test

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.resolve.is_a?(NumberLiteral) %}
Copy link
Contributor Author

@bnjamin bnjamin Aug 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for the HTTP::Status enum case. When in "macro land" status is a Crystal::Macros::Path when an HTTP::Status enum is passed. At first i tried status.is_a?(HTTP::Status) however that would return false.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, interesting. So the Crystal::Macros::Path#resolve will turn that enum in to an Integer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right, so it will actually work with all Enums with the current implementation, maybe we should add a check to see if the Path starts with HTTP::Status

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That might not be a bad idea. You wouldn't want to accidentally send out a status of 4 😂 Then again.. maybe you do? 🤔 The whole reason for this method was more of an escape hatch since for the most part, you'd want to just use the html macro...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the check, if you really wanted to sent out a status of 4 you could just pass a Number. Here be dragons 🐉.
Maybe it won't be an uncommon escsape hatch for long, the Turbo library from Hotwired expects a 422 unprocessable entity when a form fails with validation errors.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet. Sounds good. Thanks!

Copy link
Member

@jwoertink jwoertink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet! Thanks for this. I dig it 👍

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.resolve.is_a?(NumberLiteral) %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, interesting. So the Crystal::Macros::Path#resolve will turn that enum in to an Integer?

{% 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 }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I didn't know you could call upcase right on the symol at the macro level like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was also a first for me

@jwoertink jwoertink merged commit cb341cc into luckyframework:master Aug 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can we use an HTTP::Status enum in this method?
2 participants