Module: RenderTurboStream::ChannelControllerHelpers

Defined in:
lib/render_turbo_stream/channel_controller_helpers.rb

Instance Method Summary collapse

Instance Method Details

#action_to_all(action, *arguments) ⇒ Object



96
97
98
# File 'lib/render_turbo_stream/channel_controller_helpers.rb', line 96

def action_to_all(action, *arguments)
  action_to_channel('all', action, arguments)
end

#action_to_authenticated_group(group, action, *arguments) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/render_turbo_stream/channel_controller_helpers.rb', line 116

def action_to_authenticated_group(group, action, *arguments)

  begin
    u_id = helpers.current_user&.id
    unless u_id.present?
      Rails.logger.debug('  • SKIP RenderTurboStream.action_to_authenticated_group because current_user is nil')
      return
    end
  rescue
    Rails.logger.debug('  • ERROR RenderTurboStream.action_to_authenticated_group because current_user method is not available')
    return
  end

  action_to_channel("authenticated_group_#{group}", action, arguments)
end

#action_to_channel(channel, command, arguments) ⇒ Object



132
133
134
135
136
137
# File 'lib/render_turbo_stream/channel_controller_helpers.rb', line 132

def action_to_channel(channel, command, arguments)
  libs = RenderTurboStream::ChannelLibs.new
  r = libs.action_to_channel(channel, command, arguments)
  RenderTurboStream::Test::Request::Libs.set_test_responses(response, libs.test_responses)
  r
end

#action_to_me(action, *arguments) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/render_turbo_stream/channel_controller_helpers.rb', line 100

def action_to_me(action, *arguments)

  begin
    u_id = helpers.current_user&.id
    unless u_id.present?
      Rails.logger.debug('  • SKIP RenderTurboStream.action_to_me because current_user is nil')
      return
    end
  rescue
    Rails.logger.debug('  • ERROR RenderTurboStream.action_to_me because current_user is not available')
    return
  end

  action_to_channel("authenticated_user_#{helpers.current_user.id}", action, arguments)
end

#render_to_all(target_id = nil, action = :replace, partial: nil, template: nil, locals: nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/render_turbo_stream/channel_controller_helpers.rb', line 4

def render_to_all(target_id = nil, action = :replace, partial: nil, template: nil, locals: nil)
  # evaluate_instance_variables
  render_to_channel(
    'all',
    target_id,
    action,
    partial: partial,
    template: template,
    locals: locals
  )
end

#render_to_authenticated_group(group, target_id = nil, action = :replace, partial: nil, locals: nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/render_turbo_stream/channel_controller_helpers.rb', line 37

def render_to_authenticated_group(group, target_id = nil, action = :replace, partial: nil, locals: nil)
  begin
    u_id = helpers.current_user&.id
    unless u_id.present?
      Rails.logger.debug('  • SKIP RenderTurboStream.render_to_authenticated_group because current_user is nil')
      return
    end
  rescue
    Rails.logger.debug('  • ERROR RenderTurboStream.render_to_authenticated_group because current_user method is not available')
    return
  end

  render_to_channel(
    "authenticated_group_#{group}",
    target_id,
    action,
    partial: partial,
    locals: locals
  )
end

#render_to_channel(channel, target_id = nil, action = :replace, partial: nil, template: nil, locals: nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/render_turbo_stream/channel_controller_helpers.rb', line 58

def render_to_channel(channel, target_id = nil, action = :replace, partial: nil, template: nil, locals: nil)

  raise 'Arguments partial and template cannot both be specified' if partial && template

  disable_default = false
  if partial.present?
    _partial = RenderTurboStream::Libs.partial_path(controller_path, partial)
  elsif template.present?
    _template = RenderTurboStream::Libs.partial_path(controller_path, template)
    disable_default = true
  else
    _template = [controller_path, action_name].join('/')
    disable_default = true
  end

  if disable_default && !@render_cable_template_was_called
    # make sure default render action returns nil
    render template: 'render_turbo_stream_empty_template', layout: false unless @render_cable_template_was_called
    @render_cable_template_was_called = true
  end

  # evaluate_instance_variables

  libs = RenderTurboStream::ChannelLibs.new
  loc = fetch_instance_variables(locals, _partial, _template, action)
  res = libs.render_to_channel(
    channel,
    RenderTurboStream::Libs.target_id_to_target(target_id),
    action,
    template: _template,
    partial: _partial,
    locals: loc
  )
  RenderTurboStream::Test::Request::Libs.set_test_responses(response, libs.test_responses)

  res
