render_turbo_stream 1.4.13 → 1.4.15

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: f7bcd60dee638218b100db8304ddda17a1d7c87df5c46d721cb541913a424abc
4
- data.tar.gz: 6e606363c60175378ac3cf4a652560d44e5d3b49d98a77c6a0557ff679df67fa
3
+ metadata.gz: ddf2d67786644e4776a58ead9c9bd705d54aa20e6c2a7f2a50383bcc59eacbc4
4
+ data.tar.gz: e5dce474494ad9076d96390ef06fca96b7303e821d2326e6d7b4a4b737f96670
5
5
  SHA512:
6
- metadata.gz: e7ca3b5b395709063ebd1c30577280527e064bff16e9dc4bcf114a1a0d1a60bda33bfe105a76f0f497a8fcc39fcf59cc0f877ca05a7688666d228fa577f9d85d
7
- data.tar.gz: 0704ce40c37540bd5135f108668348b4b86fc21cef1d8a5e94ee41a7f5e1da593fd2ec82ea9b3f7e9d7a0c47e8432716cd094beb3dcb1de2e133487d146b047c
6
+ metadata.gz: c86bcd83fb42ebd8286c981e3a1d80d7b37d8ab3ae524f91679a2a3734429692cb3be356199252bbabb9161d056214fd7a63454095259b8cc95ba9bd62eb341e
7
+ data.tar.gz: 8330c7f4b8d178d68a7be8f8b54d76a6de88676c17178a2e46878934d2c3ecedac4bc968ffa78828e7fa6288324551698f5def18aff073883de0ab3fee42d63a
data/README.md CHANGED
@@ -155,9 +155,6 @@ def update
155
155
  :turbo_frame_set_src,
156
156
  'cars-box',
157
157
  cars_path
158
- ],
159
- [
160
- #... more actions possible ...
161
158
  ]
162
159
  ]
163
160
  )
@@ -165,6 +162,17 @@ def update
165
162
 
