module TurboGraft::XHRHeaders
Intercepts calls to _compute_redirect_to_location
(used by redirect_to) for two purposes.
-
Corrects the behavior of redirect_to with the :back option by using the X-XHR-Referer
request header instead of the standard Referer request header.
-
Stores the return value (the redirect target url) to persist through to the redirect
request, where it will be used to set the X-XHR-Redirected-To response header. The Turbolinks script will detect the header and use replaceState to reflect the redirected url.
Public Instance Methods
_compute_redirect_to_location(*args)
click to toggle source
Calls superclass method
# File lib/turbograft/xhr_headers.rb, line 15 def _compute_redirect_to_location(*args) options, request = _normalize_redirect_params(args) store_for_turbolinks begin if options == :back && request.headers["X-XHR-Referer"] super(*[(request if args.length == 2), request.headers["X-XHR-Referer"]].compact) else super(*args) end end end
Private Instance Methods
_normalize_redirect_params(args)
click to toggle source
Ensure backwards compatibility Rails < 4.2: _compute_redirect_to_location
(options) Rails >= 4.2: _compute_redirect_to_location
(request, options)
# File lib/turbograft/xhr_headers.rb, line 32 def _normalize_redirect_params(args) options, req = args.reverse [options, req || request] end
request_matches_redirect?()
click to toggle source
# File lib/turbograft/xhr_headers.rb, line 52 def request_matches_redirect? session[:_turbolinks_redirect_to] == request.original_url end
set_xhr_redirected_to()
click to toggle source
# File lib/turbograft/xhr_headers.rb, line 42 def set_xhr_redirected_to if turbolinks_redirected? && request_matches_redirect? response.headers['X-XHR-Redirected-To'] = session.delete(:_turbolinks_redirect_to) end end
store_for_turbolinks(url)
click to toggle source
# File lib/turbograft/xhr_headers.rb, line 37 def store_for_turbolinks(url) session[:_turbolinks_redirect_to] = url if session && request.headers["X-XHR-Referer"] url end
turbolinks_redirected?()
click to toggle source
# File lib/turbograft/xhr_headers.rb, line 48 def turbolinks_redirected? session && session[:_turbolinks_redirect_to] end