From 38afda5205977e2879407815740e4f6dd29a97cb Mon Sep 17 00:00:00 2001 From: pvincent Date: Thu, 11 Sep 2025 11:49:46 +0000 Subject: [PATCH] render_stream works with array --- .vscode/extensions.json | 3 +- .vscode/settings.json | 2 +- app/controllers/application_controller.rb | 16 +++++ app/controllers/notification_controller.rb | 45 +++++++------ app/views/layouts/application.html.erb | 6 +- app/views/layouts/components/_flash.html.erb | 8 +-- .../layouts/components/_flashbar.html.erb | 29 ++++----- app/views/layouts/components/_footer.html.erb | 8 +-- app/views/layouts/components/_navbar.html.erb | 2 +- .../notification/_turbo_placeholder.html.erb | 3 + .../notification/_turbo_placeholder2.html.erb | 3 + app/views/notification/index.html.erb | 64 ++++++++++++------- 12 files changed, 112 insertions(+), 77 deletions(-) create mode 100644 app/views/notification/_turbo_placeholder.html.erb create mode 100644 app/views/notification/_turbo_placeholder2.html.erb diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 5b5d8c4..0977b57 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -1,7 +1,6 @@ { "recommendations": [ "aki77.rails-db-schema", - "aliariff.vscode-erb-beautify", "bierner.markdown-mermaid", "bpruitt-goddard.mermaid-markdown-syntax-highlighting", "bradlc.vscode-tailwindcss", @@ -15,4 +14,4 @@ "mads-hartmann.bash-ide-vscode", "etsi0.class-collapse", ] -} +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 450b40d..a268df8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -15,7 +15,7 @@ "editor.formatOnType": true // Enable formatting while typing }, "[erb]": { - "editor.defaultFormatter": "aliariff.vscode-erb-beautify", + "editor.defaultFormatter": "marcoroth.herb-lsp", }, "debug.internalConsoleOptions": "neverOpen", "editor.formatOnSave": true, diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0d95db2..5e2b96b 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,4 +1,20 @@ 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 + streams = [] + streams << turbo_stream.replace(:flash, partial: 'layouts/components/flash') if flash.any? + if block_given? + yields = yield(turbo_stream) + if yields.is_a? Array + streams += yields + else + streams << yields + end + end + render turbo_stream: streams + end end diff --git a/app/controllers/notification_controller.rb b/app/controllers/notification_controller.rb index 70aa0d9..e4fa294 100644 --- a/app/controllers/notification_controller.rb +++ b/app/controllers/notification_controller.rb @@ -1,5 +1,3 @@ -COUNT ||= 0 - class NotificationController < ApplicationController def index; end @@ -14,35 +12,36 @@ class NotificationController < ApplicationController end def turbo_notice - COUNT += 1 - # TODO: render_stream (partial + flash -> flash.now + optional replace|append + notice|alert) - flash.now[:notice] = "coucou ##{COUNT}" - render turbo_stream: [ - turbo_stream.replace(:flash, partial: 'layouts/components/flash') - ] + render_stream notice: "my notice ##{inc_count}" do |stream| + stream.replace :turbo_placeholder, partial: 'turbo_placeholder' + end end def turbo_alert - COUNT += 1 - flash.now[:alert] = "alert, do the stuff right now ##{COUNT}" - render turbo_stream: [ - turbo_stream.replace(:flash, partial: 'layouts/components/flash') - ] + render_stream alert: "alert, do the stuff right now ##{inc_count}" do |stream| + stream.replace :turbo_placeholder2, partial: 'turbo_placeholder2' + end end def turbo_more - COUNT += 1 - flash.now[:alert] = "alert, do the stuff right now ##{COUNT}" - flash.now[:notice] = ["this is extra notice ##{COUNT}", 'one more time'] - render turbo_stream: [ - turbo_stream.replace(:flash, partial: 'layouts/components/flash') - ] + flash.now[:alert] = "alert, do the stuff right now ##{inc_count}" + flash.now[:notice] = ["this is extra notice ##{inc_count}", 'one more time'] + render_stream do |stream| + [ + stream.replace(:turbo_placeholder, partial: 'turbo_placeholder'), + stream.replace(:turbo_placeholder2, partial: 'turbo_placeholder2') + ] + end end def turbo_none - COUNT += 1 - render turbo_stream: [ - turbo_stream.replace(:flash, partial: 'layouts/components/flash') - ] + render_stream + end + + private + + def inc_count + @@count ||= 0 # rubocop:disable Style/ClassVars + @@count += 1 # rubocop:disable Style/ClassVars end end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 0131a7f..d859115 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -23,11 +23,11 @@ - <%= render 'layouts/components/flashbar'%> - <%= render 'layouts/components/navbar'%> + <%= render 'layouts/components/flashbar' %> + <%= render 'layouts/components/navbar' %>
<%= yield %>
- <%= render 'layouts/components/footer'%> + <%= render 'layouts/components/footer' %> diff --git a/app/views/layouts/components/_flash.html.erb b/app/views/layouts/components/_flash.html.erb index c44b0c1..a6bc352 100644 --- a/app/views/layouts/components/_flash.html.erb +++ b/app/views/layouts/components/_flash.html.erb @@ -7,14 +7,14 @@ turbo:load@window->flash#buildPopupsFromWithin popup-toast@window->flash#onPopupEvent " } do %> -