166
163
  ```
167
164
 
165
+ **Turbo::StreamsChannel**
166
+
167
+ Another workaround as described above, but with more advantages, is to use Turbo Streams, which are seamlessly integrated into `turbo rails`. See the [README](https://gitlab.com/sedl/renderturbostream/-/blob/main/README-cable.md).
168
+ If setup well and configured by
169
+
170
+ ```ruby
171
+ config.x.render_turbo_stream.use_cable = true
172
+ ```
173
+
174
+ The `turbo_stream_save` method, in the case of classical redirects (by redirect_to), would send the flashes plus additional partials, declared on arguments by `Turbo::StreamsChannel` in parallel to the redirect.
175
+
168
176
  **Parameters for turbo_stream_save**
169
177
 
170
178
  save_action,
@@ -0,0 +1,4 @@
1
+ module RenderTurboStream
2
+ class CableStream < ApplicationRecord
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ class CableStreams < ActiveRecord::Migration[7.0]
2
+ def change
3
+ end
4
+ end
@@ -0,0 +1,11 @@
1
+ class CreateRenderTurboStreamCableStreams < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :render_turbo_stream_cable_streams do |t|
4
+ t.string :request_id, limit: 80
5
+ t.text :locals
6
+ t.string :action, limit: 80
7
+ t.string :arguments
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,51 @@
1
+ module RenderTurboStream
2
+ module TurboCableHelpers
3
+
4
+ def turbo_cable_to_authenticated
5
+ if user_signed_in?
6
+
7
+ end
8
+ end
9
+
10
+ def turbo_cable_to_current_user(target, action, *args, partial: nil, locals: nil)
11
+ if Rails.env.test?
12
+ args = {
13
+ target: target,
14
+ action: action,
15
+ type: 'turbo-cable-partial',
16
+ args: args,
17
+ partial: partial,
18
+ locals: locals
19
+ }
20
+ h = response.headers.to_h
21
+ i = 1
22
+ loop do
23
+ k = "test-turbo-cable-#{i}"
24
+ unless h.keys.include?(k)
25
+ response.headers[k] = args.to_json
26
+ break
27
+ end
28
+ i += 1
29
+ end
30
+ end
31
+ if [:append, :prepend, :replace].include?(action.to_sym)
32
+ Turbo::StreamsChannel.send(
33
+ "broadcast_#{action}_to",
34
+ "current_user_#{helpers.current_user.id}",
35
+ *args,
36
+ target: target,
37
+ partial: partial,
38
+ locals: locals,
39
+ layout: false
40
+ )
41
+ else
42
+ # Turbo::StreamsChannel.broadcast_action_to(
43
+ # "current_user_#{helpers.current_user.id}",
44
+ # action:'toggle_css_class',
45
+ # 'colored-element'
46
+ # )
47
+ end
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,17 @@
1
+ module RenderTurboStream
2
+ module TurboCableViewHelpers
3
+
4
+ def turbo_cable_from_current_user
5
+ if current_user
6
+ turbo_stream_from "current_user_#{current_user.id}"
7
+ end
8
+ end
9
+
10
+ def turbo_cable_from_authenticated_users
11
+ if user_signed_in?
12
+ turbo_stream_from "authenticated_users"
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -1,3 +1,3 @@
1
1
  module RenderTurboStream
2
- VERSION = "1.4.13"
2
+ VERSION = "1.4.15"
3
3
  end
@@ -3,6 +3,8 @@ require "render_turbo_stream/railtie"
3
3
  require 'render_turbo_stream/engine'
4
4
  require 'render_turbo_stream/test/request_helpers'
5
5
  require 'render_turbo_stream/test/rspec_request_helpers'
6
+ require 'render_turbo_stream/turbo_cable_helpers'
7
+ require 'render_turbo_stream/turbo_cable_view_helpers'
6
8
 
7
9
  module RenderTurboStream
8
10
 
@@ -14,6 +16,7 @@ module RenderTurboStream
14
16
  id: nil, # if nil: no partial is rendered
15
17
  partial: nil, # example: 'customers/form' default: "#{controller_path}/#{id}"
16
18
  action: 'replace', # options: append, prepend
19
+ flash_action: action_name, # options: 'update', 'create', otherwise you have to declare a translation in config/locales like "activerecord.success.#{flash_action}" and "activerecord.errors.#{flash_action}"
17
20
  locals: {}, # locals used by the partial
18
21
  streams_on_success: [
19
22
  {
@@ -65,35 +68,47 @@ module RenderTurboStream
65
68
  if save_action
66
69
  flash_notices = if flashes_on_success.present?
67
70
  flashes_on_success
68
- elsif action_name == 'create'
71
+ elsif flash_action.to_s == 'create'
69
72
  str = I18n.t(
70
73
  'activerecord.success.successfully_created',
71
74
  default: '%<model_name>s successfully created'
72
75
  )
73
76
  [format(str, model_name: model_name)]
74
- elsif action_name == 'update'
77
+ elsif flash_action.to_s == 'update'
75
78
  str = I18n.t(
76
79
  'activerecord.success.successfully_updated',
77
80
  default: '%<model_name>s successfully updated'
78
81
  )
79
82
  [format(str, model_name: model_name)]
83
+ else
84
+ str = I18n.t(
85
+ "activerecord.success.#{flash_action}",
86
+ default: '%<model_name>s successfully updated'
87
+ )
88
+ [format(str, model_name: model_name)]
80
89
  end
81
90
  flash_alerts = []
82
91
  else
83
92
  flash_alerts = if flashes_on_error.present?
84
93
  flashes_on_error
85
- elsif action_name == 'create'
94
+ elsif flash_action.to_s == 'create'
86
95
  str = I18n.t(
87
96
  'activerecord.errors.messages.could_not_create',
88
97
  default: '%<model_name>s could not be created'
89
98
  )
90
99
  [format(str, model_name: model_name)]
91
- elsif action_name == 'update'
100
+ elsif flash_action.to_s == 'update'
92
101
  str = I18n.t(
93
102
  'activerecord.errors.messages.could_not_update',
94
103
  default: '%<model_name>s could not be updated'
95
104
  )
96
105
  [format(str, model_name: model_name)]
106
+ else
107
+ str = I18n.t(
108
+ "activerecord.errors.messages.#{flash_action}",
109
+ default: '%<model_name>s could not be updated'
110
+ )
111
+ [format(str, model_name: model_name)]
97
112
  end
98
113
  flash_notices = []
99
114
  end
@@ -143,10 +158,24 @@ module RenderTurboStream
143
158
  ])
144
159
  elsif save_action && redirect_on_success_to.present?
145
160
  response.status = 302
146
- flash[:alert] = flash_alerts
147
- flash[:notice] = flash_notices
148
- Rails.logger.debug(" • Set flash[:alert] => #{flash_alerts}") if flash_alerts.present?
149
- Rails.logger.debug(" • Set flash[:notice] => #{flash_notices}") if flash_notices.present?
161
+ if Rails.configuration.x.render_turbo_stream.use_cable && helpers.user_signed_in?
162
+ streams.each do |s|
163
+ next unless s.is_a?(Hash)
164
+ Rails.logger.debug(" • Send by Cable => «#{s}»")
165
+ turbo_cable_to_current_user(
166
+ s[:id],
167
+ :prepend,
168
+ partial: s[:partial],
169
+ locals: s[:locals]
170
+ )
171
+ end
172
+
173
+ else
174
+ flash[:alert] = flash_alerts
175
+ flash[:notice] = flash_notices
176
+ Rails.logger.debug(" • Set flash[:alert] => #{flash_alerts}") if flash_alerts.present?
177
+ Rails.logger.debug(" • Set flash[:notice] => #{flash_notices}") if flash_notices.present?
178
+ end
150
179
  redirect_to redirect_on_success_to
151
180
 
152
181
  else
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: 1.4.13
4
+ version: 1.4.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - christian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-26 00:00:00.000000000 Z
11
+ date: 2023-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,13 +38,18 @@ files:
38
38
  - README.md
39
39
  - Rakefile
40
40
  - app/controllers/render_turbo_stream/application_controller.rb
41
+ - app/models/render_turbo_stream/cable_stream.rb
41
42
  - app/views/render_turbo_stream.html.erb
42
43
  - app/views/render_turbo_stream.turbo_stream.erb
44
+ - db/migrate/20230428115511_cable_streams.rb
45
+ - db/migrate/20230428115724_create_render_turbo_stream_cable_streams.rb
43
46
  - lib/render_turbo_stream.rb
44
47
  - lib/render_turbo_stream/engine.rb
45
48
  - lib/render_turbo_stream/railtie.rb
46
49
  - lib/render_turbo_stream/test/request_helpers.rb
47
50
  - lib/render_turbo_stream/test/rspec_request_helpers.rb
51
+ - lib/render_turbo_stream/turbo_cable_helpers.rb
52
+ - lib/render_turbo_stream/turbo_cable_view_helpers.rb
48
53
  - lib/render_turbo_stream/version.rb
49
54
  - lib/tasks/render_turbo_stream_tasks.rake
50
55
  homepage: https://gitlab.com/sedl/renderturbostream