Skip to content

Commit

Permalink
chore[Op#50985]: Extract flash message helper from application helper
Browse files Browse the repository at this point in the history
  • Loading branch information
akabiru committed Nov 30, 2023
1 parent d172738 commit f4a06c4
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 53 deletions.
53 changes: 0 additions & 53 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,59 +115,6 @@ def due_date_distance_in_words(date)
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 { |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 = {})
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
Expand Down
93 changes: 93 additions & 0 deletions app/helpers/flash_messages_helper.rb
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

0 comments on commit f4a06c4

Please sign in to comment.