turbo-rails 2.0.2 → 2.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a6b62b2ebe62c102e59d47bd27106b8e4eb7d13981dcff4f320337c8386a21e6
4
- data.tar.gz: f5966219c9772a134548f1d21d00b68c52dcaf3a790ea8ed98c5b691fcbf22aa
3
+ metadata.gz: 7de922709135b46c78da967fc906370e96bace376d0c03b205b5b7fb0bf0c1db
4
+ data.tar.gz: 36c2463b1a996f8d225a3ba33eb73bb07d2c34eccf8316a4670cebca2ed1c90b
5
5
  SHA512:
6
- metadata.gz: 1e7c17ae5bf6cdd69f127862e7493bfe7957609b6efffd9632df71904a2453ec91db23336de207d07ada6befeb427a0fec7c65e63661286700a7d7edcbc1124a
7
- data.tar.gz: 28ea056509d04849be4758049cd558b6b3bc80affd4edf2b4eb66080360e2470ab0faaf8ce7862f5689de37b807bb5c388573a6b4df8d7f55c9c4d16d8d6fc90
6
+ metadata.gz: 88303af9181adb99632e23da782c7ab7063c7ce194271b3b9dbf92d89d1e1d2cd3cf1a88147f38ca957d74f7ff310cbf455e3c636cf705ef76cc46b6583b84b1
7
+ data.tar.gz: '02446187359ca3060de17e6bc435f159d5d5aa98afee83280487372bf63a70b901a44314119119ee9ff4947fba8b93d971d6f143815240cbd0bed9dcd5a7d69e'
data/README.md CHANGED
@@ -56,7 +56,7 @@ When the user clicks on the `Edit this todo` link, as a direct response to this
56
56
 
57
57
  ### A note on custom layouts
58
58
 
