render_turbo_stream 4.3.17 → 4.4.0

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: 91ec62a537df4902f5adcf7caf65a6b6a098365d2c983cda574a2d9ab1ffd22e
4
- data.tar.gz: 28b80ad8d49000bc97b8b4fb0d66e1810befd61f0812aeb00712f8836d2dca3d
3
+ metadata.gz: 2cfb040091b3e5fcee2eb48a126e707316ff2e88f56d5375cf491df3ec7008af
4
+ data.tar.gz: 0deefe1b7acb1528606695a512eaa596fc6d31c4874807398484213e22eef7e0
5
5
  SHA512:
6
- metadata.gz: 343b599092acb9ccb1dc2f046175af02a6771696f0883f573b9f5f99e3891ff86fe6064510e761a05deba57feb814d0a473da61f8842eea0ea4642fc16d33996
7
- data.tar.gz: ea135fc6460784cf9a2a7c5f738fe37e639e23645577b53b5e761443ca20bca141167eb6d68a8adcf466b7f90e4c4caea8aef55ba4838c3f29b9231670fd7139
6
+ metadata.gz: c7c1f91debd98ecede2af7bdab72cebea1218d2a47f80c5eb2e27872f54b9c93668ab37ad34721836f8f65b65c6a8fe06a175e666aff3b60c870f3a0449b50a0
7
+ data.tar.gz: 10e8f7ba4a595c97db9762747938e456e3c57df9bdaebcb44daa50f293c3f93bbfc5051f7739709577fb3281328d9f6c719481091fa66f3a769600be024ec35e
@@ -0,0 +1,43 @@
1
+ class Channel
2
+
3
+ def initialize(channel, partial: nil, template: nil, target_id: nil, action: :replace)
4
+ @channel = channel
5
+ @partial = partial
6
+ @template = template
7
+ @target_id = target_id
8
+ @action = action
9
+ @test_responses = []
10
+ end
11
+
12
+ def target_id=(target_id)
13
+ @target_id = target_id
14
+ end
15
+
16
+ def action=(action)
17
+ @action = action
18
+ end
19
+
20
+ def send(locals: {})
21
+ libs = RenderTurboStream::ChannelLibs.new
22
+ libs.render_to_channel(
23
+ @channel,
24
+ RenderTurboStream::Libs.target_id_to_target(@target_id),
25
+ @action,
26
+ template: @template,
27
+ partial: @partial,
28
+ locals: locals
29
+ )
30
+ @test_responses = libs.test_responses
31
+ end
32
+
33
+ def test_responses
34
+ @test_responses
35
+ end
36
+
37
+ private
38
+
39
+ def validate_params
40
+ raise 'Arguments partial and template cannot both be specified' if @partial && @template
41
+ end
42
+
43
+ end
@@ -1,8 +1,8 @@
1
1
  module RenderTurboStream
2
- module ControllerChannelHelpers
2
+ module ChannelControllerHelpers
3
3
 
4
4
  def render_to_all(target_id = nil, action = :replace, partial: nil, template: nil, locals: nil)
5
- #evaluate_instance_variables
5
+ # evaluate_instance_variables
6
6
  render_to_channel(
7
7
  'all',
8
8
  target_id,
@@ -55,7 +55,7 @@ module RenderTurboStream
55
55
  )
56
56
  end
57
57
 
58
- def render_to_channel(channel, target_id = nil, action = :replace, partial: nil, template: nil, locals: nil, instance_variables: true)
58
+ def render_to_channel(channel, target_id = nil, action = :replace, partial: nil, template: nil, locals: nil)
59
59
 
60
60
  raise 'Arguments partial and template cannot both be specified' if partial && template
61
61
 
@@ -76,18 +76,21 @@ module RenderTurboStream
76
76
  @render_cable_template_was_called = true
77
77
  end
78
78
 
79
- #evaluate_instance_variables
79
+ # evaluate_instance_variables
80
80
 
