Skip to content

Commit

Permalink
Improve rendering outside request
Browse files Browse the repository at this point in the history
Follow-up to [hotwired#529][]
Closes [hotwired#613][]

Add test coverage to ensure that rendering outside of a request does not
raise `nil`-related `NoMethodError`.

Alongside a change to replace the `#turbo_frame_request_id` method's `&`
operator with a conditional, this commit also makes a similar change to
the `#turbo_native_app?` method, since it accesses the user agent in a
similar way.

[hotwired#529]: hotwired#529
[hotwired#613]: hotwired#613
  • Loading branch information
seanpdoyle committed Apr 15, 2024
1 parent 102a491 commit 3856b6d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
4 changes: 3 additions & 1 deletion app/controllers/turbo/frames/frame_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion app/controllers/turbo/native/navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions test/frames/frame_request_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3856b6d

Please sign in to comment.