render_turbo_stream 4.3.3 → 4.3.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d66f0d9cb7dc4822dbcaf38e286b855386e8637b07a922db1714b14ab63d0c6d
4
- data.tar.gz: 017a6047f0c07a1f2847f7be92d810483c00690e1c31f0cb31581004d48f8f18
3
+ metadata.gz: dd429fcf5885a88fd9362d97a3af9c7cca947d173911b4552e16ef752edfc93a
4
+ data.tar.gz: 1070b77583f60c208f1f38f9b7b6963d4604710fcb8bba0dd2746c379ce47739
5
5
  SHA512:
6
- metadata.gz: a3853c69389798f965cb41e629fb208e6926b3a4ce2d5f0f925554cec41d470fa5963ad6d88dd1712e787be3db683f1e61cce7411c78f8dda574069f7d5a9b55
7
- data.tar.gz: 78719ff0ab9c7299639161ba2a966786d18dc79b9f401e8112b58656f2270436c51119c3eb6297f73c4df4e40dc7843bebe4496a9a5cbcc6d187b6898fa00245
6
+ metadata.gz: 7be3a88170fb10070ddee8cb81670c5684949961214dd855e724c1cabe9004615fbb2c64fadb3f3339f23a776b623d1c2d14efdd07c67557d647951797684db6
7
+ data.tar.gz: d303cf736a91ebb95d35a06023d487d1073f2fed926c5bf0e5a72ec4b29cd5a2cbc4d928a0df83364157d951ed9727cf6181f5425f69cf85b079cd4eb58e11c4
data/README.md CHANGED
@@ -109,7 +109,7 @@ The Rails team has integrated `ActionCable` as `Turbo::StreamsChannel` into `Tur
109
109
 
110
110
  ```ruby
111
111
 
112
- def update
112
+ def create
113
113
  turbo_stream_save(
114
114
  @article.update(article_params),
115
115
  if_success_redirect_to: articles_path,
@@ -118,6 +118,8 @@ def update
118
118
  end
119
119
  ```
120
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
+
121
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:
122
124
 
123
125
  ```haml
@@ -190,9 +192,6 @@ The target ID for turbo has to be unique for sure, and it has to be nice, becaus
190
192
  ```ruby
191
193
  # target_id(virtual_view_path, object)
192
194
  target_id('customers/_form', Customer.new) #=> 'new-customer-form'
193
- target_id('customers/_form', Customer.first) #=> 'customer-1-form'
194
- target_id('customers/_form', nil) #=> 'customers-form'
195
- target_id('customers/_form', 'hi-joe') #=> 'hi-joe'
196
195
  ```
197
196
 
198
197
  View-helper: Assuming we are inside `customers/_my_form`:
@@ -202,10 +201,13 @@ View-helper: Assuming we are inside `customers/_my_form`:
202
201
  target_id
203
202
  #=> 'customer-1-my-form'
204
203
 
205
- target_id(Customer.new)
206
- #=> 'new-customer-my-form'
204
+ target_id(Customer.first) #=> 'customer-1-my-form'
205
+ target_id( [Customer.first, Article.last, 'hello'] ) #=> 'customer-1-article-7-hello-my-form'
206
+ target_id('hi-joe') #=> 'hi-joe'
207
207
  ```
208
208
 
209
+ 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.
210
+
209
211
  **Target-ID: Avoid the same definition in multiple places**
210
212
 
211
213
  Without this gem a turbo action would be wrapped within two frames, for example:
@@ -213,7 +215,6 @@ Without this gem a turbo action would be wrapped within two frames, for example:
213
215
  ```haml
214
216
  = turbo_stream.replace 'target-id' do
215
217
  = render 'a partial'
216
-
217
218
  ```
218
219
 
219
220
  and within a partial:
@@ -82,15 +82,23 @@ module RenderTurboStream
82
82
  Rails.logger.debug(" • Successful saved && Redirect by «turbo_redirect_to»")
83
83
  Rails.logger.debug(" • Set flash[:alert] => #{flashes[:alerts]}") if flashes[:alerts].present?
84
84
  Rails.logger.debug(" • Set flash[:notice] => #{flashes[:notices]}") if flashes[:notices].present?
85
+
86
+ if if_success_turbo_redirect_to.is_a?(Proc)
87
+ redirect_path = if_success_turbo_redirect_to.call
88
+ else
89
+ redirect_path = if_success_turbo_redirect_to
90
+ end
91
+
85
92
  render_turbo_stream([
86
93
  [
87
94
  :redirect_to,
88
- if_success_turbo_redirect_to
95
+ redirect_path
89
96
  ]
90
97
  ])
91
98
 
92
99
  elsif save_action && if_success_redirect_to.present?
93
100
  response.status = 303
101
+
94
102
  if allow_channel
95
103
  Rails.logger.debug(" • Send actions through Turbo::StreamsChannel")
96
104
  c_libs = RenderTurboStream::ChannelLibs.new(response)
@@ -103,7 +111,11 @@ module RenderTurboStream
103
111
  Rails.logger.debug(" • Set flash[:notice] => #{flashes[:notices]}") if flashes[:notices].present?
104
112
  Rails.logger.debug(" • Could not send #{streams.length} actions => #{streams}")
105
113
  end
106
- redirect_to if_success_redirect_to
114
+ if if_success_redirect_to.is_a?(Proc)
115
+ redirect_to if_success_redirect_to.call
116
+ else
117
+ redirect_to if_success_redirect_to
118
+ end
107
119
 
108
120
  else
109
121
  Rails.logger.debug(" • Respond by TurboStream in #{streams.length} #{'action'.pluralize(streams.length)}")
@@ -53,26 +53,34 @@ module RenderTurboStream
53
53
  end
54
54
 
55
55
  def self.target_id(virt_view_path, id)
56
+
56
57
  if id.is_a?(String)
57
58
  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
59
+ elsif id
66
60
  (
67
- (
68
- id.id ?
69
- [id.class.to_s.downcase, id.id] :
70
- ['new', id.class.to_s.downcase]
71
- ) +
61
+ (id.is_a?(Array) ? id : [id]).map do |_id|
62
+
63
+ if _id.is_a?(String)
64
+ _id
65
+ elsif _id.methods.include?(:id)
66
+ (
67
+ _id.id ?
68
+ [_id.class.to_s.downcase, _id.id] :
69
+ ['new', _id.class.to_s.downcase]
70
+ ).join('-')
71
+ else
72
+ _id.to_s
73
+ end
74
+ end +
72
75
  [
73
76
  virt_view_path.split('/').last.sub(/^_/, '')
74
77
  ]
75
78
  ).join('-')
79
+
80
+ else
81
+ [
82
+ virt_view_path.gsub('/', '-').gsub('-_', '-')
83
+ ].join('-')
76
84
  end
77
85
  end
78
86
 
@@ -1,3 +1,3 @@
1
1
  module RenderTurboStream
2
- VERSION = "4.3.3"
2
+ VERSION = "4.3.5"
3
3
  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: 4.3.3
4
+ version: 4.3.5
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-17 00:00:00.000000000 Z
11
+ date: 2023-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails