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

chore[Op#50985]: Extract flash message helper from application helper

Kabiru Mwenja4d3fba0cf25a
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 4b092d0ad4d..7cdaf798e0d 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -115,66 +115,6 @@ module ApplicationHelper
end
end
- def render_primer_banner_message?
- flash[:primer_banner].present?
- end
-
- def render_primer_banner_message
- return unless render_primer_banner_message?
-
- render(BannerMessageComponent.new(**flash[:primer_banner].to_hash))
- end
-
- # Renders flash messages
- def render_flash_messages
- return if render_primer_banner_message?
-
- messages = flash
- .reject { |k, _| k.start_with? '_' }
- .map do |k, v|
- if k.to_sym == :modal
- component = v[:type].constantize
- component.new(**v[:parameters]).render_in(self)
- else
- render_flash_message(k, v)
- end
- end
-
- safe_join messages, "\n"
- end
-
- def join_flash_messages(messages)
- if messages.respond_to?(:join)
- safe_join(messages, '<br />'.html_safe)
- else
- messages
- end
- end
-
- def render_flash_message(type, message, html_options = {})
- if type.to_s == 'notice'
- type = 'success'
- end
- toast_css_classes = ["op-toast -#{type}", html_options.delete(:class)]
- # Add autohide class to notice flashes if configured
- if type.to_s == 'success' && User.current.pref.auto_hide_popups?
- toast_css_classes << 'autohide-toaster'
- end
- html_options = { class: toast_css_classes.join(' '), role: 'alert' }.merge(html_options)
- close_button = content_tag :a, '', class: 'op-toast--close icon-context icon-close',
- title: I18n.t('js.close_popup_title'),
- tabindex: '0'
- toast = content_tag(:div, join_flash_messages(message), class: 'op-toast--content')
- content_tag :div, '', class: 'op-toast--wrapper' do
- content_tag :div, '', class: 'op-toast--casing' do
- content_tag :div, html_options do
- concat(close_button)
- concat(toast)
- end
- end
- end
- end
-
# Yields the given block for each project with its level in the tree
#
# Wrapper for Project#project_tree
diff --git a/app/helpers/flash_messages_helper.rb b/app/helpers/flash_messages_helper.rb
new file mode 100644
index 00000000000..ee43bce13ee
--- /dev/null
+++ b/app/helpers/flash_messages_helper.rb
@@ -0,0 +1,100 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+#
+
+module FlashMessagesHelper
+ extend ActiveSupport::Concern
+
+ included do
+ # For .safe_join in join_flash_messages
+ include ActionView::Helpers::OutputSafetyHelper
+ end
+
+ def render_primer_banner_message?
+ flash[:primer_banner].present?
+ end
+
+ def render_primer_banner_message
+ return unless render_primer_banner_message?
+
+ render(BannerMessageComponent.new(**flash[:primer_banner].to_hash))
+ end
+
+ # Renders flash messages
+ def render_flash_messages
+ return if render_primer_banner_message?
+
+ messages = flash
+ .reject { |k, _| k.start_with? '_' }
+ .map do |k, v|
+ if k.to_sym == :modal
+ component = v[:type].constantize
+ component.new(**v[:parameters]).render_in(self)
+ else
+ render_flash_message(k, v)
+ end
+ end
+
+ safe_join messages, "\n"
+ end
+
+ def join_flash_messages(messages)
+ if messages.respond_to?(:join)
+ safe_join(messages, '<br />'.html_safe)
+ else
+ messages
+ end
+ end
+
+ def render_flash_message(type, message, html_options = {}) # rubocop:disable Metrics/AbcSize
+ if type.to_s == 'notice'
+ type = 'success'
+ end
+
+ toast_css_classes = ["op-toast -#{type}", html_options.delete(:class)]
+
+ # Add autohide class to notice flashes if configured
+ if type.to_s == 'success' && User.current.pref.auto_hide_popups?
+ toast_css_classes << 'autohide-toaster'
+ end
+
+ html_options = { class: toast_css_classes.join(' '), role: 'alert' }.merge(html_options)
+ close_button = content_tag :a, '', class: 'op-toast--close icon-context icon-close',
+ title: I18n.t('js.close_popup_title'),
+ tabindex: '0'
+ toast = content_tag(:div, join_flash_messages(message), class: 'op-toast--content')
+ content_tag :div, '', class: 'op-toast--wrapper' do
+ content_tag :div, '', class: 'op-toast--casing' do
+ content_tag :div, html_options do
+ concat(close_button)
+ concat(toast)
+ end
+ end
+ end
+ end
+end