end

#render_to_me(target_id = nil, action = :replace, partial: nil, locals: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/render_turbo_stream/channel_controller_helpers.rb', line 16

def render_to_me(target_id = nil, action = :replace, partial: nil, locals: nil)
  begin
    u_id = helpers.current_user&.id
    unless u_id.present?
      Rails.logger.debug('  • SKIP RenderTurboStream.render_to_me because current_user is nil')
      return
    end
  rescue
    Rails.logger.debug('  • ERROR RenderTurboStream.render_to_me because current_user is not available')
    return
  end

  render_to_channel(
    "authenticated_user_#{helpers.current_user.id}",
    target_id,
    action,
    partial: partial,
    locals: locals
  )
end

#turbo_channel_save(save_action, object: nil, if_success_redirect_to: nil, if_success_add: nil, if_error_add: nil, add: [], if_success_notices: nil, if_error_alerts: nil, add_notices: nil, add_alerts: nil, flash_controller_action_name: action_name) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/render_turbo_stream/channel_controller_helpers.rb', line 139

def turbo_channel_save(
  save_action,
  object: nil, # object used in save_action, example: @customer

  if_success_redirect_to: nil, # does a regular redirect. Works if you are inside a turbo_frame and just want to redirect inside that frame BUT CANNOT STREAM OTHERS ACTIONS ON THE SAME RESPONSE https://github.com/rails/rails/issues/48056

  if_success_add: nil, # hash for a partial to render or array with actions (as array) or hashes for partials within
  if_error_add: nil, # additional partials that should be rendered if save_action failed
  add: [], # additional streams

  if_success_notices: nil, # array of strings, override default generated flash generation in the case of success
  if_error_alerts: nil,
  add_notices: nil, # array of strings
  add_alerts: nil,

  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}"
)

  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  #                    LOGIC
  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

  unless object
    object = eval("@#{controller_name.classify.underscore}")
  end
  unless object
    raise "Could not fetch a model name by «eval(\"\@\#{controller_name.classify.underscore}\")». You must provide the argument :object."
  end

  libs = RenderTurboStream::ControllerLibs.new(save_action)
  model_name = object.model_name.human

  turbo_actions = libs.generate_flash(
    model_name,
    flash_controller_action_name,
    if_success_notices,
    if_error_alerts,
    add_notices,
    add_alerts,
  )[:turbo_actions]

  turbo_actions += libs.additional_actions(
    if_success_add,
    if_error_add,
    add
  )

  if libs.action_errors(turbo_actions).present?
    raise libs.action_errors(turbo_actions).join(', ')
  end

  if save_action
    if Rails.configuration.x.store_last_saved_object && object.id
      begin
        session['last_saved_object'] = object.to_global_id
      rescue
        Rails.logger.debug("session['last_saved_object'] = ... failed. You may be in test environent?")
      end
    end
  else
    RenderTurboStream::Libs.debug_save_errors(object, flash_controller_action_name)
  end

  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  #==                      RENDER TO CHANNEL
  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

  libs = RenderTurboStream::ChannelLibs.new
  if (helpers.current_user.id rescue false)
    libs.send_actions_to_channel(
      "authenticated_user_#{helpers.current_user.id}",
      turbo_actions
    )
    RenderTurboStream::Test::Request::Libs.set_test_responses(response, libs.test_responses)
  end

  if save_action && if_success_redirect_to.present?
    response.status = 303
    redirect_to if_success_redirect_to
  end

end