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

Turbolinks legacy module #1650

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 3 additions & 25 deletions src/lucky/redirectable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -122,38 +122,16 @@ module Lucky::Redirectable
def redirect(to path : String, status : Int32 = 302) : Lucky::TextResponse
# flash messages are not consumed here, so keep them for the next action
flash.keep

if ajax? && request.method != "GET"
context.response.headers.add "Location", path

# do not enable form disabled elements for XHR redirects, see https://github.com/rails/rails/pull/31441
context.response.headers.add "X-Xhr-Redirect", path

Lucky::TextResponse.new(context,
"text/javascript",
%[Turbolinks.clearCache();\nTurbolinks.visit(#{path.to_json}, {"action": "replace"})],
status: 200)
else
if request.headers["Turbolinks-Referrer"]?
store_turbolinks_location_in_session(path)
end
# ordinary redirect
context.response.headers.add "Location", path
context.response.status_code = status
Lucky::TextResponse.new(context, "", "")
end
context.response.headers.add "Location", path
context.response.status_code = status
Lucky::TextResponse.new(context, "", "")
end

# :nodoc:
def redirect(to page_instead_of_action : Lucky::HTMLPage.class, **unused_args)
{% raise "You accidentally redirected to a Lucky::HTMLPage instead of a Lucky::Action" %}
end

private def store_turbolinks_location_in_session(path : String)
cookies.set(:_turbolinks_location, path).http_only(true)
# this cookie read at Lucky::RedirectableTurbolinksSupport
end

private def allowed_host?(referer : String)
if referer_host = URI.parse(referer).hostname
referer_host == request.hostname
Expand Down
29 changes: 29 additions & 0 deletions src/lucky/redirectable_turbolinks_support.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,35 @@
# This pipe extracted Lucky::Redirectable, because Lucky::Redirectable included to Lucky::ErrorAction
# but Lucky::ErrorAction not have pipe support
module Lucky::RedirectableTurbolinksSupport
# Overrides Lucky::Redirectable redirect's method
def redirect(to path : String, status : Int32 = 302) : Lucky::TextResponse
# flash messages are not consumed here, so keep them for the next action
flash.keep
if ajax? && request.method != "GET"
context.response.headers.add "Location", path

# do not enable form disabled elements for XHR redirects, see https://github.com/rails/rails/pull/31441
context.response.headers.add "X-Xhr-Redirect", path

Lucky::TextResponse.new(context,
"text/javascript",
%[Turbolinks.clearCache();\nTurbolinks.visit(#{path.to_json}, {"action": "replace"})],
status: 200)
else
if request.headers["Turbolinks-Referrer"]?
store_turbolinks_location_in_session(path)
end
# ordinary redirect
context.response.headers.add "Location", path
context.response.status_code = status
Lucky::TextResponse.new(context, "", "")
end
end

private def store_turbolinks_location_in_session(path : String)
cookies.set(:_turbolinks_location, path).http_only(true)
end

macro included
before set_turbolinks_location_header_from_session
end
Expand Down