diff options
author | Ben Harris <ben@tilde.team> | 2021-09-14 23:34:39 -0400 |
---|---|---|
committer | Ben Harris <ben@tilde.team> | 2021-09-14 23:34:39 -0400 |
commit | f9ea5e999a298f658ef7fb2abc7c13ba3ca412cb (patch) | |
tree | 091233c6f148f976433e0193868d3a1f1fa2f06e /app/controllers/concerns/turbo/redirection.rb | |
parent | d32e71077cb5a874c69edfc8db77b272a1388623 (diff) |
fix turbo redirects
Diffstat (limited to 'app/controllers/concerns/turbo/redirection.rb')
-rw-r--r-- | app/controllers/concerns/turbo/redirection.rb | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/app/controllers/concerns/turbo/redirection.rb b/app/controllers/concerns/turbo/redirection.rb new file mode 100644 index 0000000..cf7cc55 --- /dev/null +++ b/app/controllers/concerns/turbo/redirection.rb @@ -0,0 +1,32 @@ +module Turbo + module Redirection + extend ActiveSupport::Concern + + def redirect_to(url = {}, options = {}) + turbo = options.delete(:turbo) + + super.tap do + if turbo != false && request.xhr? && !request.get? + visit_location_with_turbo(location, turbo) + end + end + end + + private + def visit_location_with_turbo(location, action) + visit_options = { + action: action.to_s == "advance" ? action : "replace" + } + + script = [] + script << "Turbo.clearCache()" + script << "Turbo.visit(#{location.to_json}, #{visit_options.to_json})" + + self.status = 200 + self.response_body = script.join("\n") + response.content_type = "text/javascript" + response.headers["X-Xhr-Redirect"] = location + end + end +end + |