From d0a7661bf772c67d6b6cf478a47fd43f3f25a374 Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Fri, 17 Sep 2021 10:10:42 -0400 Subject: [PATCH] Mark variant as `turbo_frame` when header present Closes https://github.com/hotwired/turbo-rails/issues/229 --- Mark the request's [variant][] as `:turbo_frame` whenever the `Turbo-Frame:` header is present in the request. Also adds documentation to describe how to achieve the previously built-in layout optimizations. [variant]: https://guides.rubyonrails.org/4_1_release_notes.html#action-pack-variants --- app/controllers/frame_requests_controller.rb | 2 ++ app/controllers/turbo/frames/frame_request.rb | 15 +++++++++++++++ .../dummy/app/views/frame_requests/index.html.erb | 1 + .../layouts/frame_requests.html+turbo_frame.erb | 1 + .../app/views/layouts/frame_requests.html.erb | 8 ++++++++ test/dummy/config/routes.rb | 1 + test/frames/frame_request_controller_test.rb | 10 ++++++++++ 7 files changed, 38 insertions(+) create mode 100644 app/controllers/frame_requests_controller.rb create mode 100644 test/dummy/app/views/frame_requests/index.html.erb create mode 100644 test/dummy/app/views/layouts/frame_requests.html+turbo_frame.erb create mode 100644 test/dummy/app/views/layouts/frame_requests.html.erb diff --git a/app/controllers/frame_requests_controller.rb b/app/controllers/frame_requests_controller.rb new file mode 100644 index 00000000..f87630d8 --- /dev/null +++ b/app/controllers/frame_requests_controller.rb @@ -0,0 +1,2 @@ +class FrameRequestsController < ApplicationController +end diff --git a/app/controllers/turbo/frames/frame_request.rb b/app/controllers/turbo/frames/frame_request.rb index 229c094b..7e28ee53 100644 --- a/app/controllers/turbo/frames/frame_request.rb +++ b/app/controllers/turbo/frames/frame_request.rb @@ -2,10 +2,25 @@ # that frame, not the whole page. They are automatically tagged as such by the Turbo Frame JavaScript, which adds a # Turbo-Frame header to the request. # +# When the Turbo-Frame header is present, the request's variant +# will be marked turbo_frame. +# +# As an optimization, applications can declare a layout with the +# html+turbo_frame.erb extension that renders +# without the navigation and layout elements: +# +# <%= app/views/layouts/application.html+turbo_frame.erb %> +# <%= yield %> +# # This module is automatically included in ActionController::Base. module Turbo::Frames::FrameRequest extend ActiveSupport::Concern + included do + before_action -> { request.variant.push :turbo_frame }, if: :turbo_frame_request? + end + private def turbo_frame_request? request.headers["Turbo-Frame"].present? diff --git a/test/dummy/app/views/frame_requests/index.html.erb b/test/dummy/app/views/frame_requests/index.html.erb new file mode 100644 index 00000000..6df570a3 --- /dev/null +++ b/test/dummy/app/views/frame_requests/index.html.erb @@ -0,0 +1 @@ +

<%= controller_name %>/<%= action_name %>

diff --git a/test/dummy/app/views/layouts/frame_requests.html+turbo_frame.erb b/test/dummy/app/views/layouts/frame_requests.html+turbo_frame.erb new file mode 100644 index 00000000..37f0bddb --- /dev/null +++ b/test/dummy/app/views/layouts/frame_requests.html+turbo_frame.erb @@ -0,0 +1 @@ +<%= yield %> diff --git a/test/dummy/app/views/layouts/frame_requests.html.erb b/test/dummy/app/views/layouts/frame_requests.html.erb new file mode 100644 index 00000000..c47e8bf7 --- /dev/null +++ b/test/dummy/app/views/layouts/frame_requests.html.erb @@ -0,0 +1,8 @@ + + + <%= controller_name %>/<%= action_name %> + + + <%= yield %> + + diff --git a/test/dummy/config/routes.rb b/test/dummy/config/routes.rb index 17f3af84..02de2efe 100644 --- a/test/dummy/config/routes.rb +++ b/test/dummy/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + resources :frame_requests resources :messages resources :trays resources :posts diff --git a/test/frames/frame_request_controller_test.rb b/test/frames/frame_request_controller_test.rb index 4999d0e1..3dd13e5b 100644 --- a/test/frames/frame_request_controller_test.rb +++ b/test/frames/frame_request_controller_test.rb @@ -1,6 +1,16 @@ require "turbo_test" class Turbo::FrameRequestControllerTest < ActionDispatch::IntegrationTest + test "frame request have turbo_frame variant" do + get frame_requests_path + + assert_select "title", count: 1, text: "frame_requests/index" + + get frame_requests_path, headers: { "Turbo-Frame" => "true" } + + assert_select "title", count: 0 + end + test "frame requests are rendered with a layout" do get tray_path(id: 1) assert_select "title", count: 1