-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore[Op#50985]: Extract flash message helper from application helper
- Loading branch information
Showing
2 changed files
with
93 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#-- 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 { |k, v| render_flash_message(k, v) } | ||
|
||
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 |