From a2bdac4081eacb710c0b5b061f728610b8261183 Mon Sep 17 00:00:00 2001 From: Hariss Mansoor Date: Fri, 30 Oct 2026 00:30:46 +0500 Subject: [PATCH] Downcase each word of multi-word authentication keys in failure flashes. downcase_first turned "Company Code" into "company Code". Leave acronyms and E-Mail intact. --- CHANGELOG.md | 5 +++++ lib/devise/failure_app.rb | 7 ++++--- test/failure_app_test.rb | 8 ++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ccb0882a4..5ac636b12d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +### Unreleased + +* bug fixes + * Downcase each word of multi-word authentication keys in failure flashes, so "Company Code" is not rendered as "company Code". + ### 5.0.4 - 2026-05-08 * security fixes diff --git a/lib/devise/failure_app.rb b/lib/devise/failure_app.rb index 70cf6d2f31..878ebe19ae 100644 --- a/lib/devise/failure_app.rb +++ b/lib/devise/failure_app.rb @@ -112,9 +112,10 @@ def i18n_message(default = nil) options[:default] = [message] auth_keys = scope_class.authentication_keys human_keys = (auth_keys.respond_to?(:keys) ? auth_keys.keys : auth_keys).map { |key| - # TODO: Remove the fallback and just use `downcase_first` once we drop support for Rails 7.0. - human_key = scope_class.human_attribute_name(key) - human_key.respond_to?(:downcase_first) ? human_key.downcase_first : human_key[0].downcase + human_key[1..] + # Downcase the first letter of each word ("Company Code" => "company code") + # so a title-cased name does not become "company Code" mid-sentence. + # Skip a capital whose next letter is not lowercase, so "ID" and "E-Mail" stay intact. + scope_class.human_attribute_name(key).gsub(/(? OpenStruct.new(message: :invalid)) + assert_equal 'Invalid login ID, company code or password.', @request.flash[:alert] + end + end + test 'humanizes the flash message' do call_failure('warden' => OpenStruct.new(message: :invalid)) assert_equal @request.flash[:alert], @request.flash[:alert].humanize