render_turbo_stream 4.3.2 → 4.3.4

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: 9f3b950cec2aadef6d1acea45b6d15e0bb10a2b6c47adaf49bad4e4ed909ffba
4
- data.tar.gz: a1ec0bf02392b4cebf5416aed80723f9a2d2c34f2996262bc451733dc56c9442
3
+ metadata.gz: de051d73d56102cedd1c2a29e0794041dbe3cd18577300002005ac050eb085e4
4
+ data.tar.gz: 0224b8f893ecb5408f917b4fe201650b54a815bab020fc3a549cc869da9615a6
5
5
  SHA512:
6
- metadata.gz: f9ca2767f8ac34efd99bdb31f3abb5bc5654c0d130482d3a999f815b435a08cb3eecce4eec88272919feefc0e4e5584fa7a61970b3e9535ba2550945c10bac9a
7
- data.tar.gz: 97b79f7d79095632faa08dc7838e959188e3342ce9eb62168e0a2e2cacac1de67eb6279dd2fecb31b289aa4d6a84799f3a129c3ee6a7692db4c6a16f4e8ee9c5
6
+ metadata.gz: f756884588c93fcf1052cfcb358fb69e7d996b746aa79d8cf3b134a91037e6acc3ea34253184491b3383a79bf87c2e8b046a4b67b70db950bb1182e41f8c49ca
7
+ data.tar.gz: 23bcf3db97515ace21d9f84fde860941326db0001c9388e44cb152a08f6288c64e72e949d884fd9be9f06ba3fc8cfe4a2ce9231f877ecc0c8536642e228c65b5
data/README.md CHANGED
@@ -31,6 +31,8 @@ gem 'render_turbo_stream'
31
31
  bundle install
32
32
  ```
33
33
 
34
+ **Include the helpers**
35
+
34
36
  ApplicationController
35
37
 
36
38
  ```ruby
@@ -40,10 +42,10 @@ include RenderTurboStream::ControllerHelpers
40
42
  Create a Initializer
41
43
 
42
44
  ```ruby
43
- ActionView::Base.send :include, RenderTurboStream::ChannelViewHelpers
45
+ ActionView::Base.send :include, RenderTurboStream::ViewHelpers
44
46
  ```
45
47
 
46
- spec/rails_helper.rb
48
+ spec/rails_helper.rb (We are using rspec instead of minitest)
47
49
 
48
50
  ```ruby
49
51
  RSpec.configure do |config|
@@ -61,6 +63,8 @@ Required Configurations for Flash Partial
61
63
  config.x.render_turbo_stream.flash_partial = 'layouts/flash'
62
64
  config.x.render_turbo_stream.flash_target_id = 'flash-box'
63
65
  config.x.render_turbo_stream.flash_turbo_action = 'prepend'
66
+
67
+ # In case of redirection a flash cannot be sent by TurboStream
64
68
  config.x.render_turbo_stream.allow_channel_to_me_for_turbo_stream_save = true
65
69
  ```
66
70
 
@@ -181,21 +185,27 @@ If an `if_success_redirect_to` argument is provided and the save action was succ
181
185
 
182
186
  **Target-ID**
183
187
 
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`:
188
+ 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`:
185
189
 
186
190
  ```ruby
187
191
  # target_id(virtual_view_path, object)
188
192
  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
193
  ```
192
- View-helpers, assuming we are inside `customers/_form`:
194
+
195
+ View-helper: Assuming we are inside `customers/_my_form`:
193
196
 
194
197
  ```ruby
195
- target_id #=> this checks automatically for @customer => if present => 'customer-1-form'
196
- target_id(Customer.new) #=> 'new-customer-form'
198
+ #=> Assuming controller-name "customers" and variable @customer is present
199
+ target_id
200
+ #=> 'customer-1-my-form'
201
+
202
+ target_id(Customer.first) #=> 'customer-1-my-form'
203
+ target_id( [Customer.first, Article.last, 'hello'] ) #=> 'customer-1-article-7-hello-my-form'
204
+ target_id('hi-joe') #=> 'hi-joe'
197
205
  ```
198
206
 
207
+ 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.
208
+
199
209
  **Target-ID: Avoid the same definition in multiple places**
200
210
 
201
211
  Without this gem a turbo action would be wrapped within two frames, for example:
@@ -203,7 +213,6 @@ Without this gem a turbo action would be wrapped within two frames, for example:
203
213
  ```haml
204
214
  = turbo_stream.replace 'target-id' do
205
215
  = render 'a partial'
206
-
207
216
  ```
208
217
 
209
218
  and within a partial:
@@ -7,7 +7,6 @@
7
7
  <% if args.is_a?(Array) %>
8
8
 
9
9
  <% ctl = "turbo_stream.#{args.first}, #{args[1..-1].join(', ')}" %>
10
- <% Rails.logger.debug(" • render-turbo-stream => #{ctl}") %>
11
10
  <%= turbo_stream.send args.first, *(args[1..-1]) %>
12
11
 
