-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
redirectable_turbolinks_support.cr
51 lines (46 loc) · 1.93 KB
/
redirectable_turbolinks_support.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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
# Overrides Lucky::Redirectable redirect's method
def redirect(
to path : String,
status : Int32 = Lucky::Redirectable.settings.redirect_status
) : 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
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