You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.2 KiB
38 lines
1.2 KiB
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_only!(notice, 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)
|
|
|
|
private
|
|
|
|
def flash_now_only!(notice, alert)
|
|
flash.discard
|
|
flash.now[:notice] = notice if notice
|
|
flash.now[:alert] = alert if alert
|
|
end
|
|
end
|