Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Implement #render_flash_messages_as_turbo_streams

Alexander Brandon Coles8858f66e47b7
diff --git a/app/helpers/flash_messages_helper.rb b/app/helpers/flash_messages_helper.rb
index 40300ae197c..afcb847a709 100644
--- a/app/helpers/flash_messages_helper.rb
+++ b/app/helpers/flash_messages_helper.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -34,23 +36,19 @@ module FlashMessagesHelper
include FlashMessagesOutputSafetyHelper
end
- # Renders flash messages
+ # Renders flash messages.
+ #
+ # @return [String] an HTML-safe string.
def render_flash_messages
- messages = flash
- .reject { |k, _| k.start_with? "_" }
- .reject { |k, _| k.to_s == "op_modal" }
- .map { |k, v| render_flash_content(k.to_sym, v) }
-
- safe_join messages, "\n"
+ safe_join build_flash_components.map { it.render_in(self) }, "\n"
end
- def render_flash_content(key, content)
- case content
- when Hash
- render_flash_message(key, **content)
- else
- render_flash_message(key, message: content)
- end
+ # Renders flash messages wrapped in `<turbo-stream>` tags, suitable for
+ # inclusion inline (e.g. as part of a turbo-frame response).
+ #
+ # @return [String] an HTML-safe string.
+ def render_flash_messages_as_turbo_streams
+ safe_join(build_flash_components.map { it.render_as_turbo_stream(view_context: self, action: :flash) })
end
def render_flash_modal
@@ -62,6 +60,15 @@ module FlashMessagesHelper
component.new(**content.fetch(:parameters, {})).render_in(self)
end
+ private
+
+ def build_flash_components
+ flash
+ .reject { |k, _| k.start_with? "_" }
+ .reject { |k, _| k.to_s == "op_modal" }
+ .map { |k, v| build_flash_component(k.to_sym, v) }
+ end
+
def mapped_flash_type(type)
case type
when :error, :danger
@@ -75,9 +82,10 @@ module FlashMessagesHelper
end
end
- def render_flash_message(type, message:, **args)
- render(OpPrimer::FlashComponent.new(scheme: mapped_flash_type(type), **args)) do
- join_flash_messages(message)
- end
+ def build_flash_component(type, *args)
+ options = args.extract_options!
+ content = args.first || options[:message]
+ OpPrimer::FlashComponent.new(scheme: mapped_flash_type(type), **options)
+ .with_content(join_flash_messages(content))
end
end
diff --git a/spec/helpers/flash_messages_helper_spec.rb b/spec/helpers/flash_messages_helper_spec.rb
index fc74ddd118a..8f10fbb0971 100644
--- a/spec/helpers/flash_messages_helper_spec.rb
+++ b/spec/helpers/flash_messages_helper_spec.rb
@@ -31,36 +31,36 @@
require "spec_helper"
RSpec.describe FlashMessagesHelper do
- describe "#render_flash_messages" do
- subject { helper.render_flash_messages }
-
- shared_examples "rendering a banner" do |css_class, icon, text|
- it "applies the correct classes" do
- expect(subject).to have_css ".flash", class: css_class
- end
+ shared_examples "rendering a banner" do |css_class, icon, text|
+ it "applies the correct classes" do
+ expect(subject).to have_css ".flash", class: css_class
+ end
- it "renders an icon" do
- expect(subject).to have_octicon(icon)
- end
+ it "renders an icon" do
+ expect(subject).to have_octicon icon
+ end
- it "renders banner text" do
- expect(subject).to have_text(text)
- end
+ it "renders banner text" do
+ expect(subject).to have_text text
+ end
- it "always returns HTML-safe strings" do
- expect(subject).to be_html_safe
- end
+ it "always returns HTML-safe strings" do
+ expect(subject).to be_html_safe
end
+ end
- shared_examples "rendering nothing" do
- it "renders nothing" do
- expect(subject).to be_blank
- end
+ shared_examples "rendering nothing" do
+ it "renders nothing" do
+ expect(subject).to be_blank
+ end
- it "always returns HTML-safe strings" do
- expect(subject).to be_html_safe
- end
+ it "always returns HTML-safe strings" do
+ expect(subject).to be_html_safe
end
+ end
+
+ describe "#render_flash_messages" do
+ subject { helper.render_flash_messages }
context "with no flash messages" do
it_behaves_like "rendering nothing"
@@ -123,6 +123,33 @@ RSpec.describe FlashMessagesHelper do
end
end
+ describe "#render_flash_messages_as_turbo_streams" do
+ subject { helper.render_flash_messages_as_turbo_streams }
+
+ context "with no flash messages" do
+ it_behaves_like "rendering nothing"
+ end
+
+ context "with an :info flash message" do
+ before do
+ flash[:info] = "zu deiner Information" # rubocop:disable Rails/I18nLocaleTexts
+ end
+
+ it "renders turbo streams" do
+ expect(subject).to have_element "turbo-stream", action: "flash", target: "op-primer-flash-component" do |element|
+ expect(element).to have_css "template", visible: :all
+ end
+ end
+
+ # N.B. Capybara does not consider <template> contents as part of the document.
+ # As such, the following is not possible:
+ #
+ # it_behaves_like "rendering a banner", "flash", :bell, "zu deiner Information"
+ #
+ # See https://github.com/teamcapybara/capybara/issues/2510
+ end
+ end
+
describe "#render_flash_modal" do
subject { helper.render_flash_modal }