|
|
@ -5,17 +5,27 @@ class ApplicationController < ActionController::Base |
|
|
|
def render_stream(notice: nil, alert: nil, &) |
|
|
|
flash.now[:notice] = notice if notice |
|
|
|
flash.now[:alert] = alert if alert |
|
|
|
streams = [] |
|
|
|
streams << turbo_stream.replace(:flash, partial: 'layouts/components/flash') if flash.any? |
|
|
|
# TODO: turbo_stream_elements which stores any append, prepend, replace, update, remove, before, after or refresh actions |
|
|
|
if block_given? |
|
|
|
yields = yield(turbo_stream) |
|
|
|
if yields.is_a? Array |
|
|
|
streams += yields |
|
|
|
else |
|
|
|
streams << yields |
|
|
|
end |
|
|
|
|
|
|
|
proxy_stream = ProxyStream.new(turbo_stream) |
|
|
|
proxy_stream.replace(:flash, partial: 'layouts/components/flash') if flash.any? |
|
|
|
yield(proxy_stream) if block_given? |
|
|
|
render turbo_stream: proxy_stream.elements |
|
|
|
end |
|
|
|
|
|
|
|
class ProxyStream |
|
|
|
attr_reader :elements |
|
|
|
|
|
|
|
def initialize(turbo_stream) |
|
|
|
@turbo_stream = turbo_stream |
|
|
|
@elements = [] |
|
|
|
end |
|
|
|
|
|
|
|
def append(*streamables, **attributes) |
|
|
|
@elements << @turbo_stream.append(*streamables, **attributes) |
|
|
|
end |
|
|
|
|
|
|
|
def replace(*streamables, **attributes) |
|
|
|
@elements << @turbo_stream.replace(*streamables, **attributes) |
|
|
|
end |
|
|
|
render turbo_stream: streams |
|
|
|
end |
|
|
|
end |