81
- libs = RenderTurboStream::ChannelLibs.new(response)
82
- libs.render_to_channel(
81
+ libs = RenderTurboStream::ChannelLibs.new
82
+ loc = fetch_instance_variables(locals, _partial, _template, action)
83
+ res = libs.render_to_channel(
83
84
  channel,
84
85
  RenderTurboStream::Libs.target_id_to_target(target_id),
85
86
  action,
86
- self,
87
87
  template: _template,
88
- partial: _partial, locals: locals
88
+ partial: _partial,
89
+ locals: loc
89
90
  )
91
+ RenderTurboStream::Libs.set_test_responses(response, libs.test_responses)
90
92
 
93
+ res
91
94
  end
92
95
 
93
96
  def action_to_all(action, *arguments)
@@ -127,8 +130,10 @@ module RenderTurboStream
127
130
  end
128
131
 
129
132
  def action_to_channel(channel, command, arguments)
130
- libs = RenderTurboStream::ChannelLibs.new(response)
131
- libs.action_to_channel(channel, command, arguments)
133
+ libs = RenderTurboStream::ChannelLibs.new
134
+ r = libs.action_to_channel(channel, command, arguments)
135
+ RenderTurboStream::Libs.set_test_responses(response, libs.test_responses)
136
+ r
132
137
  end
133
138
 
134
139
  def turbo_channel_save(
@@ -146,8 +151,7 @@ module RenderTurboStream
146
151
  add_notices: nil, # array of strings
147
152
  add_alerts: nil,
148
153
 
149
- flash_controller_action_name: action_name, # options: 'update', 'create', otherwise you have to declare a translation in config/locales like "activerecord.success.#{flash_controller_action_name}" and "activerecord.errors.#{flash_controller_action_name}"
150
- instance_variables: true # options: if false, nothing happens, if true: Instance variables that matching to controller name like @article or @articles are set to locals. By Array you can add more, example: [:my_var]
154
+ flash_controller_action_name: action_name # options: 'update', 'create', otherwise you have to declare a translation in config/locales like "activerecord.success.#{flash_controller_action_name}" and "activerecord.errors.#{flash_controller_action_name}"
151
155
  )
152
156
 
153
157
  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
@@ -199,13 +203,13 @@ module RenderTurboStream
199
203
  #== RENDER TO CHANNEL
200
204
  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
201
205
 
202
- libs = RenderTurboStream::ChannelLibs.new(response)
206
+ libs = RenderTurboStream::ChannelLibs.new
203
207
  if (helpers.current_user.id rescue false)
204
208
  libs.send_actions_to_channel(
205
209
  "authenticated_user_#{helpers.current_user.id}",
206
- turbo_actions,
207
- instance_variables: instance_variables
210
+ turbo_actions
208
211
  )
212
+ RenderTurboStream::Libs.set_test_responses(response, libs.test_responses)
209
213
  end
210
214
 
211
215
  if save_action && if_success_redirect_to.present?
@@ -215,14 +219,29 @@ module RenderTurboStream
215
219
 
216
220
  end
217
221
 
218
- @render_turbo_stream_evaluate_instance_variables = nil
222
+ private
219
223
 
220
- def evaluate_instance_variables
221
- @render_turbo_stream_evaluate_instance_variables ||= (
222
- { var_test: 'alpha' }
224
+ def fetch_instance_variables(locals, partial, template, action)
225
+ _locals = (locals ? locals : {})
226
+
227
+ # instance variables
228
+
229
+ check_template = RenderTurboStream::CheckTemplate.new(
230
+ partial: partial,
231
+ template: template,
232
+ available_instance_variables: self.instance_variables,
233
+ action: action
223
234
  )
224
235
 
236
+ # add instance_variables to locals
237
+
238
+ check_template.templates_instance_variables.each do |v|
239
+ _locals[:"render_turbo_stream_instance_variable_#{v}"] = self.instance_variable_get(v.to_s)
240
+ end
241
+
242
+ _locals
225
243
  end
226
244
 
245
+
227
246
  end
228
247
  end
@@ -1,29 +1,14 @@
1
1
  module RenderTurboStream
2
2
  class ChannelLibs
3
3
 
4
- def initialize(response)
5
- @response = response
4
+ def initialize
5
+ @test_responses = []
6
6
  end
7
7
 
8
- def render_to_channel(channel, target, action, controller_self, partial: nil, template: nil, locals: nil)
8
+ def render_to_channel(channel, target, action, partial: nil, template: nil, locals: nil)
9
9
 
10
10
  _locals = (locals ? locals : {})
11
11
 
12
- # instance variables
13
-
14
- check_template = RenderTurboStream::CheckTemplate.new(
15
- partial: partial,
16
- template: template,
17
- available_instance_variables: controller_self.instance_variables,
18
- action: action
19
- )
20
-
21
- # add instance_variables to locals
22
-
23
- check_template.templates_instance_variables.each do |v|
24
- _locals[:"render_turbo_stream_instance_variable_#{v}"] = controller_self.instance_variable_get(v.to_s)
25
- end
26
-
27
12
  # fetch target-id
28
13
 
29
14
  target_id = RenderTurboStream::Libs.target_to_target_id(target)
@@ -48,7 +33,7 @@ module RenderTurboStream
48
33
  # add headers for test
49
34
 
50
35
  if Rails.env.test?
51
- props = {
36
+ test_response = {
52
37
  target: target,
53
38
  action: action,
54
39
  type: 'channel-partial',
@@ -56,19 +41,9 @@ module RenderTurboStream
56
41
  channel: channel,
57
42
  html_response: rendered_html.to_s
58
43
  }
59
-
60
- props[:partial] = partial if partial
61
- props[:template] = template if template
62
- h = @response.headers.to_h
63
- i = 1
64
- loop do
65
- k = "test-turbo-channel-#{i}"
66
- unless h.keys.include?(k)
67
- @response.headers[k] = props.to_json
68
- break
69
- end
70
- i += 1
71
- end
44
+ test_response[:partial] = partial if partial
45
+ test_response[:template] = template if template
46
+ @test_responses.push(test_response)
72
47
  end
73
48
 
74
49
  # send
@@ -98,7 +73,7 @@ module RenderTurboStream
98
73
 
99
74
  if Rails.env.test?
100
75
 
101
- props = {
76
+ test_response = {
102
77
  action: command,
103
78
  type: 'channel-command',
104
79
  channel: channel,
@@ -106,19 +81,9 @@ module RenderTurboStream
106
81
  }
107
82
  if RenderTurboStream::Test::Request::Libs.first_arg_is_html_id(command)
108
83
  target_id = (arguments.first[0..0] == '#' ? arguments.first[1..-1] : arguments.first)
109
- props[:target] = RenderTurboStream::Libs.target_id_to_target(target_id)
110
- end
111
-
112
- h = @response.headers.to_h
113
- i = 1
114
- loop do
115
- k = "test-turbo-channel-#{i}"
116
- unless h.keys.include?(k)
117
- @response.headers[k] = props.to_json
118
- break
119
- end
120
- i += 1
84
+ test_response[:target] = RenderTurboStream::Libs.target_id_to_target(target_id)
121
85
  end
86
+ @test_responses.push(test_response)
122
87
  end
123
88
 
124
89
  content = RenderController.render(template: 'render_turbo_stream_command', layout: false, locals: { command: command, arguments: arguments })
@@ -129,17 +94,21 @@ module RenderTurboStream
129
94
  )
130
95
  end
131
96
 
132
- def send_actions_to_channel(channel, actions, instance_variables)
97
+ def send_actions_to_channel(channel, actions)
133
98
  actions.each do |a|
134
99
  if a.is_a?(Array)
135
100
  action_to_channel(channel, a.first, a[1..-1])
136
101
  else
137
- render_to_channel(channel, a[:target], a[:action], instance_variables, partial: a[:partial], template: a[:template], locals: a[:locals])
102
+ render_to_channel(channel, a[:target], a[:action], partial: a[:partial], template: a[:template], locals: a[:locals])
138
103
  end
139
104
  end
140
105
  end
141
106
 
142
107
  def self.fetch_partials_variables(relative_path) end
143
108
 
109
+ def test_responses
110
+ @test_responses || []
111
+ end
112
+
144
113
  end
145
114
  end
@@ -1,6 +1,11 @@
1
+
2
+
3
+
1
4
  module RenderTurboStream
2
5
  module ControllerHelpers
3
6
 
7
+
8
+
4
9
  # Handles translated flash messages as defined in translations and configs.
5
10
  # If :if_success_redirect_to and channel set up and allow_channel_to_me_for_turbo_stream_save are configured, sends flash message by channel_to_me.
6
11
  # you can add more stream actions to the same response
@@ -109,9 +114,11 @@ module RenderTurboStream
109
114
  response.status = 303
110
115
 
111
116
  if allow_channel
117
+
112
118
  Rails.logger.debug(" • Send actions through Turbo::StreamsChannel")
113
- c_libs = RenderTurboStream::ChannelLibs.new(response)
114
- c_libs.send_actions_to_channel("authenticated_user_#{helpers.current_user.id}", streams, @render_turbo_stream_evaluate_instance_variables)
119
+ c_libs = RenderTurboStream::ChannelLibs.new
120
+ c_libs.send_actions_to_channel("authenticated_user_#{helpers.current_user.id}", streams)
121
+ set_test_responses(c_libs.test_responses)
115
122
  else
116
123
  flash[:alert] = flashes[:alerts]
117
124
  flash[:notice] = flashes[:notices]
@@ -183,5 +190,23 @@ module RenderTurboStream
183
190
  render_turbo_stream([{ partial: partial, locals: locals, id: id, action: action }])
184
191
  end
185
192
 
193
+
194
+ def set_test_responses(test_responses)
195
+ if Rails.env.test?
196
+ h = response.headers.to_h
197
+ i = 1
198
+ test_responses.each do |test_response|
199
+ loop do
200
+ k = "turbo-response-test-#{i}"
201
+ unless h.keys.include?(k)
202
+ response.headers[k] = test_response.to_json
203
+ break
204
+ end
205
+ i += 1
206
+ end
207
+ end
208
+ end
209
+ end
210
+
186
211
  end
187
212
  end
@@ -5,8 +5,8 @@ module RenderTurboStream
5
5
  if target_id.present?
6
6
  if target_id[0] == '#'
7
7
  target_id
8
- else
9
- "##{target_id}"
8
+ else
9
+ "##{target_id}"
10
10
  end
11
11
  else
12
12
  nil
@@ -89,14 +89,31 @@ module RenderTurboStream
89
89
  end
90
90
 
91
91
  def self.debug_save_errors(object, controller_action_name)
92
- if object.id.present?
93
- Rails.logger.debug(" • #{controller_action_name} #{object.class}:#{object.id} FAILED:")
94
- else
95
- Rails.logger.debug(" • #{controller_action_name} #{object.class} FAILED:")
96
- end
97
- object.errors.full_messages.each do |e|
98
- Rails.logger.debug(" => #{e}")
92
+ if object.id.present?
93
+ Rails.logger.debug(" • #{controller_action_name} #{object.class}:#{object.id} FAILED:")
94
+ else
95
+ Rails.logger.debug(" • #{controller_action_name} #{object.class} FAILED:")
96
+ end
97
+ object.errors.full_messages.each do |e|
98
+ Rails.logger.debug(" => #{e}")
99
+ end
100
+ end
101
+
102
+ def self.set_test_responses(response, test_responses)
103
+ if Rails.env.test?
104
+ h = response.headers.to_h
105
+ i = 1
106
+ test_responses.each do |test_response|
107
+ loop do
108
+ k = "turbo-response-test-#{i}"
109
+ unless h.keys.include?(k)
110
+ response.headers[k] = test_response.to_json
111
+ break
112
+ end
113
+ i += 1
114
+ end
99
115
  end
116
+ end
100
117
  end
101
118
 
102
119
  end
@@ -8,10 +8,11 @@ module RenderTurboStream
8
8
  end
9
9
 
10
10
  def all_turbo_responses
11
+
11
12
  e = Nokogiri::HTML(@response.body).search('#rendered-partials').first
12
13
  res = (e.present? ? JSON.parse(e.inner_html) : [])
13
14
  @response.headers.each do |k, v|
14
- next unless k.match(/^test-turbo-channel-[\d]+$/)
15
+ next unless k.match(/^turbo-response-test-[\d]+$/)
15
16
  h = JSON.parse(v)
16
17
  res.push(h)
17
18
  end
@@ -1,3 +1,3 @@
1
1
  module RenderTurboStream
2
- VERSION = "4.3.17"
2
+ VERSION = "4.4.0"
3
3
  end
@@ -8,9 +8,11 @@ require 'render_turbo_stream/test/system/helpers'
8
8
  require 'render_turbo_stream/test/request/libs'
9
9
 
10
10
  require 'render_turbo_stream/controller_helpers'
11
- require 'render_turbo_stream/controller_channel_helpers'
12
11
 
12
+ require 'render_turbo_stream/channel'
13
+ require 'render_turbo_stream/channel_controller_helpers'
13
14
  require 'render_turbo_stream/channel_view_helpers'
15
+
14
16
  require 'render_turbo_stream/view_helpers'
15
17
 
16
18
  require 'render_turbo_stream/channel_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.17
4
+ version: 4.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - christian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-01-17 00:00:00.000000000 Z
11
+ date: 2024-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -49,10 +49,11 @@ files:
49
49
  - app/views/render_turbo_stream_request_test.html.erb
50
50
  - db/migrate/20230616070450_create_render_turbo_stream_options.rb
51
51
  - lib/render_turbo_stream.rb
52
+ - lib/render_turbo_stream/channel.rb
53
+ - lib/render_turbo_stream/channel_controller_helpers.rb
52
54
  - lib/render_turbo_stream/channel_libs.rb
53
55
  - lib/render_turbo_stream/channel_view_helpers.rb
54
56
  - lib/render_turbo_stream/check_template.rb
55
- - lib/render_turbo_stream/controller_channel_helpers.rb
56
57
  - lib/render_turbo_stream/controller_helpers.rb
57
58
  - lib/render_turbo_stream/controller_libs.rb
58
59
  - lib/render_turbo_stream/engine.rb