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

Extend ActionDispatch::Request #250

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/controllers/turbo/frames/frame_request.rb
Expand Up @@ -15,10 +15,11 @@ module Turbo::Frames::FrameRequest
included do
layout -> { false if turbo_frame_request? }
etag { :frame if turbo_frame_request? }
before_action { request.variant.push(:turbo_frame) if turbo_frame_request? }
end

private
def turbo_frame_request?
request.headers["Turbo-Frame"].present?
request.turbo_frame?
end
end
7 changes: 7 additions & 0 deletions lib/turbo/engine.rb
@@ -1,5 +1,6 @@
require "rails/engine"
require "turbo/test_assertions"
require "turbo/frames/request_extensions"

module Turbo
class Engine < Rails::Engine
Expand Down Expand Up @@ -43,6 +44,12 @@ class Engine < Rails::Engine
Mime::Type.register "text/vnd.turbo-stream.html", :turbo_stream
end

initializer "turbo.request_extensions" do
ActiveSupport.on_load(:action_dispatch_request) do
include Turbo::Frames::RequestExtensions
end
end

initializer "turbo.renderer" do
ActiveSupport.on_load(:action_controller) do
ActionController::Renderers.add :turbo_stream do |turbo_streams_html, options|
Expand Down
13 changes: 13 additions & 0 deletions lib/turbo/frames/request_extensions.rb
@@ -0,0 +1,13 @@
module Turbo
module Frames
module RequestExtensions
def turbo_frame?
turbo_frame.present?
end

def turbo_frame
headers["Turbo-Frame"]
end
end
end
end
1 change: 1 addition & 0 deletions test/dummy/app/views/trays/show.html.erb
@@ -1,3 +1,4 @@
<turbo-frame id="tray">
<div>This is a tray!</div>
<div id="variant"><%= safe_join request.variant, "," %></div>
</turbo-frame>
8 changes: 8 additions & 0 deletions test/frames/frame_request_controller_test.rb
Expand Up @@ -18,4 +18,12 @@ class Turbo::FrameRequestControllerTest < ActionDispatch::IntegrationTest

assert_not_equal etag_with_frame, etag_without_frame
end

test "frame requests have a turbo_frame variant" do
get tray_path(id: 1)
assert_select "#variant", text: ""

get tray_path(id: 1), headers: { "Turbo-Frame" => "true" }
assert_select "#variant", text: "turbo_frame"
end
end