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

WIP : Replacing Turbolinks with Turbo #1648

Closed
wants to merge 3 commits into from
Closed
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
22 changes: 11 additions & 11 deletions spec/lucky/action_redirect_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe Lucky::Action do
end
end

it "turbolinks redirects after a XHR POST form submission" do
it "turbo redirects after a XHR POST form submission" do
request = build_request("POST")
request.headers["Accept"] = "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01"
request.headers["X-Requested-With"] = "XmlHttpRequest"
Expand All @@ -107,32 +107,32 @@ describe Lucky::Action do
action = RedirectAction.new(context, params)
response = action.redirect to: "/somewhere", status: 302
should_redirect(action, to: "/somewhere", status: 200)
action.context.response.headers.has_key?("Turbolinks-Location").should be_false
response.body.should eq %[Turbolinks.clearCache();\nTurbolinks.visit("/somewhere", {"action": "replace"})]
action.context.response.headers.has_key?("Turbo-Location").should be_false
response.body.should eq %[Turbo.clearCache();\nTurbo.visit("/somewhere", {"action": "replace"})]
end

it "set a cookie for redirects occurring during a turbolinks GET request" do
it "set a cookie for redirects occurring during a turbo GET request" do
request = build_request
request.headers["Turbolinks-Referrer"] = "/"
request.headers["Turbo-Referrer"] = "/"
context = build_context("/", request: request)

action = RedirectAction.new(context, params)
response = action.redirect to: "/somewhere", status: 302
should_redirect(action, to: "/somewhere", status: 302)
context.response.headers.has_key?("Turbolinks-Location").should be_false
context.response.headers.has_key?("Turbo-Location").should be_false
response.body.should eq ""
# should remember redirect to
context.cookies.get?(:_turbolinks_location).should eq "/somewhere"
context.cookies.get?(:_turbo_location).should eq "/somewhere"
end

it "restore turbolinks redirect target" do
it "restore turbo redirect target" do
context = build_context
context.cookies.set(:_turbolinks_location, "/somewhere")
context.cookies.set(:_turbo_location, "/somewhere")

RedirectAction.new(context, params).call
context.response.status_code.should eq 200
context.response.headers["Turbolinks-Location"].should eq "/somewhere"
context.cookies.deleted?(:_turbolinks_location).should be_true
context.response.headers["Turbo-Location"].should eq "/somewhere"
context.cookies.deleted?(:_turbo_location).should be_true
end

it "keeps flash messages for the next action" do
Expand Down
2 changes: 1 addition & 1 deletion src/lucky/action.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ abstract class Lucky::Action
include Lucky::ParamHelpers
include Lucky::ActionPipes
include Lucky::Redirectable
include Lucky::RedirectableTurbolinksSupport
include Lucky::RedirectableTurboSupport
include Lucky::VerifyAcceptsFormat
end
12 changes: 6 additions & 6 deletions src/lucky/redirectable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ module Lucky::Redirectable

Lucky::TextResponse.new(context,
"text/javascript",
%[Turbolinks.clearCache();\nTurbolinks.visit(#{path.to_json}, {"action": "replace"})],
%[Turbo.clearCache();\nTurbo.visit(#{path.to_json}, {"action": "replace"})],
status: 200)
else
if request.headers["Turbolinks-Referrer"]?
store_turbolinks_location_in_session(path)
if request.headers["Turbo-Referrer"]?
store_turbo_location_in_session(path)
end
# ordinary redirect
context.response.headers.add "Location", path
Expand All @@ -149,9 +149,9 @@ module Lucky::Redirectable
{% 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
private def store_turbo_location_in_session(path : String)
cookies.set(:_turbo_location, path).http_only(true)
# this cookie read at Lucky::RedirectableTurboSupport
end

private def allowed_host?(referer : String)
Expand Down
19 changes: 19 additions & 0 deletions src/lucky/redirectable_turbo_support.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Set "Turbo-Location" from session
# Needs to change browser address bar at last request
#
# This pipe extracted Lucky::Redirectable, because Lucky::Redirectable included to Lucky::ErrorAction
# but Lucky::ErrorAction not have pipe support
module Lucky::RedirectableTurboSupport
macro included
before set_turbo_location_header_from_session
end

private def set_turbo_location_header_from_session
if turbo_location = cookies.get?(:_turbo_location)
cookies.delete(:_turbo_location)
# change browser address bar at last request
response.headers["Turbo-Location"] = turbo_location
end
continue
end
end
19 changes: 0 additions & 19 deletions src/lucky/redirectable_turbolinks_support.cr

This file was deleted.