59
- In order to render turbo frame requests without the application layout, Turbo registers a custom [layout method](https://api.rubyonrails.org/classes/ActionView/Layouts/ClassMethods.html#method-i-layout).
59
+ In order to render turbo frame requests without the application layout, Turbo registers a custom [layout method](https://api.rubyonrails.org/classes/ActionView/Layouts/ClassMethods.html#method-i-layout).
60
60
  If your application uses custom layout resolution, you have to make sure to return `"turbo_rails/frame"` (or `false` for TurboRails < 1.4.0) for turbo frame requests:
61
61
 
62
62
  ```ruby
@@ -64,7 +64,7 @@ layout :custom_layout
64
64
 
65
65
  def custom_layout
66
66
  return "turbo_rails/frame" if turbo_frame_request?
67
-
67
+
68
68
  # ... your custom layout logic
69
69
  ```
70
70
 
@@ -74,14 +74,14 @@ If you are using a custom, but "static" layout,
74
74
  layout "some_static_layout"
75
75
  ```
76
76
 
77
- you **have** to change it to a layout method in order to conditionally return `false` for turbo frame requests:
77
+ you **have** to change it to a layout method in order to conditionally return `"turbo_rails/frame"` for turbo frame requests:
78
78
 
79
79
  ```ruby
80
80
  layout :custom_layout
81
81
 
82
82
  def custom_layout
83
83
  return "turbo_rails/frame" if turbo_frame_request?
84
-
84
+
85
85
  "some_static_layout"
86
86
  ```
87
87
 
@@ -100,6 +100,64 @@ This gem provides a `turbo_stream_from` helper to create a turbo stream.
100
100
  <%# Rest of show here %>
101
101
  ```
102
102
 
103
+ ### Testing Turbo Stream Broadcasts
104
+
105
+ Receiving server-generated Turbo Broadcasts requires a connected Web Socket.
106
+ Views that render `<turbo-cable-stream-source>` elements with the
107
+ `#turbo_stream_from` view helper incur a slight delay before they're ready to
108
+ receive broadcasts. In System Tests, that delay can disrupt Capybara's built-in
109
+ synchronization mechanisms that wait for or assert on content that's broadcast
110
+ over Web Sockets. For example, consider a test that navigates to a page and then
111
+ immediately asserts that broadcast content is present:
112
+
113
+ ```ruby
114
+ test "renders broadcasted Messages" do
115
+ message = Message.new content: "Hello, from Action Cable"
116
+
117
+ visit "/"
118
+ click_link "All Messages"
119
+ message.save! # execute server-side code to broadcast a Message
120
+
121
+ assert_text message.content
122
+ end
123
+ ```
124
+
125
+ If the call to `Message#save!` executes quickly enough, it might beat-out any
126
+ `<turbo-cable-stream-source>` elements rendered by the call to `click_link "All
127
+ Messages"`.
128
+
129
+ To wait for any disconnected `<turbo-cable-stream-source>` elements to connect,
130
+ call [`#connect_turbo_cable_stream_sources`](https://github.com/hotwired/turbo-rails/blob/main/lib/turbo/system_test_helper.rb):
131
+
132
+ ```diff
133
+ test "renders broadcasted Messages" do
134
+ message = Message.new content: "Hello, from Action Cable"
135
+
136
+ visit "/"
137
+ click_link "All Messages"
138
+ + connect_turbo_cable_stream_sources
139
+ message.save! # execute server-side code to broadcast a Message
140
+
141
+ assert_text message.content
142
+ end
143
+ ```
144
+
145
+ By default, calls to [`#visit`](https://rubydoc.info/github/teamcapybara/capybara/master/Capybara/Session:visit) will wait for all `<turbo-cable-stream-source>` elements to connect. You can control this by modifying the `config.turbo.test_connect_after_actions`. For example, to wait after calls to [`#click_link`](https://rubydoc.info/github/teamcapybara/capybara/master/Capybara/Node/Actions:click_link), add the following to `config/environments/test.rb`:
146
+
147
+ ```ruby
148
+ # config/environments/test.rb
149
+
150
+ config.turbo.test_connect_after_actions << :click_link
151
+ ```
152
+
153
+ To disable automatic connecting, set the configuration to `[]`:
154
+
155
+ ```ruby
156
+ # config/environments/test.rb
157
+
158
+ config.turbo.test_connect_after_actions = []
159
+ ```
160
+
103
161
  [See documentation](https://turbo.hotwired.dev/handbook/streams).
104
162
 
105
163
  ## Installation
@@ -109,9 +167,8 @@ This gem is automatically configured for applications made with Rails 7+ (unless
109
167
  1. Add the `turbo-rails` gem to your Gemfile: `gem 'turbo-rails'`
110
168
  2. Run `./bin/bundle install`
111
169
  3. Run `./bin/rails turbo:install`
112
- 4. Run `./bin/rails turbo:install:redis` to change the development Action Cable adapter from Async (the default one) to Redis. The Async adapter does not support Turbo Stream broadcasting.
113
170
 
114
- Running `turbo:install` will install through NPM if Node.js is used in the application. Otherwise the asset pipeline version is used. To use the asset pipeline version, you must have `importmap-rails` installed first and listed higher in the Gemfile.
171
+ Running `turbo:install` will install through NPM or Bun if a JavaScript runtime is used in the application. Otherwise the asset pipeline version is used. To use the asset pipeline version, you must have `importmap-rails` installed first and listed higher in the Gemfile.
115
172
 
116
173
  If you're using node and need to use the cable consumer, you can import [`cable`](https://github.com/hotwired/turbo-rails/blob/main/app/javascript/turbo/cable.js) (`import { cable } from "@hotwired/turbo-rails"`), but ensure that your application actually *uses* the members it `import`s when using this style (see [turbo-rails#48](https://github.com/hotwired/turbo-rails/issues/48)).
117
174
 
@@ -125,6 +182,8 @@ import "@hotwired/turbo-rails"
125
182
 
126
183
  You can watch [the video introduction to Hotwire](https://hotwired.dev/#screencast), which focuses extensively on demonstrating Turbo in a Rails demo. Then you should familiarize yourself with [Turbo handbook](https://turbo.hotwired.dev/handbook/introduction) to understand Drive, Frames, and Streams in-depth. Finally, dive into the code documentation by starting with [`Turbo::FramesHelper`](https://github.com/hotwired/turbo-rails/blob/main/app/helpers/turbo/frames_helper.rb), [`Turbo::StreamsHelper`](https://github.com/hotwired/turbo-rails/blob/main/app/helpers/turbo/streams_helper.rb), [`Turbo::Streams::TagBuilder`](https://github.com/hotwired/turbo-rails/blob/main/app/models/turbo/streams/tag_builder.rb), and [`Turbo::Broadcastable`](https://github.com/hotwired/turbo-rails/blob/main/app/models/concerns/turbo/broadcastable.rb).
127
184
 
185
+ Note that in development, the default Action Cable adapter is the single-process `async` adapter. This means that turbo updates are only broadcast within that same process. So you can't start `bin/rails console` and trigger Turbo broadcasts and expect them to show up in a browser connected to a server running in a separate `bin/dev` or `bin/rails server` process. Instead, you should use the web-console when needing to manually trigger Turbo broadcasts inside the same process. Add "console" to any action or "<%= console %>" in any view to make the web console appear.
186
+
128
187
  ### RubyDoc Documentation
129
188
 
130
189
  For the API documentation covering this gem's classes and packages, [visit the RubyDoc page](https://rubydoc.info/github/hotwired/turbo-rails/main).
@@ -139,6 +198,7 @@ Note that this documentation is updated automatically from the main branch, so i
139
198
  - [Turbo Test Assertions](https://rubydoc.info/github/hotwired/turbo-rails/main/Turbo/TestAssertions)
140
199
  - [Turbo Integration Test Assertions](https://rubydoc.info/github/hotwired/turbo-rails/main/Turbo/TestAssertions/IntegrationTestAssertions)
141
200
  - [Turbo Broadcastable Test Helper](https://rubydoc.info/github/hotwired/turbo-rails/main/Turbo/Broadcastable/TestHelper)
201
+ - [Turbo System Test Helper](https://rubydoc.info/github/hotwired/turbo-rails/main/Turbo/SystemTestHelper)
142
202
 
143
203
  ## Compatibility with Rails UJS
144
204
 
@@ -146,16 +206,62 @@ Turbo can coexist with Rails UJS, but you need to take a series of upgrade steps
146
206
 
147
207
  ## Testing
148
208
 
149
- The [`Turbo::TestAssertions`](./lib/turbo/test_assertions.rb) concern provides Turbo Stream test helpers that assert the presence or absence ofs s `<turbo-stream>` elements in a rendered fragment of HTML. `Turbo::TestAssertions` are automatically included in [`ActiveSaupport::TestCase`](https://edgeapi.rubyonrails.org/classes/ActiveSupport/TestCase.html) and depend on the presence of [`rails-dom-testing`](https://github.com/rails/rails-dom-testing/) assertions.
209
+ The [`Turbo::TestAssertions`](./lib/turbo/test_assertions.rb) concern provides Turbo Stream test helpers that assert the presence or absence ofs s `<turbo-stream>` elements in a rendered fragment of HTML. `Turbo::TestAssertions` are automatically included in [`ActiveSupport::TestCase`](https://edgeapi.rubyonrails.org/classes/ActiveSupport/TestCase.html) and depend on the presence of [`rails-dom-testing`](https://github.com/rails/rails-dom-testing/) assertions.
150
210
 
151
211
  The [`Turbo::TestAssertions::IntegrationTestAssertions`](./lib/turbo/test_assertions/integration_test_assertions.rb) are built on top of `Turbo::TestAssertions`, and add support for passing a `status:` keyword. They are automatically included in [`ActionDispatch::IntegrationTest`](https://edgeguides.rubyonrails.org/testing.html#integration-testing).
152
212
 
153
213
  The [`Turbo::Broadcastable::TestHelper`](./lib/turbo/broadcastable/test_helper.rb) concern provides Action Cable-aware test helpers that assert that `<turbo-stream>` elements were or were not broadcast over Action Cable. `Turbo::Broadcastable::TestHelper` is automatically included in [`ActiveSupport::TestCase`](https://edgeapi.rubyonrails.org/classes/ActiveSupport/TestCase.html).
154
214
 
215
+ ### Rendering Outside of a Request
216
+
217
+ Turbo utilizes [ActionController::Renderer][] to render templates and partials
218
+ outside the context of the request-response cycle. If you need to render a
219
+ Turbo-aware template, partial, or component, use [ActionController::Renderer][]:
220
+
221
+ ```ruby
222
+ ApplicationController.renderer.render template: "posts/show", assigns: { post: Post.first } # => "<html>…"
223
+ PostsController.renderer.render :show, assigns: { post: Post.first } # => "<html>…"
224
+ ```
225
+
226
+ As a shortcut, you can also call render directly on the controller class itself:
227
+
228
+ ```ruby
229
+ ApplicationController.render template: "posts/show", assigns: { post: Post.first } # => "<html>…"
230
+ PostsController.render :show, assigns: { post: Post.first } # => "<html>…"
231
+ ```
232
+
233
+ [ActionController::Renderer]: https://api.rubyonrails.org/classes/ActionController/Renderer.html
234
+
155
235
  ## Development
156
236
 
157
237
  Run the tests with `./bin/test`.
158
238
 
239
+ ### Using local Turbo version
240
+
241
+ Often you might want to test changes made locally to [Turbo lib](https://github.com/hotwired/turbo) itself. To package your local development version of Turbo you can use [yarn link](https://classic.yarnpkg.com/lang/en/docs/cli/link/) feature:
242
+
243
+ ```sh
244
+ cd <local-turbo-dir>
245
+ yarn link
246
+
247
+ cd <local-turbo-rails-dir>
248
+ yarn link @hotwired/turbo
249
+
250
+ # Build the JS distribution files...
251
+ yarn build
252
+ # ...and commit the changes
253
+ ```
254
+
255
+ Now you can reference your version of turbo-rails in your Rails projects packaged with your local version of Turbo.
256
+
257
+ ## Contributing
258
+
259
+ Having a way to reproduce your issue will help people confirm, investigate, and ultimately fix your issue. You can do this by providing an executable test case. To make this process easier, we have prepared an [executable bug report Rails application](./bug_report_template.rb) for you to use as a starting point.
260
+
261
+ This template includes the boilerplate code to set up a System Test case. Copy the content of the template into a `.rb` file and make the necessary changes to demonstrate the issue. You can execute it by running `ruby the_file.rb` in your terminal. If all goes well, you should see your test case failing.
262
+
263
+ You can then share your executable test case as a gist or paste the content into the issue description.
264
+
159
265
  ## License
160
266
 
161
267
  Turbo is released under the [MIT License](https://opensource.org/licenses/MIT).