Backport: Views::TurboStream-Basisklasse + Notification-Komponenten aus Centinel #138

Closed
opened 2026-10-12 17:38:01 +00:00 by hobbypunk · 0 comments
Owner

Hintergrund

Im Zuge von Centinel #53 („Raum testen“-Button für Matrix-Hooks) sind drei generische Infrastruktur-Pieces entstanden, die hier in der Base-App allen Projekten zugutekommen sollten.

Was zu backporten ist

1. Views::TurboStream-Basisklasse

Neue Datei app/views/turbo_stream.rb:

class Views::TurboStream < Views::Base
  layout(nil)

  class_attribute :_target, default: {}
  class_attribute :_action, default: :append

  def self.target(target)
    self._target = target
  end

  def self.action(action)
    self._action = action
  end

  def turbo_target
    return instance_exec(&_target) if _target.is_a? Proc

    _target
  end

  def turbo_action
    return instance_exec(&_action) if _action.is_a? Proc

    _action
  end
end

Deklarative target/action-DSL (auch Procs für dynamische Targets), turbo_action/turbo_target als Instanz-Reader (namensgetrennt vom Klassen-DSL).

2. PhlexRenderer-Erweiterung

default_render rendert Views::TurboStream-Instanzen generisch als Turbo-Stream:

def default_render
  view_instance = find_and_prepare_phlex_view(action_name)

  if view_instance.present?
    if view_instance.is_a?(Views::TurboStream)
      render turbo_stream: turbo_stream.public_send(view_instance.turbo_action, view_instance.turbo_target, view_instance), layout: false
    else
      render view_instance, layout: false
    end
  else
    super
  end
end

Dazu im resolve_phlex_class das Naming-Schema vereinheitlichen:

  • Variant: weiterhin concatenated, direkt an den Action-Namen (IndexMention)
  • Andere Formate: eigenes Modul (Action::TurboStream), z. B. SendTestMessage::TurboStream

3. Notification-Komponenten-Split (Umbenennung von Layout::Flash)

  • Layout::Notification (Singular, app/components/layout/notification.rb): einzelne Toast-Card mit type/title/message/debug-Attributen, relative i18n-Keys (t(".icons.#{type}"), t(".debug.title") → components.layout.notification.*)
  • Layout::Notifications (Plural, app/components/layout/notifications.rb): Container mit id="notifications" (immer gerendert, damit Streams andocken können), iteriert Flash-Einträge und delegiert an Layout::Notification
  • Layout rendert Layout::Notifications() statt Layout::Flash()
  • Navbar-Icon-Key umziehen: helpers.flash.icons.warning → components.layout.notification.icons.warning
  • i18n: Icons + Debug-Title unter components.layout.notification in components.de.yml; dynamisch aufgelöste Keys in config/i18n-tasks.yml unter ignore_unused aufnehmen

4. Card-Component-Verallgemeinerung

Die Components::Card wurde flexibler gemacht, damit sie neben den bisherigen Listen-/Link-Cards auch als freie Layout-Card (z. B. Startpage-Features, Login-/Registrierungs-Karte) genutzt werden kann:

class Components::Card < Components::Base
  attribute :icon, default: nil
  attribute :action_icon, default: "fa-arrow-right"
  attribute :href, default: nil
  attribute :data, default: {}
  attribute :span, default: "col-span-6 lg:col-span-4 xl:col-span-3"
  attribute :with_body, default: true

  def view_template
    tag = href ? :a : :div
    tag_options = {
      class: [
        "card", "bg-base-100", "card-border", "border-input-base", span, options[:class],
        ("hover:scale-101 transition-all duration-100 cursor-pointer no-underline" if href.present?)
      ],
      **({ href:, data: } if href.present?)
    }

    public_send(tag, **tag_options) do
      if with_body
        body(with_footer: true) { yield self }
      else
        yield self
      end
    end
  end

  def title(text = nil, **options, &)
    div(class: ["card-title", options[:class]]) do
      if icon.is_a?(Components::Icon)
        icon
      elsif icon.is_a?(Translatable::Icon)
        Icon(model: icon, class: "text-primary fa-lg")
      elsif icon.is_a?(String) || icon.is_a?(Symbol)
        Icon(name: icon, class: "text-primary fa-lg")
      end
      plain(text) if text.present?
      yield if block_given?
    end
  end

  def subtitle(text, **options)
    div(class: ["card-title text-xs text-zinc-500 mb-4", options[:class]]) { plain(text) }
  end

  def body(**options, &)
    div(class: "card-body #{options[:class]}") do
      yield self

      footer("") if options[:with_footer]
    end
  end

  def footer(text = nil, **options, &)
    return if @footer_called
    @footer_called = true

    div(class: ["flex justify-between items-center text-xs h-5", options[:wrapper_class]]) do
      div(class: ["text-base-content/50", options[:class]]) do
        text ? plain(text) : yield
      end

      Icon(name: action_icon, class: "fa-2xl text-primary") if href.present?
    end
  end
