Skip to content

Commit

Permalink
Add support for rendering turbo stream tags (#1227)
Browse files Browse the repository at this point in the history
* Add failing test for rendering turbo stream tags

* Skip Turbo test on Rails < 6

* Use Appraisal to only load turbo-rails for compatible versions

* Don't error if turbo-rails is required on an unsupported Rails version

* Add turbo-rails for rails-7.1 appraisal, regenerate lockfiles

* Re-add `end` tag that went missing in the rebase

* Make test pass and document

* Verify capture compatibility patch fixes this

* Require action_cable to navigate hotwired/turbo-rails#74

* Lint

* Remove lingering Rails 5.2/6.0 support

* Update docs/CHANGELOG.md

---------

Co-authored-by: Joel Hawksley <joel@hawksley.org>
  • Loading branch information
boardfish and joelhawksley authored Aug 20, 2024
1 parent 55306bc commit 5d0e075
Show file tree
Hide file tree
Showing 16 changed files with 50 additions and 106 deletions.
4 changes: 4 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ appraise "rails-6.1" do
gem "net-smtp", require: false
gem "net-imap", require: false
gem "net-pop", require: false
gem "turbo-rails", "~> 1"
end

appraise "rails-7.0" do
gem "rails", "~> 7.0"
gem "tailwindcss-rails", "~> 2.0"
gem "turbo-rails", "~> 1"
end

appraise "rails-7.1" do
gem "rails", "~> 7.1"
gem "tailwindcss-rails", "~> 2.0"
gem "turbo-rails", "~> 1"
end

appraise "rails-7.2" do
Expand All @@ -28,4 +31,5 @@ end
appraise "rails-main" do
gem "rails", github: "rails/rails", branch: "main"
gem "tailwindcss-rails", "~> 2.0"
gem "turbo-rails", "~> 1"
end
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ GEM
thor (1.3.1)
tilt (2.4.0)
timeout (0.4.1)
turbo-rails (1.5.0)
actionpack (>= 6.0.0)
activejob (>= 6.0.0)
railties (>= 6.0.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
Expand Down Expand Up @@ -355,6 +359,7 @@ DEPENDENCIES
slim (~> 5.1)
sprockets-rails (~> 3.4.2)
standard (~> 1)
turbo-rails (~> 1)
view_component!
warning
yard (~> 0.9.34)
Expand Down
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ nav_order: 5

## main

* Add test coverage for use of `turbo_stream` helpers in components when `capture_compatibility_patch_enabled` is `true`.

*Simon Fish*

* Add experimental `SlotableDefault` module, allowing components to define a `default_SLOTNAME` method to provide a default value for slots.

*Joel Hawksley*
Expand Down
19 changes: 0 additions & 19 deletions gemfiles/rails_5.2.gemfile

This file was deleted.

20 changes: 0 additions & 20 deletions gemfiles/rails_6.0.gemfile

This file was deleted.

19 changes: 3 additions & 16 deletions gemfiles/rails_6.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,11 @@

source "https://rubygems.org"

gem "capybara", "~> 3"
gem "rails", "~> 6.1"
gem "rspec-rails", "~> 5"
gem "tailwindcss-rails", "~> 2.0"
gem "net-smtp", require: false
gem "net-imap", require: false
gem "net-pop", require: false
gem "net-smtp", require: false
gem "debug"
gem "tailwindcss-rails", "~> 2.0"

group :test do
gem "cuprite", "~> 0.15"
gem "puma", "~> 6"
gem "warning"
gem "selenium-webdriver", "4.9.0"
end

group :development, :test do
gem "appraisal", "~> 2.5"
end
gem "turbo-rails", "~> 1"

gemspec path: "../"
18 changes: 1 addition & 17 deletions gemfiles/rails_7.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,8 @@

source "https://rubygems.org"

gem "capybara", "~> 3"
gem "rails", "~> 7.0"
gem "rspec-rails", "~> 5"
gem "net-imap", require: false
gem "net-pop", require: false
gem "net-smtp", require: false
gem "debug"
gem "tailwindcss-rails", "~> 2.0"

group :test do
gem "cuprite", "~> 0.15"
gem "puma", "~> 6"
gem "warning"
gem "selenium-webdriver", "4.9.0"
end

group :development, :test do
gem "appraisal", "~> 2.5"
end
gem "turbo-rails", "~> 1"

gemspec path: "../"
18 changes: 1 addition & 17 deletions gemfiles/rails_7.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,8 @@

source "https://rubygems.org"

gem "capybara", "~> 3"
gem "rails", "~> 7.1"
gem "rspec-rails", "~> 5"
gem "net-imap", require: false
gem "net-pop", require: false
gem "net-smtp", require: false
gem "debug"
gem "tailwindcss-rails", "~> 2.0"

group :test do
gem "cuprite", "~> 0.15"
gem "puma", "~> 6"
gem "warning"
gem "selenium-webdriver", "4.9.0"
end

group :development, :test do
gem "appraisal", "~> 2.5"
end
gem "turbo-rails", "~> 1"

gemspec path: "../"
18 changes: 1 addition & 17 deletions gemfiles/rails_main.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,8 @@

source "https://rubygems.org"

gem "capybara", "~> 3"
gem "rails", github: "rails/rails", branch: "main"
gem "rspec-rails", "~> 5"
gem "net-imap", require: false
gem "net-pop", require: false
gem "net-smtp", require: false
gem "debug"
gem "tailwindcss-rails", "~> 2.0"

group :test do
gem "cuprite", "~> 0.15"
gem "puma", "~> 6"
gem "warning"
gem "selenium-webdriver", "4.9.0"
end

group :development, :test do
gem "appraisal", "~> 2.5"
end
gem "turbo-rails", "~> 1"

gemspec path: "../"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= helpers.turbo_stream.update 'area1' do %><span>Hello, world!</span><% end %>
4 changes: 4 additions & 0 deletions test/sandbox/app/components/turbo_stream_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class TurboStreamComponent < ViewComponent::Base
end
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,8 @@ def unsafe_preamble_component
def unsafe_postamble_component
render(UnsafePostambleComponent.new)
end

def turbo_stream
respond_to { |format| format.turbo_stream { render TurboStreamComponent.new } }
end
end
3 changes: 3 additions & 0 deletions test/sandbox/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
require File.expand_path("boot", __dir__)

require "active_model/railtie"
require "action_cable/engine" # Remove when hotwired/turbo-rails#74 is resolved
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "sprockets/railtie"

require "turbo-rails"

# Track when different Rails frameworks get loaded.
# Ideally, none of them should be loaded until after initialization is complete.
# See config/initializers/zzz_complete_initialization.rb for the other half of this.
Expand Down
1 change: 1 addition & 0 deletions test/sandbox/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@
get :constraints_with_env, to: "integration_examples#index"
end
get :helpers_proxy_component, to: "integration_examples#helpers_proxy_component"
get :turbo_stream, to: "integration_examples#turbo_stream"
end
17 changes: 17 additions & 0 deletions test/sandbox/test/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,23 @@ def test_does_not_render_additional_newline_with_render_in
ActionView::Template::Handlers::ERB.strip_trailing_newlines = false if Rails::VERSION::MAJOR >= 7
end

# This test documents a bug that reports an incompatibility with the turbo-rails gem's `turbo_stream` helper.
# This helper may work if the `capture_compatibility_patch` is enabled.
# Prefer `tag.turbo_stream` instead if you do not have the patch enabled already.
def test_render_component_in_turbo_stream
without_template_annotations do
get turbo_stream_path, headers: {"HTTP_ACCEPT" => "text/vnd.turbo-stream.html"}
expected_response_body = <<~TURBOSTREAM
<turbo-stream action="update" target="area1"><template><span>Hello, world!</span></template></turbo-stream>
TURBOSTREAM
if ViewComponent::Base.config.capture_compatibility_patch_enabled
assert_equal expected_response_body, response.body
else
assert_not_equal expected_response_body, response.body
end
end
end

def test_renders_the_preview_example_with_its_own_template_and_a_layout
get "/rails/view_components/my_component/inside_banner"
assert_includes response.body, "ViewComponent - Admin - Test"
Expand Down
1 change: 1 addition & 0 deletions view_component.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "simplecov-console", "~> 0.9.1"
spec.add_development_dependency "slim", "~> 5.1"
spec.add_development_dependency "sprockets-rails", "~> 3.4.2"
spec.add_development_dependency "turbo-rails", "~> 1"
spec.add_development_dependency "warning"
spec.add_development_dependency "yard", "~> 0.9.34"
spec.add_development_dependency "yard-activesupport-concern", "~> 0.0.1"
Expand Down

0 comments on commit 5d0e075

Please sign in to comment.