Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Turbo Native helpers to Hotwire Native #691

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions app/controllers/turbo/native/navigation.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
# Turbo is built to work with native navigation principles and present those alongside what's required for the web. When you
# have Turbo Native clients running (see the Turbo iOS and Turbo Android projects for details), you can respond to native
# requests with three dedicated responses: <tt>recede</tt>, <tt>resume</tt>, <tt>refresh</tt>.
# have Hotwire Native clients running (see the Hotwire Native iOS and Hotwire Native Android projects for details),
# you can respond to native requests with three dedicated responses: <tt>recede</tt>, <tt>resume</tt>, <tt>refresh</tt>.
#
# turbo-android handles these actions automatically. You are required to implement the handling on your own for turbo-ios.
# Hotwire Native Android and Hotwire Native iOS handle these actions automatically.
module Turbo::Native::Navigation
extend ActiveSupport::Concern

included do
helper_method :turbo_native_app?
helper_method :hotwire_native_app?, :turbo_native_app?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It breaks the following patch for Devise:

#507 (comment)

Quick fix for anyone stumbling across:

class TurboFailureApp < Devise::FailureApp
  # Compatibility for Turbo::Native::Navigation
  class << self
    def helper_method(*names)
    end
  end

  include Turbo::Native::Navigation

  # Turbo Native requests that require authentication should return 401s to trigger the login modal
  def http_auth?
    turbo_native_app? || super
  end
end

end

# 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/)
# Hotwire Native applications are identified by having the string "Hotwire Native" as part of their user agent.
# Legacy Turbo Native applications use the "Turbo Native" string.
def hotwire_native_app?
request.user_agent.to_s.match?(/(Turbo|Hotwire) Native/)
end


alias_method :turbo_native_app?, :hotwire_native_app?

# 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.
def recede_or_redirect_to(url, **options)
turbo_native_action_or_redirect url, :recede, :to, options
Expand Down
10 changes: 5 additions & 5 deletions test/native/navigation_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Turbo::Native::NavigationControllerTest < ActionDispatch::IntegrationTest
post trays_path, params: { return_to: "#{action}_or_redirect" }
assert_redirected_to tray_path(id: 1)

post trays_path, params: { return_to: "#{action}_or_redirect" }, headers: header_for_turbo_native_app
post trays_path, params: { return_to: "#{action}_or_redirect" }, headers: header_for_hotwire_native_app
assert_redirected_to send("turbo_#{action}_historical_location_url")
end
end
Expand All @@ -19,7 +19,7 @@ class Turbo::Native::NavigationControllerTest < ActionDispatch::IntegrationTest
post trays_path, params: { return_to: "#{action}_or_redirect_back" }, headers: header_for_referer
assert_redirected_to "/past_place"

post trays_path, params: { return_to: "#{action}_or_redirect_back" }, headers: header_for_turbo_native_app.merge(header_for_referer)
post trays_path, params: { return_to: "#{action}_or_redirect_back" }, headers: header_for_hotwire_native_app.merge(header_for_referer)
assert_redirected_to send("turbo_#{action}_historical_location_url")
end
end
Expand All @@ -28,7 +28,7 @@ class Turbo::Native::NavigationControllerTest < ActionDispatch::IntegrationTest
post trays_path, params: { return_to: "refresh_or_redirect_with_options" }
assert_redirected_to tray_path(id: 5)

post trays_path, params: { return_to: "refresh_or_redirect_with_options" }, headers: header_for_turbo_native_app
post trays_path, params: { return_to: "refresh_or_redirect_with_options" }, headers: header_for_hotwire_native_app
assert_redirected_to send("turbo_refresh_historical_location_url", notice: "confirmed", custom: 123)
end

Expand All @@ -40,8 +40,8 @@ class Turbo::Native::NavigationControllerTest < ActionDispatch::IntegrationTest
end

private
def header_for_turbo_native_app
{ "HTTP_USER_AGENT" => "MyApp iOS/3.0 Turbo Native (build 13; iPad Air 2); iOS 9.3" }
def header_for_hotwire_native_app
{ "HTTP_USER_AGENT" => "MyApp iOS/3.0 Hotwire Native (build 13; iPad Air 2); iOS 9.3" }
end

def header_for_referer
Expand Down
Loading