From 68b112e00a3a481cd5ff6e7b02c50c62b48b5a54 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Thu, 6 Jan 2022 12:43:02 +0100 Subject: [PATCH 1/2] helpers: don't crash on unknown flash level Errors generated by the `invisible-catcha` gem are reported as `flash[:error]` (which differs from Rail's usual `flash[:alert]`). Avoid crashing when the flash level passed to this helper is not known. --- app/helpers/application_helper.rb | 9 ++++++--- spec/helpers/application_helper_spec.rb | 6 ++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 98ec37d23..c4dbcee21 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -8,12 +8,15 @@ module ApplicationHelper end def flash_class(level, sticky: false, fixed: false) - class_names = case level + class_names = [] + + case level when 'notice' - ['alert-success'] + class_names << 'alert-success' when 'alert' - ['alert-danger'] + class_names << 'alert-danger' end + if sticky class_names << 'sticky' end diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index ff70654fd..cb7f12e70 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -18,6 +18,12 @@ describe ApplicationHelper do end end + describe "#flash_class" do + it { expect(flash_class('notice')).to eq 'alert-success' } + it { expect(flash_class('alert', sticky: true, fixed: true)).to eq 'alert-danger sticky alert-fixed' } + it { expect(flash_class('unknown-level')).to eq '' } + end + describe "#try_format_date" do subject { try_format_date(date) } From 2e1a3c32cb88480db4603012f00acb733ca147e8 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Thu, 6 Jan 2022 13:21:47 +0100 Subject: [PATCH 2/2] helpers: handle flash[:error] Errors generated by the `invisible-catcha` gem are reported as `flash[:error]` (which differs from Rail's usual `flash[:alert]`). We probably shouldn't use flash[:error] in our own codebase; but we can handle its use in third-party libraries. --- app/helpers/application_helper.rb | 2 +- spec/helpers/application_helper_spec.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index c4dbcee21..e5ee529a3 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -13,7 +13,7 @@ module ApplicationHelper case level when 'notice' class_names << 'alert-success' - when 'alert' + when 'alert', 'error' class_names << 'alert-danger' end diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index cb7f12e70..7fd93a96c 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -21,6 +21,7 @@ describe ApplicationHelper do describe "#flash_class" do it { expect(flash_class('notice')).to eq 'alert-success' } 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