diff --git a/app/controllers/turbo/frames/frame_request.rb b/app/controllers/turbo/frames/frame_request.rb index 9e52f91e..000bcb66 100644 --- a/app/controllers/turbo/frames/frame_request.rb +++ b/app/controllers/turbo/frames/frame_request.rb @@ -33,6 +33,8 @@ def turbo_frame_request? end def turbo_frame_request_id - request&.headers["Turbo-Frame"] + if request + request.headers["Turbo-Frame"] + end end end diff --git a/app/controllers/turbo/native/navigation.rb b/app/controllers/turbo/native/navigation.rb index 9f1d6b1b..c2742813 100644 --- a/app/controllers/turbo/native/navigation.rb +++ b/app/controllers/turbo/native/navigation.rb @@ -12,7 +12,9 @@ module Turbo::Native::Navigation # Turbo Native applications are identified by having the string "Turbo Native" as part of their user agent. def turbo_native_app? - request.user_agent.to_s.match?(/Turbo Native/) + if request + request.user_agent.to_s.match?(/Turbo Native/) + end end # Tell the Turbo Native app to dismiss a modal (if presented) or pop a screen off of the navigation stack. Otherwise redirect to the given URL if Turbo Native is not present. diff --git a/test/frames/frame_request_controller_test.rb b/test/frames/frame_request_controller_test.rb index 71a12928..714fdca1 100644 --- a/test/frames/frame_request_controller_test.rb +++ b/test/frames/frame_request_controller_test.rb @@ -54,3 +54,22 @@ def with_prepended_view_path(path, &block) ApplicationController.view_paths = previous_view_paths end end + +class Turbo::FrameRequestViewTest < ActionView::TestCase + test "supports rendering context without request object" do + controller = ApplicationController.new + controller.request = nil + + @rendered = controller.render_to_string(template: "trays/show") + + assert_dom "html" do |html| + assert_dom html, "head" do |head| + assert_dom head, "title", text: "Dummy" + assert_dom head, "script" + end + assert_dom html, "body:not(.turbo-native)" do |body| + assert_dom body, "turbo-frame#tray" + end + end + end +end