end
  • with_body-Attribut (Default true): bei false wird kein Auto-Body (inkl. Footer) gerendert – die View erhält volle Kontrolle
  • body(**options) als öffentliche Methode: eigene Klassen (card.body(class: "items-center text-center gap-4")), optionales with_footer:
  • Bestehende Listen-/Link-Cards funktionieren unverändert (with_body default true)

Anwendungs-Referenzen (für eine saubere Übernahme nachzuvollziehen in Centinel):

  • app/views/pages/home.rb: Feature-Cards (Card(span: nil, with_body: false) + eigenes card.body mit Icon-Badge, card.title { h3 { ... } }) und Login-Karte (Card(span: nil, class: "shadow-xl w-full max-w-md", with_body: false))
  • app/views/registrations/new.rb: Registrierungs-Karte nach demselben Muster

Referenz-Implementierung

Alle Dateien im Centinel-PR #54 (Branch enhancement/53_send_test_message); Card-Verallgemeinerung in Centinel auf Branch enhancement/61_startpage_content (Commits werden beim Squash zusammengefasst):

  • app/views/turbo_stream.rb
  • app/controllers/concerns/phlex_renderer.rb
  • app/components/layout/notification.rb + notifications.rb
  • app/components/base.rb (Phlex::Rails::Helpers::TurboStream includieren)
  • app/components/card.rb (Verallgemeinerung)
  • app/views/pages/home.rb + app/views/registrations/new.rb (Anwendungs-Referenzen)

Akzeptanz

  • Views::TurboStream vorhanden, ein minimaler Stream-View (z. B. Demo/Toast) funktioniert
  • PhlexRenderer rendert Stream-Views generisch, HTML-Views unverändert
  • Flash→Notification-Umbenennung komplett (inkl. Navbar, Layout, i18n)
  • Card-Component mit with_body/body(**) übernommen; bestehende Listen-/Link-Cards unverändert
  • Bestehende Tests grün, i18n-Tasks health grün

Aftermath

Nach dem Backport: Upstream-Merge in Centinel (und andere abgeleitete Apps) – dabei auflösen, dass Centinel die Änderungen bereits lokal hat.

