about summary refs log tree commit diff
path: root/app/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'app/controllers')
-rw-r--r--app/controllers/application_controller.rb1
-rw-r--r--app/controllers/concerns/turbo/redirection.rb32
2 files changed, 33 insertions, 0 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 09705d1..c8be53e 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -1,2 +1,3 @@
 class ApplicationController < ActionController::Base
+  include Turbo::Redirection
 end
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
+