render_turbo_stream 4.3.7 → 4.3.9

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: ea7913deb2cafba8983dac02703b99af22fdc6258a065ee9a133764bb7b5eb65
4
- data.tar.gz: af7c12ff7132866f47467069cd1acaa7a3cba770ad71e52e723f346c7f1ded48
3
+ metadata.gz: b7267e8e4cbcb083f89379501417b50ee9452f573e9df2ca3db731d89b314307
4
+ data.tar.gz: 33c771a46e9c0cb0210b2cf0253fbfb6629652ba549857309c2c448a1872816d
5
5
  SHA512:
6
- metadata.gz: 84fb7d4e0a4ed16afe2fffbaa1552e7c805094eec885ba05bb9d5a7339aae0269a027d5194f238bb0e5f4913bcabc9e5c7916067b7e35fbb9f297e5c6e4f8e80
7
- data.tar.gz: 5c1889def50f6eca8fa5c733792bb1cd721a399cc7ff3c1a20ff6de7eac988894a42ac9933c16a3ea59fcb68b0fda53a3a39f1549c195c6de5bdd099f1edefed
6
+ metadata.gz: 2e548748b5db6f7bd0ebdec11f2c56a1c2828fb3c711162a76d5ca9b978c057118604a4a8d8ce7bb42565ce00f9123770ada2aa619e73f80f3152e5a365c5421
7
+ data.tar.gz: df6c1bcd7dc3b140ab73506ba8d94a43d62514192734b6024b3439c90fe8b0b7582af1dde2bb494a3e0e24314ad3e102e00387bc3fed49f3fc7fb954e22472ee
data/README.md CHANGED
@@ -49,9 +49,10 @@ spec/rails_helper.rb (We are using rspec instead of minitest)
49
49
 
50
50
  ```ruby
51
51
  RSpec.configure do |config|
52
- ...
52
+ #...
53
53
  config.include RenderTurboStream::Test::Request::Helpers, type: :request
54
54
  config.include RenderTurboStream::Test::Request::ChannelHelpers, type: :request
55
+ config.include RenderTurboStream::Test::System::Helpers, type: :system
55
56
  end
56
57
  ```
57
58
 
@@ -105,63 +106,22 @@ The Rails team has integrated `ActionCable` as `Turbo::StreamsChannel` into `Tur
105
106
 
106
107
  # Usage
107
108
 
108
- `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:
109
+ **render_turbo_stream**
109
110
 
110
- ```ruby
111
-
112
- def create
113
- turbo_stream_save(
114
- @article.update(article_params),
115
- if_success_redirect_to: articles_path,
116
- partial: 'form'
117
- )
118
- end
119
- ```
120
-
121
- Note that if you want to redirect to show path, but the save action fails, `article_path(@article)` would throw an error. For this, the arguments `if_success_turbo_redirect_to` and `if_success_redirect_to` accept procs that are evaluated only on success. So your line would look like this `if_success_redirect_to: ->{article_path(@article)}`.
122
-
123
- 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:
124
-
125
- ```haml
126
- = turbo_target_tag do
127
- = simple_form ...
128
- ```
129
-
130
- This will generate an element like `<turbo-target id="new-article-form">`.
131
-
132
- And the form should work!
133
-
134
- 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.
135
-
136
- Technical details? see later in this README.
137
-
138
- As mentioned above, the turbo stream actions are handled by the `render_turbo_stream` controller helper:
111
+ The basic idea for this gem is **to have all the decisions/logic within the controller** so that the views are just views. So in the end we have an array of turbo stream actions that are handled by the gem. In normal cases, `*.turbo_stream.*` templates are not needed anymore.
139
112
 
