feat(flash_messages): dry up code and ensure turbo_force: :server otherwise morphing does not refresh page with new attrs

This commit is contained in:
mfo 2024-10-23 15:18:43 +02:00 committed by Corinne Durrmeyer
parent dd362055df
commit 84870615d8
No known key found for this signature in database
GPG key ID: DDC049DDA35585B6
3 changed files with 19 additions and 20 deletions

View file

@ -36,9 +36,15 @@ module ApplicationHelper
end end
end end
def flash_class(sticky: false, fixed: false) def flash_class(level, sticky: false, fixed: false)
class_names = [] class_names = []
case level
when 'notice'
class_names << 'alert-success'
when 'alert', 'error'
class_names << 'alert-danger'
end
if sticky if sticky
class_names << 'sticky' class_names << 'sticky'
end end

View file

@ -1,22 +1,13 @@
#flash_messages{ tabindex: '-1' } #flash_messages{ tabindex: '-1', data: { turbo_force: :server } }
#flash_message.center{ class: defined?(unique_classname) ? unique_classname : '' } #flash_message.center{ class: defined?(unique_classname) ? unique_classname : '' }
- if flash.any? - if flash.any?
- flash.each do |key, value| - flash.each do |key, value|
- sticky = defined?(sticky) ? sticky : false - sticky = defined?(sticky) ? sticky : false
- fixed = defined?(fixed) ? fixed : false - fixed = defined?(fixed) ? fixed : false
- if flash_role(key) == 'status' .alert{ role: flash_role(key), class: flash_class(key, sticky: sticky, fixed: fixed) }
.alert.alert-success{ role: 'status', class: flash_class(sticky: sticky, fixed: fixed), tabindex: '-1', data: { controller: 'autofocus' } } - if value.class == Array
- if value.class == Array - value.each do |message|
- value.each do |message| = sanitize_with_link(message)
= sanitize_with_link(message) %br
%br - elsif value.present?
- elsif value.present? = sanitize_with_link(value)
= sanitize_with_link(value)
- elsif flash_role(key) == 'alert'
.alert.alert-danger{ role: 'alert', class: flash_class(sticky: sticky, fixed: fixed), tabindex: '-1', data: { controller: 'autofocus' } }
- if value.class == Array
- value.each do |message|
= sanitize_with_link(message)
%br
- elsif value.present?
= sanitize_with_link(value)

View file

@ -71,8 +71,10 @@ describe ApplicationHelper do
end end
describe "#flash_class" do describe "#flash_class" do
it { expect(flash_class(sticky: true)).to eq 'sticky' } it { expect(flash_class('notice')).to eq 'alert-success' }
it { expect(flash_class(fixed: true)).to eq 'alert-fixed' } it { expect(flash_class('alert', sticky: true, fixed: true)).to eq 'alert-danger sticky alert-fixed' }
it { expect(flash_class('error')).to eq 'alert-danger' }
it { expect(flash_class('unknown-level')).to eq '' }
end end
describe "#try_format_date" do describe "#try_format_date" do