render_turbo_stream 2.0.4 → 2.1.1
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac522064ae87773819b4fdd059166e10a8bbb805de1dbbd020781f806ac6faeb
|
4
|
+
data.tar.gz: a78259d9e2468305da273618929296735f3217592ed0ac2f0311e14b9c913eaf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b9e22fa2a8dbb9d7bb85d9fd19993ac6511c4cf9364c20da4b950b4bb368839c48f8c6ac93f31452592bc286f717d4c63107db7757731e339f1a37b871c7332a
|
7
|
+
data.tar.gz: 5c13d3c56d66450c20920403cef30c18c18e0101ba0c3de86516d9db26d57edcd5c3a37866dc6e2c9052e0f2be34c2751486b29361dd93a5b1f54f5255660dc6
|
data/README.md
CHANGED
@@ -1,12 +1,16 @@
|
|
1
1
|
# RenderTurboStream
|
2
2
|
|
3
|
-
Defining templates like `(create|update).turbo_stream.haml` annoyed me
|
3
|
+
Defining templates like `(create|update).turbo_stream.haml` annoyed me.
|
4
4
|
|
5
|
-
Working consistently with turbo_stream means shooting
|
5
|
+
Working consistently with turbo_stream or Turbo Streams Channel means shooting a lot of partials from the backend to the frontend. This always requires the same attributes: the path to the partial, the html id that turbo_stream points to, and maybe some locals, and maybe generate a translated flash message for update or create actions. All this can be done in one step directly from the controller.
|
6
6
|
|
7
|
-
|
7
|
+
There are many ways and tedious details to handle redirects since Turbo! Below described are options.
|
8
8
|
|
9
|
-
|
9
|
+
Turbo has seamlessly integrated Action Cable. The intention here is to bring it all together in a unified workflow with a sufficient testing strategy.
|
10
|
+
|
11
|
+
Execute [turbo_power](https://github.com/marcoroth/turbo_power) commands, such as adding a css class to an html element, pushing a state to the browser history, or running custom javascript actions through Turbo Stream (cable not yet integrated here) can be written in pure ruby code. No need for embeds like `.html.erb`.
|
12
|
+
|
13
|
+
This plugin is in a early state.
|
10
14
|
|
11
15
|
An overview of how we design a rails-7 application with turbo
|
12
16
|
is [published on dev.to](https://dev.to/chmich/rails-7-vite-wrapping-up-1pia).
|
@@ -15,6 +19,8 @@ A quick and dirty application with all the features, including tests, built in i
|
|
15
19
|
|
16
20
|
Hope it can help you.
|
17
21
|
|
22
|
+
**Chris**
|
23
|
+
|
18
24
|
## Installation
|
19
25
|
|
20
26
|
```ruby
|
@@ -76,7 +82,7 @@ A comprehensive tutorial on turbo and how to check that it is working properly c
|
|
76
82
|
**Turbo::StreamsChannel**
|
77
83
|
|
78
84
|
The Rails team has seamlessly integrated `ActionCable` as `Turbo::StreamsChannel` into `Turbo Rails`. For installation along with this gem, see the [README](https://gitlab.com/sedl/renderturbostream/-/blob/main/README-cable.md).
|
79
|
-
|
85
|
+
# Usage
|
80
86
|
|
81
87
|
`turbo_stream_save` is a special method for streamlining `update` or `create` functions with `turbo_stream`. A controller action for update might look like this:
|
82
88
|
|
@@ -207,7 +213,7 @@ def update
|
|
207
213
|
flashes_on_error: [] #=> array of strings
|
208
214
|
|
209
215
|
|
210
|
-
|
216
|
+
# Testing Scenarios
|
211
217
|
|
212
218
|
For system testing there is Capybara. Its the only way to check if frontend and backend work together. But its a good practice to break tests into smaller pieces. The much larger number of tests we will write on the much faster request tests, examples here in rspec.
|
213
219
|
|
@@ -288,7 +294,7 @@ For more detailed testing of partials, there are view tests.
|
|
288
294
|
Testing the plugin itself: There is a [quick-and-dirty app](https://gitlab.com/sedl/renderturbostream_railsapp) which
|
289
295
|
includes the plugin and has tests done by rspec/request and capybara.
|
290
296
|
|
291
|
-
|
297
|
+
# More Configs
|
292
298
|
|
293
299
|
On test helpers, the marker for a turbo-stream target is in most cases the id of the target element. This is true for the standard turbo-stream functions. On `turbo_power` it is the same in most cases. `RenderTurboStream::Libs.first_arg_is_html_id()` checks for which methods this is true. You can override this:
|
294
300
|
|
@@ -299,14 +305,14 @@ config.x.render_turbo_stream.first_argument_is_html_id = %[replace append prepen
|
|
299
305
|
This setting is relevant for testing helpers.
|
300
306
|
|
301
307
|
|
302
|
-
|
308
|
+
# Contributing
|
303
309
|
|
304
310
|
Contribution welcome.
|
305
311
|
|
306
|
-
|
312
|
+
# License
|
307
313
|
|
308
314
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
309
315
|
|
310
|
-
|
316
|
+
# Thanks
|
311
317
|
|
312
318
|
Thanks to Marco Roth for turbo_power and thanks to DHH and the rails team for taking a bold new step by creating turbo.
|
@@ -1,32 +1,32 @@
|
|
1
1
|
module RenderTurboStream
|
2
2
|
module TurboCableHelpers
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
#
|
8
|
-
# end
|
9
|
-
# end
|
4
|
+
def partial_to_all_authenticated_users
|
5
|
+
raise "Function partial_to_all_authenticated_users is not yet implemented"
|
6
|
+
if user_signed_in?
|
10
7
|
|
11
|
-
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def partial_to_channel(action, channel, partial, id, locals: nil)
|
12
12
|
|
13
13
|
# add headers for test
|
14
14
|
if Rails.env.test?
|
15
|
-
|
15
|
+
html = RenderTurboStreamRenderController.render(partial: partial, locals: locals)
|
16
|
+
props = {
|
16
17
|
target: "##{id}",
|
17
18
|
action: action,
|
18
19
|
type: 'cable-partial',
|
19
20
|
partial: partial,
|
20
|
-
locals: locals
|
21
|
+
locals: locals,
|
22
|
+
html_response: html.to_s
|
21
23
|
}
|
22
|
-
html = RenderTurboStreamRenderController.render(partial: partial, locals: locals)
|
23
24
|
h = response.headers.to_h
|
24
25
|
i = 1
|
25
26
|
loop do
|
26
27
|
k = "test-turbo-cable-#{i}"
|
27
28
|
unless h.keys.include?(k)
|
28
|
-
response.headers[k] =
|
29
|
-
response.headers["#{k}-html"] = html.to_s
|
29
|
+
response.headers[k] = props.to_json
|
30
30
|
break
|
31
31
|
end
|
32
32
|
i += 1
|
@@ -45,22 +45,22 @@ module RenderTurboStream
|
|
45
45
|
)
|
46
46
|
end
|
47
47
|
|
48
|
-
def
|
48
|
+
def partial_to_me(partial, id, action: :replace, locals: nil)
|
49
49
|
begin
|
50
50
|
u_id = helpers.current_user&.id
|
51
51
|
a=1
|
52
52
|
unless u_id.present?
|
53
|
-
Rails.logger.debug(' • SKIP RenderTurboStream.
|
53
|
+
Rails.logger.debug(' • SKIP RenderTurboStream.partial_to_me because current_user is nil')
|
54
54
|
return
|
55
55
|
end
|
56
56
|
rescue
|
57
|
-
Rails.logger.debug(' • ERROR RenderTurboStream.
|
57
|
+
Rails.logger.debug(' • ERROR RenderTurboStream.partial_to_me because current_user is not available')
|
58
58
|
return
|
59
59
|
end
|
60
60
|
|
61
|
-
|
61
|
+
partial_to_channel(
|
62
62
|
action,
|
63
|
-
"
|
63
|
+
"authenticated-user-#{helpers.current_user.id}",
|
64
64
|
partial,
|
65
65
|
id,
|
66
66
|
locals: locals
|
@@ -3,19 +3,20 @@ module RenderTurboStream
|
|
3
3
|
|
4
4
|
def cable_from_me
|
5
5
|
if current_user
|
6
|
-
turbo_stream_from "
|
6
|
+
turbo_stream_from "authenticated-user-#{current_user.id}"
|
7
7
|
else
|
8
8
|
Rails.logger.debug(" • SKIP CABLE_FROM_ME because not authenticated")
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
12
|
+
def cable_from_all_authenticated_users
|
13
|
+
raise "Function cable_from_all_authenticated_users is not yet implemented"
|
14
|
+
if user_signed_in?
|
15
|
+
turbo_stream_from "all-authenticated-users"
|
16
|
+
else
|
17
|
+
Rails.logger.debug(" • SKIP CABLE_FROM_ALL_AUTHENTICATED_USERS because not authenticated")
|
18
|
+
end
|
19
|
+
end
|
19
20
|
|
20
21
|
def cable_from_all
|
21
22
|
turbo_stream_from "all"
|
data/lib/render_turbo_stream.rb
CHANGED
@@ -162,7 +162,7 @@ module RenderTurboStream
|
|
162
162
|
streams.each do |s|
|
163
163
|
next unless s.is_a?(Hash)
|
164
164
|
Rails.logger.debug(" • Send by Cable => «#{s}»")
|
165
|
-
|
165
|
+
partial_to_me(
|
166
166
|
s[:partial],
|
167
167
|
s[:id],
|
168
168
|
action: flash_action,
|
@@ -237,23 +237,11 @@ module RenderTurboStream
|
|
237
237
|
class Libs
|
238
238
|
def self.all_responses(response)
|
239
239
|
e = Nokogiri::HTML(response.body).search('#rendered-partials').first
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
cables = response.headers.to_h.select { |k| k.match(/^test-turbo-cable-[\d]+$/) }
|
246
|
-
cables.each do |k, v|
|
247
|
-
args = JSON.parse(v)
|
248
|
-
#html = ApplicationController.render(partial: args['partial'], locals: args['locals'].symbolize_keys)
|
249
|
-
html = response.headers["#{k}-html"]
|
250
|
-
res.push(
|
251
|
-
args.merge(
|
252
|
-
{
|
253
|
-
'html_response' => html,
|
254
|
-
}
|
255
|
-
)
|
256
|
-
)
|
240
|
+
res = (e.present? ? JSON.parse(e.inner_html) : [])
|
241
|
+
response.headers.each do |k, v|
|
242
|
+
next unless k.match(/^test-turbo-cable-[\d]+$/)
|
243
|
+
h = JSON.parse(v)
|
244
|
+
res.push(h)
|
257
245
|
end
|
258
246
|
res
|
259
247
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: render_turbo_stream
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- christian
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-05-
|
11
|
+
date: 2023-05-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -27,7 +27,7 @@ dependencies:
|
|
27
27
|
description: Handles translated flash messages, sets status and renders partials via
|
28
28
|
Turbo Stream or Turbo::StreamsChannel directly from the controller. No need to write
|
29
29
|
*.turbo_stream.* templates. Together with the turbo_power gem or custom turbo_stream
|
30
|
-
actions,
|
30
|
+
actions, it can run javascript. Redirects can be handled easily and in multiple
|
31
31
|
ways. Through request testing helpers, this allows for consistent testing strategy.
|
32
32
|
email:
|
33
33
|
- christian@sedlmair.ch
|