render_turbo_stream 2.0.0 → 2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -3
- data/app/controllers/render_turbo_stream/render_turbo_stream_render_controller.rb +4 -0
- data/lib/render_turbo_stream/turbo_cable_helpers.rb +47 -38
- data/lib/render_turbo_stream/turbo_cable_view_helpers.rb +10 -6
- data/lib/render_turbo_stream/version.rb +1 -1
- data/lib/render_turbo_stream.rb +5 -4
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9858266fb82670a9ef99550a3acb17c4d744299969634a16dc34f05a62b6e5e7
|
4
|
+
data.tar.gz: 445dd3768ef6674b7286fb55d7738f9c112c31f5d57841f8fb8b4390c64b31af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dae6e979f4abed3f7b287a9fe5d39d8a2b3c7d0e92f4048d1bc551ddedfd39fd365c26cc8bf7be1c67d6e21a63f22670107e292ba50728db824c09ed48cbaf91
|
7
|
+
data.tar.gz: 1c5feab440874cc4f8e75324b60c1f352eb4a0cc54a38ff55598250f898e02bd5fbe454ea68fb6542a12f3b6c1eeab8493a2f7c146861c3010295eaedb58a7c2
|
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# RenderTurboStream
|
2
2
|
|
3
|
-
Defining templates like `(create|update).turbo_stream.haml` annoyed me.
|
4
|
-
|
5
|
-
The biggest advantage of this gem is the testing strategy.
|
3
|
+
Defining templates like `(create|update).turbo_stream.haml` annoyed me and I was looking for a testing strategy.
|
6
4
|
|
7
5
|
Working consistently with turbo_stream means shooting lots 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. This gem serialises that: Partials can be controlled directly from the controller. It sets the status, generates a flash message, handles redirection, pushes it all to the front. Includes helpers for enabling request tests.
|
8
6
|
|
@@ -1,61 +1,70 @@
|
|
1
1
|
module RenderTurboStream
|
2
2
|
module TurboCableHelpers
|
3
3
|
|
4
|
-
def
|
5
|
-
|
4
|
+
# def cable_partial_to_all_authenticated_users
|
5
|
+
# raise "Function cable_partial_to_all_authenticated_users is "
|
6
|
+
# if user_signed_in?
|
7
|
+
#
|
8
|
+
# end
|
9
|
+
# end
|
6
10
|
|
7
|
-
|
8
|
-
end
|
11
|
+
def cable_partial_to_channel(action, channel, partial, id, locals: nil)
|
9
12
|
|
10
|
-
|
11
|
-
begin
|
12
|
-
u_id = helpers.current_user&.id
|
13
|
-
a=1
|
14
|
-
unless u_id.present?
|
15
|
-
Rails.logger.debug(' • SKIP RenderTurboStream.cable_to_me because current_user is nil')
|
16
|
-
return
|
17
|
-
end
|
18
|
-
rescue
|
19
|
-
Rails.logger.debug(' • ERROR RenderTurboStream.cable_to_me because current_user is not available')
|
20
|
-
return
|
21
|
-
end
|
13
|
+
# add headers for test
|
22
14
|
if Rails.env.test?
|
23
15
|
args = {
|
24
16
|
target: "##{id}",
|
25
17
|
action: action,
|
26
18
|
type: 'cable-partial',
|
27
|
-
args: args,
|
28
19
|
partial: partial,
|
29
20
|
locals: locals
|
30
21
|
}
|
22
|
+
html = RenderTurboStreamRenderController.render(partial: partial, locals: locals)
|
31
23
|
h = response.headers.to_h
|
32
24
|
i = 1
|
33
25
|
loop do
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
26
|
+
k = "test-turbo-cable-#{i}"
|
27
|
+
unless h.keys.include?(k)
|
28
|
+
response.headers[k] = args.to_json
|
29
|
+
response.headers["#{k}-html"] = html.to_s
|
30
|
+
break
|
31
|
+
end
|
39
32
|
i += 1
|
40
33
|
end
|
41
34
|
end
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
35
|
+
|
36
|
+
# send
|
37
|
+
|
38
|
+
Turbo::StreamsChannel.send(
|
39
|
+
"broadcast_#{action}_to",
|
40
|
+
channel.to_s,
|
41
|
+
target: id,
|
42
|
+
partial: partial,
|
43
|
+
locals: locals&.symbolize_keys,
|
44
|
+
layout: false
|
45
|
+
)
|
46
|
+
end
|
47
|
+
|
48
|
+
def cable_partial_to_me(partial, id, action: :replace, locals: nil)
|
49
|
+
begin
|
50
|
+
u_id = helpers.current_user&.id
|
51
|
+
a=1
|
52
|
+
unless u_id.present?
|
53
|
+
Rails.logger.debug(' • SKIP RenderTurboStream.cable_partial_to_me because current_user is nil')
|
54
|
+
return
|
55
|
+
end
|
56
|
+
rescue
|
57
|
+
Rails.logger.debug(' • ERROR RenderTurboStream.cable_partial_to_me because current_user is not available')
|
58
|
+
return
|
58
59
|
end
|
60
|
+
|
61
|
+
cable_partial_to_channel(
|
62
|
+
action,
|
63
|
+
"current_user_#{helpers.current_user.id}",
|
64
|
+
partial,
|
65
|
+
id,
|
66
|
+
locals: locals
|
67
|
+
)
|
59
68
|
end
|
60
69
|
|
61
70
|
end
|
@@ -9,12 +9,16 @@ module RenderTurboStream
|
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
-
def cable_from_all_authenticated_users
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
12
|
+
# def cable_from_all_authenticated_users
|
13
|
+
# if user_signed_in?
|
14
|
+
# turbo_stream_from "all_authenticated_users"
|
15
|
+
# else
|
16
|
+
# Rails.logger.debug(" • SKIP CABLE_FROM_ALL_AUTHENTICATED_USERS because not authenticated")
|
17
|
+
# end
|
18
|
+
# end
|
19
|
+
|
20
|
+
def cable_from_all
|
21
|
+
turbo_stream_from "all"
|
18
22
|
end
|
19
23
|
|
20
24
|
def local_model(object)
|
data/lib/render_turbo_stream.rb
CHANGED
@@ -162,10 +162,10 @@ 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
|
+
cable_partial_to_me(
|
166
|
+
s[:partial],
|
166
167
|
s[:id],
|
167
|
-
:
|
168
|
-
partial: s[:partial],
|
168
|
+
action: flash_action,
|
169
169
|
locals: s[:locals]
|
170
170
|
)
|
171
171
|
end
|
@@ -245,7 +245,8 @@ module RenderTurboStream
|
|
245
245
|
cables = response.headers.to_h.select { |k| k.match(/^test-turbo-cable-[\d]+$/) }
|
246
246
|
cables.each do |k, v|
|
247
247
|
args = JSON.parse(v)
|
248
|
-
html = ApplicationController.render(partial: args['partial'], locals: args['locals'].symbolize_keys)
|
248
|
+
#html = ApplicationController.render(partial: args['partial'], locals: args['locals'].symbolize_keys)
|
249
|
+
html = response.headers["#{k}-html"]
|
249
250
|
res.push(
|
250
251
|
args.merge(
|
251
252
|
{
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: render_turbo_stream
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- christian
|
@@ -24,11 +24,11 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 7.0.4.3
|
27
|
-
description:
|
28
|
-
|
29
|
-
templates. Together with the turbo_power gem or custom
|
30
|
-
can run javascript. Redirects can now be handled easily
|
31
|
-
helpers for enabling request tests.
|
27
|
+
description: Handles translated flash messages, sets status, and renders partials
|
28
|
+
via Turbo Stream or Turbo::StreamsChannel directly from the controller. No need
|
29
|
+
to write *.turbo_stream.* templates. Together with the turbo_power gem or custom
|
30
|
+
turbo-stream actions, you can run javascript. Redirects can now be handled easily
|
31
|
+
and in multiple ways. Includes helpers for enabling request tests.
|
32
32
|
email:
|
33
33
|
- christian@sedlmair.ch
|
34
34
|
executables: []
|
@@ -38,6 +38,7 @@ files:
|
|
38
38
|
- README.md
|
39
39
|
- Rakefile
|
40
40
|
- app/controllers/render_turbo_stream/application_controller.rb
|
41
|
+
- app/controllers/render_turbo_stream/render_turbo_stream_render_controller.rb
|
41
42
|
- app/models/render_turbo_stream/cable_stream.rb
|
42
43
|
- app/views/render_turbo_stream.html.erb
|
43
44
|
- app/views/render_turbo_stream.turbo_stream.erb
|