diff --git a/spec/lucky/action_redirect_spec.cr b/spec/lucky/action_redirect_spec.cr index 171aad7c3..2528d602b 100644 --- a/spec/lucky/action_redirect_spec.cr +++ b/spec/lucky/action_redirect_spec.cr @@ -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" @@ -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 diff --git a/src/lucky/action.cr b/src/lucky/action.cr index e655f9c6a..57a05f0f5 100644 --- a/src/lucky/action.cr +++ b/src/lucky/action.cr @@ -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 diff --git a/src/lucky/redirectable.cr b/src/lucky/redirectable.cr index 6d397e604..7acce7ada 100644 --- a/src/lucky/redirectable.cr +++ b/src/lucky/redirectable.cr @@ -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 @@ -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) diff --git a/src/lucky/redirectable_turbo_support.cr b/src/lucky/redirectable_turbo_support.cr new file mode 100644 index 000000000..175885530 --- /dev/null +++ b/src/lucky/redirectable_turbo_support.cr @@ -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 diff --git a/src/lucky/redirectable_turbolinks_support.cr b/src/lucky/redirectable_turbolinks_support.cr deleted file mode 100644 index 38ca22c1f..000000000 --- a/src/lucky/redirectable_turbolinks_support.cr +++ /dev/null @@ -1,19 +0,0 @@ -# Set "Turbolinks-Location" from session -# Needs to change browser address bar at last request, see https://github.com/turbolinks/turbolinks#following-redirects -# -# This pipe extracted Lucky::Redirectable, because Lucky::Redirectable included to Lucky::ErrorAction -# but Lucky::ErrorAction not have pipe support -module Lucky::RedirectableTurbolinksSupport - macro included - before set_turbolinks_location_header_from_session - end - - private def set_turbolinks_location_header_from_session - if turbolinks_location = cookies.get?(:_turbolinks_location) - cookies.delete(:_turbolinks_location) - # change browser address bar at last request, see https://github.com/turbolinks/turbolinks#following-redirects - response.headers["Turbolinks-Location"] = turbolinks_location - end - continue - end -end