class ApplicationController < ActionController::Base # Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has. allow_browser versions: :modern def render_stream(notice: nil, alert: nil, &) flash.now[:notice] = notice if notice flash.now[:alert] = alert if alert 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 # see: https://turbo.hotwired.dev/reference/streams STREAM_ACTIONS = %i[append prepend replace update remove before after refresh].freeze STREAM_ACTIONS.each do |method| define_method(method) do |*streamables, **attributes| @elements << @turbo_stream.send(method, *streamables, **attributes) end end attr_reader :elements def initialize(turbo_stream) @turbo_stream = turbo_stream @elements = [] end end private_constant(:ProxyStream) end