140
113
  ```ruby
141
114
  render_turbo_stream(
142
115
  [
143
116
  {
144
- id: 'customer-form',
145
- partial: 'customers/customer_form'
117
+ partial: 'customers/customer_form' #=> The target id can be defined within the form and retrieved during rendering. The default :action is :replace
146
118
  },
147
119
  {
148
120
  id: 'flash-wrapper',
149
121
  partial: 'layouts/flashes',
150
- action: :prepend
151
- }
152
- ]
153
- )
154
- ```
155
-
156
- If the update succeeds, it will do a `redirect_to` action from turbo_power
157
-
158
- **More options**
159
-
160
- `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:
161
-
162
- ```ruby
163
- render_turbo_stream(
164
- [
122
+ action: :prepend,
123
+ locals: { success: true }
124
+ },
165
125
  [
166
126
  :push_state,
167
127
  '/articles/my-special-path'
@@ -175,7 +135,35 @@ render_turbo_stream(
175
135
  )
176
136
  ```
177
137
 
178
- Under the hood, inside a `*.turbo_stream.erb` template, it does the following: `= turbo_stream.send args.first, *(args[1..-1])`
138
+ this enables:
139
+
140
+ - turbo-stream or custom javascript actions from turbo and all actions from [turbo_power](https://github.com/marcoroth/turbo_power)
141
+ - Testing helpers (request-testing), as mentioned later
142
+ - Helpers for handling the target id, as mentioned later
143
+
144
+ If element is an array instead of a hash, it does this under the hood: `= turbo_stream.send args.first, *(args[1..-1])`. If element is a hash, it is interpreted as a partial or template to render.
145
+
146
+ **turbo_stream_save**
147
+
148
+ `turbo_stream_save` is a special method for `update` or `create` functions with `turbo_stream`. A controller action for update might look like this:
149
+
150
+ ```ruby
151
+
152
+ def create
153
+ turbo_stream_save(
154
+ @article.update(article_params),
155
+ if_success_redirect_to: articles_path,
156
+ partial: 'form'
157
+ )
158
+ end
159
+ ```
160
+ - uses `render_turbo_stream` or [render_to_me](https://gitlab.com/sedl/renderturbostream/-/blob/main/README-channels.md) (if allowed by config) under the hood
161
+ - Generates translated flash messages
162
+ - Handles redirects: Either classic redirect, which would never break out of a turbo-frame or the redirect from [turbo_power](https://github.com/marcoroth/turbo_power) which does a full-page-reload.
163
+
164
+
165
+ Note that if you want to redirect to show path, but the save action fails, `article_path(@article)` would throw an error. For this, the arguments `if_success_turbo_redirect_to` and `if_success_redirect_to` accept procs that are evaluated only on success. So your line would look like this `if_success_redirect_to: ->{article_path(@article)}`.
166
+
179
167
 
180
168
  **Config: allow_channel_to_me_for_turbo_stream_save**
181
169
 
@@ -185,6 +173,19 @@ If this config is set to true, Turbo::StreamsChannel is installed and a current
185
173
 
186
174
  If an `if_success_redirect_to` argument is provided and the save action was successful, `turbo_stream_save` would send the partials by channel.
187
175
 
176
+
177
+ **turbo_target_tag**
178
+
179
+ 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:
180
+
181
+ ```haml
182
+ = turbo_target_tag do
183
+ = simple_form ...
184
+ ```
185
+
186
+ - Generates an element like `<turbo-target id="new-article-form">`
187
+ - If no target-id is defined on `turbo_stream_save` or `render_turbo_stream` the renderer will grab inside the rendered content for a turbo-frame or turbo-target and get the target-id from there, so you can avoid having to define the same target-id on multiple places.
188
+
188
189
  **Target-ID**
189
190
 
190
191
  The target ID for turbo has to be unique for sure, 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`:
@@ -204,6 +205,8 @@ target_id
204
205
  target_id(Customer.first) #=> 'customer-1-my-form'
205
206
  target_id( [Customer.first, Article.last, 'hello'] ) #=> 'customer-1-article-7-hello-my-form'
206
207
  target_id('hi-joe') #=> 'hi-joe'
208
+
209
+ target_id_css('hi-joe') #=> '#hi-joe'
207
210
  ```
208
211
 
209
212
  Why include the filename in a html-id? Because for turbo its likely to have multiple cases for the same object on the same page, for example: _form, _show, _password. These can all be the same customer.
@@ -287,18 +290,6 @@ config.x.render_turbo_stream.first_argument_is_html_id = %[replace append prepen
287
290
 
288
291
  This setting is relevant for testing helpers.
289
292
 
290
- # Personal note
291
-
292
- The World Wide Web, founded around 1990 by [Tim Berners-Lee](https://en.wikipedia.org/wiki/Tim_Berners-Lee), was an html response from the server.
293
-
294
- Frameworks like Angular, Ember, React, Vue brought a much better user experience, so called "single page applications". But they changed a paradigma: Now Javascript was the processor of HTML, which is far from the [Progressive Enhancement](https://developer.mozilla.org/en-US/docs/Glossary/Progressive_Enhancement).
295
-
296
- 2021 DHH annouced a great milestone in web development with [Rails 7, "Fulfilling a Vision"](https://rubyonrails.org/2021/12/15/Rails-7-fulfilling-a-vision). The Rails core now has done a big step, for bringing a user experience like a single page app, but by reducing javascript. This is a modern back to the roots. Thank you Rails Team!
297
-
298
- But as of 2023, I searched for months and found nothing to solve all these details to make this Vision really work for rails. Now i am really happy and i cannot imagine working without Turbo!
299
-
300
- Let us build on this vision and make rails what it has always been: A framework ahead of its time!
301
-
302
293
  # Contributing
303
294
 
304
295
  Contributors welcome.
@@ -89,6 +89,10 @@ module RenderTurboStream
89
89
  libs.target_id(relative_view_path, object)
90
90
  end
91
91
 
92
+ def target_id_css(relative_view_path, object)
93
+ "##{target_id(relative_view_path, object)}"
94
+ end
95
+
92
96
  end
93
97
  end
94
98
  end
@@ -0,0 +1,18 @@
1
+ module RenderTurboStream
2
+ module Test
3
+ module System
4
+ module Helpers
5
+
6
+ def target_id(relative_view_path, object)
7
+ libs = RenderTurboStream::Libs
8
+ libs.target_id(relative_view_path, object)
9
+ end
10
+
11
+ def target_id_css(relative_view_path, object)
12
+ "##{target_id(relative_view_path, object)}"
13
+ end
14
+
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,3 +1,3 @@
1
1
  module RenderTurboStream
2
- VERSION = "4.3.7"
2
+ VERSION = "4.3.9"
3
3
  end
@@ -4,6 +4,7 @@ require 'render_turbo_stream/engine'
4
4
 
5
5
  require 'render_turbo_stream/test/request/channel_helpers'
6
6
  require 'render_turbo_stream/test/request/helpers'
7
+ require 'render_turbo_stream/test/system/helpers'
7
8
  require 'render_turbo_stream/test/request/libs'
8
9
 
9
10
  require 'render_turbo_stream/controller_helpers'
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.7
4
+ version: 4.3.9
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-19 00:00:00.000000000 Z
11
+ date: 2023-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -61,6 +61,7 @@ files:
61
61
  - lib/render_turbo_stream/test/request/channel_helpers.rb
62
62
  - lib/render_turbo_stream/test/request/helpers.rb
63
63
  - lib/render_turbo_stream/test/request/libs.rb
64
+ - lib/render_turbo_stream/test/system/helpers.rb
64
65
  - lib/render_turbo_stream/version.rb
65
66
  - lib/render_turbo_stream/view_helpers.rb
66
67
  - lib/tasks/render_turbo_stream_tasks.rake