## Hintergrund Im Zuge von Centinel [#53](https://code.hoppe-dev.eu/hobbypunk/centinel/pulls/54) („Raum testen“-Button für Matrix-Hooks) sind drei generische Infrastruktur-Pieces entstanden, die hier in der Base-App allen Projekten zugutekommen sollten. ## Was zu backporten ist ### 1. `Views::TurboStream`-Basisklasse Neue Datei `app/views/turbo_stream.rb`: ```ruby class Views::TurboStream < Views::Base layout(nil) class_attribute :_target, default: {} class_attribute :_action, default: :append def self.target(target) self._target = target end def self.action(action) self._action = action end def turbo_target return instance_exec(&_target) if _target.is_a? Proc _target end def turbo_action return instance_exec(&_action) if _action.is_a? Proc _action end end ``` Deklarative `target`/`action`-DSL (auch Procs für dynamische Targets), `turbo_action`/`turbo_target` als Instanz-Reader (namensgetrennt vom Klassen-DSL). ### 2. `PhlexRenderer`-Erweiterung `default_render` rendert `Views::TurboStream`-Instanzen generisch als Turbo-Stream: ```ruby def default_render view_instance = find_and_prepare_phlex_view(action_name) if view_instance.present? if view_instance.is_a?(Views::TurboStream) render turbo_stream: turbo_stream.public_send(view_instance.turbo_action, view_instance.turbo_target, view_instance), layout: false else render view_instance, layout: false end else super end end ``` Dazu im `resolve_phlex_class` das Naming-Schema vereinheitlichen: - **Variant:** weiterhin concatenated, direkt an den Action-Namen (`IndexMention`) - **Andere Formate:** eigenes Modul (`Action::TurboStream`), z. B. `SendTestMessage::TurboStream` ### 3. Notification-Komponenten-Split (Umbenennung von `Layout::Flash`) - **`Layout::Notification`** (Singular, `app/components/layout/notification.rb`): einzelne Toast-Card mit `type`/`title`/`message`/`debug`-Attributen, relative i18n-Keys (`t(".icons.#{type}")`, `t(".debug.title")` → `components.layout.notification.*`) - **`Layout::Notifications`** (Plural, `app/components/layout/notifications.rb`): Container mit `id="notifications"` (immer gerendert, damit Streams andocken können), iteriert Flash-Einträge und delegiert an `Layout::Notification` - Layout rendert `Layout::Notifications()` statt `Layout::Flash()` - Navbar-Icon-Key umziehen: `helpers.flash.icons.warning` → `components.layout.notification.icons.warning` - i18n: Icons + Debug-Title unter `components.layout.notification` in `components.de.yml`; dynamisch aufgelöste Keys in `config/i18n-tasks.yml` unter `ignore_unused` aufnehmen ### 4. Card-Component-Verallgemeinerung Die `Components::Card` wurde flexibler gemacht, damit sie neben den bisherigen Listen-/Link-Cards auch als freie Layout-Card (z. B. Startpage-Features, Login-/Registrierungs-Karte) genutzt werden kann: ```ruby class Components::Card < Components::Base attribute :icon, default: nil attribute :action_icon, default: "fa-arrow-right" attribute :href, default: nil attribute :data, default: {} attribute :span, default: "col-span-6 lg:col-span-4 xl:col-span-3" attribute :with_body, default: true def view_template tag = href ? :a : :div tag_options = { class: [ "card", "bg-base-100", "card-border", "border-input-base", span, options[:class], ("hover:scale-101 transition-all duration-100 cursor-pointer no-underline" if href.present?) ], **({ href:, data: } if href.present?) } public_send(tag, **tag_options) do if with_body body(with_footer: true) { yield self } else yield self end end end def title(text = nil, **options, &) div(class: ["card-title", options[:class]]) do if icon.is_a?(Components::Icon) icon elsif icon.is_a?(Translatable::Icon) Icon(model: icon, class: "text-primary fa-lg") elsif icon.is_a?(String) || icon.is_a?(Symbol) Icon(name: icon, class: "text-primary fa-lg") end plain(text) if text.present? yield if block_given? end end def subtitle(text, **options) div(class: ["card-title text-xs text-zinc-500 mb-4", options[:class]]) { plain(text) } end def body(**options, &) div(class: "card-body #{options[:class]}") do yield self footer("") if options[:with_footer] end end def footer(text = nil, **options, &) return if @footer_called @footer_called = true div(class: ["flex justify-between items-center text-xs h-5", options[:wrapper_class]]) do div(class: ["text-base-content/50", options[:class]]) do text ? plain(text) : yield end Icon(name: action_icon, class: "fa-2xl text-primary") if href.present? end end end ``` - **`with_body`-Attribut** (Default `true`): bei `false` wird kein Auto-Body (inkl. Footer) gerendert – die View erhält volle Kontrolle - **`body(**options)`** als öffentliche Methode: eigene Klassen (`card.body(class: "items-center text-center gap-4")`), optionales `with_footer:` - Bestehende Listen-/Link-Cards funktionieren unverändert (`with_body` default `true`) Anwendungs-Referenzen (für eine saubere Übernahme nachzuvollziehen in Centinel): - `app/views/pages/home.rb`: Feature-Cards (`Card(span: nil, with_body: false)` + eigenes `card.body` mit Icon-Badge, `card.title { h3 { ... } }`) und Login-Karte (`Card(span: nil, class: "shadow-xl w-full max-w-md", with_body: false)`) - `app/views/registrations/new.rb`: Registrierungs-Karte nach demselben Muster ## Referenz-Implementierung Alle Dateien im Centinel-PR #54 (Branch `enhancement/53_send_test_message`); Card-Verallgemeinerung in Centinel auf Branch `enhancement/61_startpage_content` (Commits werden beim Squash zusammengefasst): - `app/views/turbo_stream.rb` - `app/controllers/concerns/phlex_renderer.rb` - `app/components/layout/notification.rb` + `notifications.rb` - `app/components/base.rb` (`Phlex::Rails::Helpers::TurboStream` includieren) - `app/components/card.rb` (Verallgemeinerung) - `app/views/pages/home.rb` + `app/views/registrations/new.rb` (Anwendungs-Referenzen) ## Akzeptanz - [ ] `Views::TurboStream` vorhanden, ein minimaler Stream-View (z. B. Demo/Toast) funktioniert - [ ] `PhlexRenderer` rendert Stream-Views generisch, HTML-Views unverändert - [ ] Flash→Notification-Umbenennung komplett (inkl. Navbar, Layout, i18n) - [ ] Card-Component mit `with_body`/`body(**)` übernommen; bestehende Listen-/Link-Cards unverändert - [ ] Bestehende Tests grün, i18n-Tasks health grün ## Aftermath Nach dem Backport: Upstream-Merge in Centinel (und andere abgeleitete Apps) – dabei auflösen, dass Centinel die Änderungen bereits lokal hat.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
hobbypunk/rails-base_app#138
No description provided.