13
12
 
@@ -26,7 +25,9 @@
26
25
 
27
26
  <% unless args[:target].present? %>
28
27
  <% args[:target] = RenderTurboStream::Libs.fetch_arguments_from_rendered_string(rendered_html)[:target] %>
29
- <% unless args[:target].present? %>
28
+ <% if args[:target].present? %>
29
+ <% Rails.logger.debug(" • Target #{args[:target]} found in #{args[:partial]}") %>
30
+ <% else %>
30
31
  <% raise 'No target specified by arguments and no target found inside the rendered partial' %>
31
32
  <% end %>
32
33
  <% end %>
@@ -38,7 +39,6 @@
38
39
 
39
40
 
40
41
  <% if args[:action].present? %>
41
- <% Rails.logger.debug(" • render-turbo-stream #{args[:action].upcase} => #{info}") %>
42
42
  <%= turbo_stream.send args[:action].to_sym, target_id do %>
43
43
 
44
44
  <%= rendered_html %>
@@ -37,6 +37,7 @@ module RenderTurboStream
37
37
  r = RenderTurboStream::Libs.fetch_arguments_from_rendered_string(rendered_html)
38
38
  target_id = r[:target_id]
39
39
  target = r[:target]
40
+ Rails.logger.debug(" • Target #{r[:target]} found in #{partial.to_s + template.to_s}")
40
41
  end
41
42
  end
42
43
 
@@ -79,6 +79,7 @@ module RenderTurboStream
79
79
  response.status = 302
80
80
  flash[:alert] = flashes[:alerts]
81
81
  flash[:notice] = flashes[:notices]
82
+ Rails.logger.debug(" • Successful saved && Redirect by «turbo_redirect_to»")
82
83
  Rails.logger.debug(" • Set flash[:alert] => #{flashes[:alerts]}") if flashes[:alerts].present?
83
84
  Rails.logger.debug(" • Set flash[:notice] => #{flashes[:notices]}") if flashes[:notices].present?
84
85
  render_turbo_stream([
@@ -91,18 +92,21 @@ module RenderTurboStream
91
92
  elsif save_action && if_success_redirect_to.present?
92
93
  response.status = 303
93
94
  if allow_channel
94
- Rails.logger.debug(" • Sent #{streams.length} actions through Turbo::StreamsChannel because allowed in configs.")
95
+ Rails.logger.debug(" • Send actions through Turbo::StreamsChannel")
95
96
  c_libs = RenderTurboStream::ChannelLibs.new(response)
96
97
  c_libs.send_actions_to_channel("authenticated-user-#{helpers.current_user.id}", streams, @render_turbo_stream_evaluate_instance_variables)
97
98
  else
98
99
  flash[:alert] = flashes[:alerts]
99
100
  flash[:notice] = flashes[:notices]
101
+ Rails.logger.debug(" • Turbo::StreamsChannel NOT ALLOWED BY CONFIGS!")
100
102
  Rails.logger.debug(" • Set flash[:alert] => #{flashes[:alerts]}") if flashes[:alerts].present?
101
103
  Rails.logger.debug(" • Set flash[:notice] => #{flashes[:notices]}") if flashes[:notices].present?
104
+ Rails.logger.debug(" • Could not send #{streams.length} actions => #{streams}")
102
105
  end
103
106
  redirect_to if_success_redirect_to
104
107
 
105
108
  else
109
+ Rails.logger.debug(" • Respond by TurboStream in #{streams.length} #{'action'.pluralize(streams.length)}")
106
110
  streams += libs.generate_action(controller_path, target_id, action, partial, (partial ? nil : action_name), locals)
107
111
  render_turbo_stream(streams)
108
112
 
@@ -114,6 +118,7 @@ module RenderTurboStream
114
118
 
115
119
  ary = []
116
120
  array.each do |pr|
121
+ cmd = nil
117
122
  if !pr.present?
118
123
  Rails.logger.warn " WARNING render_turbo_stream: Empty element inside attributes: «#{array}»"
119
124
  elsif pr.is_a?(Hash)
@@ -133,13 +138,15 @@ module RenderTurboStream
133
138
  end
134
139
  r[:type] = 'stream-partial'
135
140
 
136
- ary.push(r)
141
+ cmd = r
137
142
  elsif pr.is_a?(Array)
138
143
  raise "array has to contain at least one element: #{pr}" unless pr.first.present?
139
- ary.push(pr)
144
+ cmd = pr
140
145
  else
141
146
  raise "ERROR render_turbo_stream invalid type: Only hash or array allowed"
142
147
  end
148
+ ary.push(cmd) if cmd
149
+ Rails.logger.debug(" • Stream => #{cmd}")
143
150
  end
144
151
 
145
152
  if request.format.to_sym == :turbo_stream
@@ -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
+ raise("target_id / argument ID: Only String or object with method :id accepted")
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.2"
2
+ VERSION = "4.3.4"
3
3
  end
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: 4.3.2
4
+ version: 4.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - christian