Skip to content

Commit

Permalink
Mark variant as turbo_frame when header present
Browse files Browse the repository at this point in the history
Closes #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
  • Loading branch information
seanpdoyle committed Sep 17, 2021
1 parent f56024c commit d0a7661
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/controllers/frame_requests_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class FrameRequestsController < ApplicationController
end
15 changes: 15 additions & 0 deletions app/controllers/turbo/frames/frame_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,25 @@
# that frame, not the whole page. They are automatically tagged as such by the Turbo Frame JavaScript, which adds a
# <tt>Turbo-Frame</tt> header to the request.
#
# When the <tt>Turbo-Frame</tt> header is present, the request's <a
# href="https://guides.rubyonrails.org/4_1_release_notes.html#action-pack-variants">variant</a>
# will be marked <tt>turbo_frame</tt>.
#
# As an optimization, applications can declare a layout with the
# <tt>html+turbo_frame.erb</tt> extension that renders
# without the navigation and layout elements:
#
# <%= app/views/layouts/application.html+turbo_frame.erb %>
# <%= yield %>
#
# This module is automatically included in <tt>ActionController::Base</tt>.
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?
Expand Down
1 change: 1 addition & 0 deletions test/dummy/app/views/frame_requests/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1><%= controller_name %>/<%= action_name %></h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= yield %>
8 changes: 8 additions & 0 deletions test/dummy/app/views/layouts/frame_requests.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title><%= controller_name %>/<%= action_name %></title>
</head>
<body>
<%= yield %>
</body>
</html>
1 change: 1 addition & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :frame_requests
resources :messages
resources :trays
resources :posts
Expand Down
10 changes: 10 additions & 0 deletions test/frames/frame_request_controller_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit d0a7661

Please sign in to comment.