render_turbo_stream 4.3.1 → 4.3.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 +42 -9
- data/lib/render_turbo_stream/controller_helpers.rb +2 -2
- data/lib/render_turbo_stream/libs.rb +25 -1
- data/lib/render_turbo_stream/test/request/helpers.rb +5 -0
- data/lib/render_turbo_stream/version.rb +1 -1
- data/lib/render_turbo_stream/view_helpers.rb +20 -0
- data/lib/render_turbo_stream.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f3b950cec2aadef6d1acea45b6d15e0bb10a2b6c47adaf49bad4e4ed909ffba
|
4
|
+
data.tar.gz: a1ec0bf02392b4cebf5416aed80723f9a2d2c34f2996262bc451733dc56c9442
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9ca2767f8ac34efd99bdb31f3abb5bc5654c0d130482d3a999f815b435a08cb3eecce4eec88272919feefc0e4e5584fa7a61970b3e9535ba2550945c10bac9a
|
7
|
+
data.tar.gz: 97b79f7d79095632faa08dc7838e959188e3342ce9eb62168e0a2e2cacac1de67eb6279dd2fecb31b289aa4d6a84799f3a129c3ee6a7692db4c6a16f4e8ee9c5
|
data/README.md
CHANGED
@@ -37,6 +37,12 @@ ApplicationController
|
|
37
37
|
include RenderTurboStream::ControllerHelpers
|
38
38
|
```
|
39
39
|
|
40
|
+
Create a Initializer
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
ActionView::Base.send :include, RenderTurboStream::ChannelViewHelpers
|
44
|
+
```
|
45
|
+
|
40
46
|
spec/rails_helper.rb
|
41
47
|
|
42
48
|
```ruby
|
@@ -102,15 +108,28 @@ The Rails team has integrated `ActionCable` as `Turbo::StreamsChannel` into `Tur
|
|
102
108
|
def update
|
103
109
|
turbo_stream_save(
|
104
110
|
@article.update(article_params),
|
105
|
-
|
106
|
-
target_id: 'customer-form',
|
111
|
+
if_success_redirect_to: articles_path,
|
107
112
|
partial: 'form'
|
108
113
|
)
|
109
114
|
end
|
110
115
|
```
|
111
|
-
This will set a status, generate a flash message, and run `render_turbo_stream`. If it fails, it would result in something like this:
|
112
116
|
|
113
|
-
|
117
|
+
Assuming we want to build a form that works within a turbo-frame, including flash messages and redirects, we have to build all that inside a `turbo-frame`, and, within that we need a target-id, that must not be a turbo-frame. For that, there is a helper:
|
118
|
+
|
119
|
+
```haml
|
120
|
+
= turbo_target_tag do
|
121
|
+
= simple_form ...
|
122
|
+
```
|
123
|
+
|
124
|
+
This will generate an element like `<turbo-target id="new-article-form">`.
|
125
|
+
|
126
|
+
And the form should work!
|
127
|
+
|
128
|
+
If the update succeeds, it sends a flash message by channel(!), because stream cannot work alongside redirect, and redirects to articles#index. If it fails, it sends a flash message by stream and replaces the form tag by stream so that the invalid fields are marked red and you can see the errors inside the form.
|
129
|
+
|
130
|
+
Technical details? see later in this README.
|
131
|
+
|
132
|
+
As mentioned above, the turbo stream actions are handled by the `render_turbo_stream` controller helper:
|
114
133
|
|
115
134
|
```ruby
|
116
135
|
render_turbo_stream(
|
@@ -130,9 +149,6 @@ render_turbo_stream(
|
|
130
149
|
|
131
150
|
If the update succeeds, it will do a `redirect_to` action from turbo_power
|
132
151
|
|
133
|
-
|
134
|
-
**locals**: The hash for locals goes through a `symbolize_keys`, so you need to use locals in used partials like this: `locals[:message]`.
|
135
|
-
|
136
152
|
**More options**
|
137
153
|
|
138
154
|
`render_turbo_stream` interprets a hash as a partial to be sent by `turbo_stream` and an array as a command to be sent. This allows you to perform most actions from, e.g., [turbo_power](https://github.com/marcoroth/turbo_power). An example of adding a css class to an html element and updating the browser history would look like this:
|
@@ -163,7 +179,24 @@ If this config is set to true, Turbo::StreamsChannel is installed and a current
|
|
163
179
|
|
164
180
|
If an `if_success_redirect_to` argument is provided and the save action was successful, `turbo_stream_save` would send the partials by channel.
|
165
181
|
|
166
|
-
**
|
182
|
+
**Target-ID**
|
183
|
+
|
184
|
+
The target ID for turbo has to be safely unique, and it has to be nice, because at least during testing the developer has to deal with it. Since the default ID builder `dom_id` is too simple for this, there are some helpers. How it works is best shown by the `request-test helper target_id`:
|
185
|
+
|
186
|
+
```ruby
|
187
|
+
# target_id(virtual_view_path, object)
|
188
|
+
target_id('customers/_form', Customer.new) #=> 'new-customer-form'
|
189
|
+
target_id('customers/_form', Customer.first) #=> 'customer-1-form'
|
190
|
+
target_id('customers/_form', nil) #=> 'customers-form'
|
191
|
+
```
|
192
|
+
View-helpers, assuming we are inside `customers/_form`:
|
193
|
+
|
194
|
+
```ruby
|
195
|
+
target_id #=> this checks automatically for @customer => if present => 'customer-1-form'
|
196
|
+
target_id(Customer.new) #=> 'new-customer-form'
|
197
|
+
```
|
198
|
+
|
199
|
+
**Target-ID: Avoid the same definition in multiple places**
|
167
200
|
|
168
201
|
Without this gem a turbo action would be wrapped within two frames, for example:
|
169
202
|
|
@@ -187,7 +220,7 @@ This means that the target id must be defined in several places: inside a partia
|
|
187
220
|
In order to avoid this kind of tedious coding, the gem has a kind of fallback built in: If the argument `partial` is given, but the attribute `target_id` is not, the gem will get the target_id from the partial. The process is:
|
188
221
|
|
189
222
|
1. Render the partial with the provided locals
|
190
|
-
2. Grabs into the partial by Nokogiri and looks for the first `
|
223
|
+
2. Grabs into the partial by Nokogiri and looks for the first `turbo-frame` or `turbo-target` element, get the id and uses this as target_id.
|
191
224
|
3. If all that not is found it raises a exception
|
192
225
|
4. wraps the partial within the `turbo_stream.*` and sends this to the front.
|
193
226
|
|
@@ -12,7 +12,7 @@ module RenderTurboStream
|
|
12
12
|
if_success_redirect_to: nil, # does a regular redirect. Works if you are inside a turbo_frame and just want to redirect inside that frame BUT CANNOT STREAM OTHERS ACTIONS ON THE SAME RESPONSE https://github.com/rails/rails/issues/48056
|
13
13
|
if_success_turbo_redirect_to: nil, # does a full page redirect (break out of all frames by turbo_power redirect)
|
14
14
|
|
15
|
-
target_id: nil, # IF NIL: the gem grabs inside the rendered content for
|
15
|
+
target_id: nil, # IF NIL: the gem grabs inside the rendered content for turbo-frame tag or turbo-target (element from helper of this gem) tag and takes the id from there.
|
16
16
|
partial: nil, # if nil: the gem renders the default template by turbo-stream
|
17
17
|
action: 'replace', # options: append, prepend
|
18
18
|
locals: {},
|
@@ -91,7 +91,7 @@ module RenderTurboStream
|
|
91
91
|
elsif save_action && if_success_redirect_to.present?
|
92
92
|
response.status = 303
|
93
93
|
if allow_channel
|
94
|
-
Rails.logger.debug(" • Sent #{streams.length} actions through Turbo::StreamsChannel
|
94
|
+
Rails.logger.debug(" • Sent #{streams.length} actions through Turbo::StreamsChannel because allowed in configs.")
|
95
95
|
c_libs = RenderTurboStream::ChannelLibs.new(response)
|
96
96
|
c_libs.send_actions_to_channel("authenticated-user-#{helpers.current_user.id}", streams, @render_turbo_stream_evaluate_instance_variables)
|
97
97
|
else
|
@@ -36,7 +36,7 @@ module RenderTurboStream
|
|
36
36
|
def self.fetch_arguments_from_rendered_string(rendered_string)
|
37
37
|
noko = Nokogiri::HTML(rendered_string)
|
38
38
|
frame = noko.at_css('turbo-frame')
|
39
|
-
ele = noko.at_css('
|
39
|
+
ele = noko.at_css('turbo-target')
|
40
40
|
if frame.present?
|
41
41
|
{
|
42
42
|
target_id: frame[:id],
|
@@ -52,5 +52,29 @@ module RenderTurboStream
|
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
|
+
def self.target_id(virt_view_path, id)
|
56
|
+
if id.is_a?(String)
|
57
|
+
id
|
58
|
+
elsif !id
|
59
|
+
[
|
60
|
+
virt_view_path.gsub('/', '-').gsub('-_', '-')
|
61
|
+
].join('-')
|
62
|
+
else
|
63
|
+
unless id.methods.include?(:id)
|
64
|
+
raise("target_id / argument ID: Only String or object with method :id accepted")
|
65
|
+
end
|
66
|
+
(
|
67
|
+
(
|
68
|
+
id.id ?
|
69
|
+
[id.class.to_s.downcase, id.id] :
|
70
|
+
['new', id.class.to_s.downcase]
|
71
|
+
) +
|
72
|
+
[
|
73
|
+
virt_view_path.split('/').last.sub(/^_/, '')
|
74
|
+
]
|
75
|
+
).join('-')
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
55
79
|
end
|
56
80
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module RenderTurboStream
|
2
|
+
module ViewHelpers
|
3
|
+
|
4
|
+
def turbo_target_tag(id = nil, &block)
|
5
|
+
content_tag :'turbo-target', id: target_id(id) do
|
6
|
+
if block_given?
|
7
|
+
capture(&block)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def target_id(id = nil)
|
13
|
+
libs = RenderTurboStream::Libs
|
14
|
+
virt_path = self.instance_variable_get('@current_template').instance_variable_get('@virtual_path')
|
15
|
+
obj = (id ? id : eval("@#{controller_name.singularize}"))
|
16
|
+
libs.target_id(virt_path, obj)
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
data/lib/render_turbo_stream.rb
CHANGED
@@ -10,6 +10,7 @@ require 'render_turbo_stream/controller_helpers'
|
|
10
10
|
require 'render_turbo_stream/controller_channel_helpers'
|
11
11
|
|
12
12
|
require 'render_turbo_stream/channel_view_helpers'
|
13
|
+
require 'render_turbo_stream/view_helpers'
|
13
14
|
|
14
15
|
require 'render_turbo_stream/channel_libs'
|
15
16
|
require 'render_turbo_stream/controller_libs'
|
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: 4.3.
|
4
|
+
version: 4.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- christian
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-06-
|
11
|
+
date: 2023-06-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -62,6 +62,7 @@ files:
|
|
62
62
|
- lib/render_turbo_stream/test/request/helpers.rb
|
63
63
|
- lib/render_turbo_stream/test/request/libs.rb
|
64
64
|
- lib/render_turbo_stream/version.rb
|
65
|
+
- lib/render_turbo_stream/view_helpers.rb
|
65
66
|
- lib/tasks/render_turbo_stream_tasks.rake
|
66
67
|
homepage: https://gitlab.com/sedl/renderturbostream
|
67
68
|
licenses:
|