render_turbo_stream 4.3.17 → 4.4.1

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: 91ec62a537df4902f5adcf7caf65a6b6a098365d2c983cda574a2d9ab1ffd22e
4
- data.tar.gz: 28b80ad8d49000bc97b8b4fb0d66e1810befd61f0812aeb00712f8836d2dca3d
3
+ metadata.gz: 2751555dcedccddcd194a704306282dd4a7f9e8dfce3da98fff1f3f43008826b
4
+ data.tar.gz: 40c7a231488efdd817a0143234188189e51c9dd27917c22858c637e7cce823ae
5
5
  SHA512:
6
- metadata.gz: 343b599092acb9ccb1dc2f046175af02a6771696f0883f573b9f5f99e3891ff86fe6064510e761a05deba57feb814d0a473da61f8842eea0ea4642fc16d33996
7
- data.tar.gz: ea135fc6460784cf9a2a7c5f738fe37e639e23645577b53b5e761443ca20bca141167eb6d68a8adcf466b7f90e4c4caea8aef55ba4838c3f29b9231670fd7139
6
+ metadata.gz: 5c31b229cf1e1b14915c20e0a8b93ee5fe5f685fd3e1a2ae9b07d335d7b7a0b84a84b72138fa40e2ea71aa280f38cb66565ed3a3906e87d3d6f86b7ee0b58a15
7
+ data.tar.gz: a8fd505b93f9ebe97b1fcac9540bf4bd6bdc8181f05b4ba719dda2c38fb0984c4b2d8ece9b023e95f2ae218cca10db0838807679bada4a3820350abeac05bd94
@@ -0,0 +1,45 @@
1
+ module RenderTurboStream
2
+ class Channel
3
+
4
+ def initialize(channel, partial: nil, template: nil, target_id: nil, action: :replace)
5
+ @channel = channel
6
+ @partial = partial
7
+ @template = template
8
+ @target_id = target_id
9
+ @action = action
10
+ @test_responses = []
11
+ end
12
+
13
+ def target_id=(target_id)
14
+ @target_id = target_id
15
+ end
16
+
17
+ def action=(action)
18
+ @action = action
19
+ end
20
+
21
+ def send(locals: {})
22
+ libs = RenderTurboStream::ChannelLibs.new
23
+ libs.render_to_channel(
24
+ @channel,
25
+ RenderTurboStream::Libs.target_id_to_target(@target_id),
26
+ @action,
27
+ template: @template,
28
+ partial: @partial,
29
+ locals: locals
30
+ )
31
+ @test_responses = libs.test_responses
32
+ end
33
+
34
+ def test_responses
35
+ @test_responses
36
+ end
37
+
38
+ private
39
+
40
+ def validate_params
41
+ raise 'Arguments partial and template cannot both be specified' if @partial && @template
42
+ end
43
+
44
+ end
45
+ 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.1"
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.1
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