render_turbo_stream 1.4.14 → 1.4.15
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 +4 -4
- data/README.md +11 -3
- data/lib/render_turbo_stream/turbo_cable_helpers.rb +43 -14
- data/lib/render_turbo_stream/version.rb +1 -1
- data/lib/render_turbo_stream.rb +35 -8
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ddf2d67786644e4776a58ead9c9bd705d54aa20e6c2a7f2a50383bcc59eacbc4
|
4
|
+
data.tar.gz: e5dce474494ad9076d96390ef06fca96b7303e821d2326e6d7b4a4b737f96670
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c86bcd83fb42ebd8286c981e3a1d80d7b37d8ab3ae524f91679a2a3734429692cb3be356199252bbabb9161d056214fd7a63454095259b8cc95ba9bd62eb341e
|
7
|
+
data.tar.gz: 8330c7f4b8d178d68a7be8f8b54d76a6de88676c17178a2e46878934d2c3ecedac4bc968ffa78828e7fa6288324551698f5def18aff073883de0ab3fee42d63a
|
data/README.md
CHANGED
@@ -155,9 +155,6 @@ def update
|
|
155
155
|
:turbo_frame_set_src,
|
156
156
|
'cars-box',
|
157
157
|
cars_path
|
158
|
-
],
|
159
|
-
[
|
160
|
-
#... more actions possible ...
|
161
158
|
]
|
162
159
|
]
|
163
160
|
)
|
@@ -165,6 +162,17 @@ def update
|
|
165
162
|
|
166
163
|
```
|
167
164
|
|
165
|
+
**Turbo::StreamsChannel**
|
166
|
+
|
167
|
+
Another workaround as described above, but with more advantages, is to use Turbo Streams, which are seamlessly integrated into `turbo rails`. See the [README](https://gitlab.com/sedl/renderturbostream/-/blob/main/README-cable.md).
|
168
|
+
If setup well and configured by
|
169
|
+
|
170
|
+
```ruby
|
171
|
+
config.x.render_turbo_stream.use_cable = true
|
172
|
+
```
|
173
|
+
|
174
|
+
The `turbo_stream_save` method, in the case of classical redirects (by redirect_to), would send the flashes plus additional partials, declared on arguments by `Turbo::StreamsChannel` in parallel to the redirect.
|
175
|
+
|
168
176
|
**Parameters for turbo_stream_save**
|
169
177
|
|
170
178
|
save_action,
|
@@ -1,22 +1,51 @@
|
|
1
1
|
module RenderTurboStream
|
2
|
-
|
2
|
+
module TurboCableHelpers
|
3
3
|
|
4
|
+
def turbo_cable_to_authenticated
|
5
|
+
if user_signed_in?
|
4
6
|
|
5
|
-
|
6
|
-
|
7
|
-
end
|
7
|
+
end
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
10
|
+
def turbo_cable_to_current_user(target, action, *args, partial: nil, locals: nil)
|
11
|
+
if Rails.env.test?
|
12
|
+
args = {
|
13
|
+
target: target,
|
14
|
+
action: action,
|
15
|
+
type: 'turbo-cable-partial',
|
16
|
+
args: args,
|
17
|
+
partial: partial,
|
18
|
+
locals: locals
|
19
|
+
}
|
20
|
+
h = response.headers.to_h
|
21
|
+
i = 1
|
22
|
+
loop do
|
23
|
+
k = "test-turbo-cable-#{i}"
|
24
|
+
unless h.keys.include?(k)
|
25
|
+
response.headers[k] = args.to_json
|
26
|
+
break
|
18
27
|
end
|
28
|
+
i += 1
|
19
29
|
end
|
20
|
-
|
30
|
+
end
|
31
|
+
if [:append, :prepend, :replace].include?(action.to_sym)
|
32
|
+
Turbo::StreamsChannel.send(
|
33
|
+
"broadcast_#{action}_to",
|
34
|
+
"current_user_#{helpers.current_user.id}",
|
35
|
+
*args,
|
36
|
+
target: target,
|
37
|
+
partial: partial,
|
38
|
+
locals: locals,
|
39
|
+
layout: false
|
40
|
+
)
|
41
|
+
else
|
42
|
+
# Turbo::StreamsChannel.broadcast_action_to(
|
43
|
+
# "current_user_#{helpers.current_user.id}",
|
44
|
+
# action:'toggle_css_class',
|
45
|
+
# 'colored-element'
|
46
|
+
# )
|
47
|
+
end
|
21
48
|
end
|
49
|
+
|
50
|
+
end
|
22
51
|
end
|
data/lib/render_turbo_stream.rb
CHANGED
@@ -16,6 +16,7 @@ module RenderTurboStream
|
|
16
16
|
id: nil, # if nil: no partial is rendered
|
17
17
|
partial: nil, # example: 'customers/form' default: "#{controller_path}/#{id}"
|
18
18
|
action: 'replace', # options: append, prepend
|
19
|
+
flash_action: action_name, # options: 'update', 'create', otherwise you have to declare a translation in config/locales like "activerecord.success.#{flash_action}" and "activerecord.errors.#{flash_action}"
|
19
20
|
locals: {}, # locals used by the partial
|
20
21
|
streams_on_success: [
|
21
22
|
{
|
@@ -67,35 +68,47 @@ module RenderTurboStream
|
|
67
68
|
if save_action
|
68
69
|
flash_notices = if flashes_on_success.present?
|
69
70
|
flashes_on_success
|
70
|
-
elsif
|
71
|
+
elsif flash_action.to_s == 'create'
|
71
72
|
str = I18n.t(
|
72
73
|
'activerecord.success.successfully_created',
|
73
74
|
default: '%<model_name>s successfully created'
|
74
75
|
)
|
75
76
|
[format(str, model_name: model_name)]
|
76
|
-
elsif
|
77
|
+
elsif flash_action.to_s == 'update'
|
77
78
|
str = I18n.t(
|
78
79
|
'activerecord.success.successfully_updated',
|
79
80
|
default: '%<model_name>s successfully updated'
|
80
81
|
)
|
81
82
|
[format(str, model_name: model_name)]
|
83
|
+
else
|
84
|
+
str = I18n.t(
|
85
|
+
"activerecord.success.#{flash_action}",
|
86
|
+
default: '%<model_name>s successfully updated'
|
87
|
+
)
|
88
|
+
[format(str, model_name: model_name)]
|
82
89
|
end
|
83
90
|
flash_alerts = []
|
84
91
|
else
|
85
92
|
flash_alerts = if flashes_on_error.present?
|
86
93
|
flashes_on_error
|
87
|
-
elsif
|
94
|
+
elsif flash_action.to_s == 'create'
|
88
95
|
str = I18n.t(
|
89
96
|
'activerecord.errors.messages.could_not_create',
|
90
97
|
default: '%<model_name>s could not be created'
|
91
98
|
)
|
92
99
|
[format(str, model_name: model_name)]
|
93
|
-
elsif
|
100
|
+
elsif flash_action.to_s == 'update'
|
94
101
|
str = I18n.t(
|
95
102
|
'activerecord.errors.messages.could_not_update',
|
96
103
|
default: '%<model_name>s could not be updated'
|
97
104
|
)
|
98
105
|
[format(str, model_name: model_name)]
|
106
|
+
else
|
107
|
+
str = I18n.t(
|
108
|
+
"activerecord.errors.messages.#{flash_action}",
|
109
|
+
default: '%<model_name>s could not be updated'
|
110
|
+
)
|
111
|
+
[format(str, model_name: model_name)]
|
99
112
|
end
|
100
113
|
flash_notices = []
|
101
114
|
end
|
@@ -145,10 +158,24 @@ module RenderTurboStream
|
|
145
158
|
])
|
146
159
|
elsif save_action && redirect_on_success_to.present?
|
147
160
|
response.status = 302
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
161
|
+
if Rails.configuration.x.render_turbo_stream.use_cable && helpers.user_signed_in?
|
162
|
+
streams.each do |s|
|
163
|
+
next unless s.is_a?(Hash)
|
164
|
+
Rails.logger.debug(" • Send by Cable => «#{s}»")
|
165
|
+
turbo_cable_to_current_user(
|
166
|
+
s[:id],
|
167
|
+
:prepend,
|
168
|
+
partial: s[:partial],
|
169
|
+
locals: s[:locals]
|
170
|
+
)
|
171
|
+
end
|
172
|
+
|
173
|
+
else
|
174
|
+
flash[:alert] = flash_alerts
|
175
|
+
flash[:notice] = flash_notices
|
176
|
+
Rails.logger.debug(" • Set flash[:alert] => #{flash_alerts}") if flash_alerts.present?
|
177
|
+
Rails.logger.debug(" • Set flash[:notice] => #{flash_notices}") if flash_notices.present?
|
178
|
+
end
|
152
179
|
redirect_to redirect_on_success_to
|
153
180
|
|
154
181
|
else
|